import java.awt.*; import javax.swing.*;public class InputTest { public static void main(String[] args) { // get first input JOptionPane.showMessageDialog(null, "消息") ; JOptionPane.showOptionDialog(null, "mess", "title", 0, 0, null, null, null); String name = JOptionPane.showInputDialog("What is your name?"); JPanel jp = new JPanel(); BorderLayout borderLayout1 = new BorderLayout(); jp.setLayout(borderLayout1); // get second input String input = JOptionPane.showInputDialog("How old are you?"); // convert string to integer value try{ int age = Integer.parseInt(input); // display output on console System.out.println("Hello, " + name + ". Next year, you'll be " + (age + 1)); } catch(Exception e) { e.printStackTrace(); } finally { System.exit(0); } } }
import javax.swing.*;public class InputTest
{
public static void main(String[] args)
{
// get first input
JOptionPane.showMessageDialog(null, "消息") ;
JOptionPane.showOptionDialog(null, "mess", "title", 0, 0, null, null, null);
String name = JOptionPane.showInputDialog("What is your name?");
JPanel jp = new JPanel();
BorderLayout borderLayout1 = new BorderLayout();
jp.setLayout(borderLayout1); // get second input
String input = JOptionPane.showInputDialog("How old are you?");
// convert string to integer value
try{
int age = Integer.parseInt(input);
// display output on console
System.out.println("Hello, " + name + ". Next year, you'll be " + (age + 1));
}
catch(Exception e) {
e.printStackTrace();
}
finally {
System.exit(0);
}
}
}
点击按钮后:
YouDialog youDialog=new YouDialog(this);//可以设为模式或者非模式
youDialog.show();
youDialog.getParam();//获取你要从其中获取的参数了.自己写就行了,不要找例子了.
import javax.swing.*;
import java.awt.event.*;/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: </p>
* @author unascribed
* @version 1.0
*/public class Frame2 extends JFrame {
private JButton jButton1 = new JButton();
public JLabel jLabel1 = new JLabel();
public JLabel jLabel2 = new JLabel();
public JLabel jLabel3 = new JLabel();
public JLabel jLabel4 = new JLabel(); public Frame2() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Frame2 frame2 = new Frame2();
frame2.setSize(400,300);
frame2.setVisible(true);
}
private void jbInit() throws Exception {
jButton1.setBounds(new Rectangle(26, 20, 88, 31));
jButton1.setFont(new java.awt.Font("Serif", 0, 12));
jButton1.setText("输入参数");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton1_actionPerformed(e);
}
});
this.getContentPane().setLayout(null);
jLabel1.setFont(new java.awt.Font("Serif", 0, 12));
jLabel1.setText("参数1");
jLabel1.setBounds(new Rectangle(78, 84, 100, 32));
jLabel2.setFont(new java.awt.Font("Serif", 0, 12));
jLabel2.setText("参数2");
jLabel2.setBounds(new Rectangle(78, 136, 100, 32));
jLabel3.setFont(new java.awt.Font("Serif", 0, 12));
jLabel3.setText("参数3");
jLabel3.setBounds(new Rectangle(78, 188, 100, 32));
jLabel4.setFont(new java.awt.Font("Serif", 0, 12));
jLabel4.setToolTipText("");
jLabel4.setText("参数4");
jLabel4.setBounds(new Rectangle(78, 244, 100, 32));
this.getContentPane().add(jButton1, null);
this.getContentPane().add(jLabel1, null);
this.getContentPane().add(jLabel4, null);
this.getContentPane().add(jLabel2, null);
this.getContentPane().add(jLabel3, null);
} void jButton1_actionPerformed(ActionEvent e) { Dialog1 dlog = new Dialog1(this,"",true);
dlog.setSize(400,200);
dlog.setVisible(true);
}
}
import javax.swing.*;
import com.borland.jbcl.layout.*;
import java.awt.event.*;/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: </p>
* @author unascribed
* @version 1.0
*/public class Dialog1 extends JDialog {
private JPanel panel1 = new JPanel();
private JTextField jTextField1 = new JTextField();
private JTextField jTextField2 = new JTextField();
private JTextField jTextField3 = new JTextField();
private JTextField jTextField4 = new JTextField();
private JLabel jLabel1 = new JLabel();
private JLabel jLabel2 = new JLabel();
private JLabel jLabel3 = new JLabel();
private JLabel jLabel4 = new JLabel();
private JButton jButton1 = new JButton();
private JButton jButton2 = new JButton(); Frame2 frm;
public Dialog1(Frame2 frame, String title, boolean modal) {
super(frame, title, modal);
try {
jbInit();
frm = frame;
pack();
}
catch(Exception ex) {
ex.printStackTrace();
}
} public Dialog1() {
this(null, "", false);
}
private void jbInit() throws Exception {
panel1.setLayout(null);
this.getContentPane().setLayout(null);
jTextField1.setFont(new java.awt.Font("Serif", 0, 12));
jTextField1.setBounds(new Rectangle(15, 36, 63, 27));
jTextField2.setFont(new java.awt.Font("Serif", 0, 12));
jTextField2.setBounds(new Rectangle(87, 36, 63, 27));
jTextField3.setFont(new java.awt.Font("Serif", 0, 12));
jTextField3.setBounds(new Rectangle(161, 36, 63, 27));
jTextField4.setFont(new java.awt.Font("Serif", 0, 12));
jTextField4.setBounds(new Rectangle(239, 36, 63, 27));
jLabel1.setFont(new java.awt.Font("Serif", 0, 12));
jLabel1.setText("参数1");
jLabel1.setBounds(new Rectangle(13, 7, 47, 24));
jLabel2.setBounds(new Rectangle(90, 7, 47, 24));
jLabel2.setFont(new java.awt.Font("Serif", 0, 12));
jLabel2.setText("参数2");
jLabel3.setFont(new java.awt.Font("Serif", 0, 12));
jLabel3.setText("参数3");
jLabel3.setBounds(new Rectangle(165, 8, 53, 23));
jLabel4.setBounds(new Rectangle(251, 7, 54, 23));
jLabel4.setFont(new java.awt.Font("Serif", 0, 12));
jLabel4.setText("参数4");
panel1.setBounds(new Rectangle(0, 0, 336, 96));
jButton1.setBounds(new Rectangle(149, 67, 52, 26));
jButton1.setFont(new java.awt.Font("Serif", 0, 12));
jButton1.setText("ok");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton1_actionPerformed(e);
}
});
jButton2.setBounds(new Rectangle(218, 64, 77, 29));
jButton2.setFont(new java.awt.Font("Serif", 0, 12));
jButton2.setText("cancel");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton2_actionPerformed(e);
}
});
panel1.add(jTextField1, null);
panel1.add(jTextField2, null);
panel1.add(jTextField3, null);
panel1.add(jTextField4, null);
panel1.add(jLabel1, null);
panel1.add(jLabel2, null);
panel1.add(jLabel4, null);
panel1.add(jButton1, null);
panel1.add(jButton2, null);
panel1.add(jLabel3, null);
this.getContentPane().add(panel1, null);
} void jButton2_actionPerformed(ActionEvent e) {
this.dispose();
} void jButton1_actionPerformed(ActionEvent e) {
frm.jLabel1.setText(this.jTextField1.getText());
frm.jLabel2.setText(this.jTextField2.getText());
frm.jLabel3.setText(this.jTextField3.getText());
frm.jLabel4.setText(this.jTextField4.getText());
this.dispose();
}
}
你可以扩展一个对话框,传一个JTextField参数进去即可(如果只想得到一个String对象,而不是需要将其放入主界面JTextField,则传入一个String对象进去即可),设置其构造器为:
private JTextField mText;//可以不显示出来
private JTextField mTextShow;//显示出来,用以接收用户输入数据public mydialog(Frame frame,boolean modal,JTextField text) {
super(frame,modal);
this.mText= text;
}
//确定按钮返回后,给mText赋值即可
void jButton1_actionPerformed(ActionEvent e) {
this.mText.setText(mTextShow.getText().trim());
//也可以按需求处理,判断等
}主界面调用如下:
mydialog tp2 = new mydialog(this, true, jTextField2);
tp2.show();
其实继承一个JDialog几可以实现了
点击按钮后:
YouDialog youDialog=new YouDialog(this);//可以设为模式或者非模式
youDialog.show();
youDialog.getParam();//