在actionPerformed方法中获取传输的值:
public void actionPerformed(ActionEvent e){
str1=text1.getText();//text1是你在对话框中定义的jtextfield的名字,引用
str2=text2.getText();
}你也可以JOptionPane.showInputDialog(),如果是在swing中的话!
public void actionPerformed(ActionEvent e){
str1=text1.getText();//text1是你在对话框中定义的jtextfield的名字,引用
str2=text2.getText();
}你也可以JOptionPane.showInputDialog(),如果是在swing中的话!
2、下面的代码是例子程序中的一个对话框,不知道你需要用户输入、选择还是其他,如果比较特殊,可以自己设计对话框,其中包含的元素自己定制。
public JButton createComponentDialogButton() {
Action a = new AbstractAction(getString("OptionPaneDemo.componentbutton")) {
public void actionPerformed(ActionEvent e) {
// In a ComponentDialog, you can show as many message components and
// as many options as you want:
// Messages
Object[] message = new Object[4];
message[0] = getString("OptionPaneDemo.componentmessage");
message[1] = new JTextField(getString("OptionPaneDemo.componenttextfield"));
JComboBox cb = new JComboBox();
cb.addItem(getString("OptionPaneDemo.component_cb1"));
cb.addItem(getString("OptionPaneDemo.component_cb2"));
cb.addItem(getString("OptionPaneDemo.component_cb3"));
message[2] = cb;
message[3] = getString("OptionPaneDemo.componentmessage2");
// Options
String[] options = {
getString("OptionPaneDemo.component_op1"),
getString("OptionPaneDemo.component_op2"),
getString("OptionPaneDemo.component_op3"),
getString("OptionPaneDemo.component_op4"),
getString("OptionPaneDemo.component_op5")
};
int result = JOptionPane.showOptionDialog(
getDemoPanel(), // the parent that the dialog blocks
message, // the dialog message array
getString("OptionPaneDemo.componenttitle"), // the title of the dialog window
JOptionPane.DEFAULT_OPTION, // option type
JOptionPane.INFORMATION_MESSAGE, // message type
null, // optional icon, use null to use the default icon
options, // options string array, will be made into buttons
options[3] // option that should be made into a default button
);
switch(result) {
case 0: // yes
JOptionPane.showMessageDialog(getDemoPanel(), getString("OptionPaneDemo.component_r1"));
break;
case 1: // no
JOptionPane.showMessageDialog(getDemoPanel(), getString("OptionPaneDemo.component_r2"));
break;
case 2: // maybe
JOptionPane.showMessageDialog(getDemoPanel(), getString("OptionPaneDemo.component_r3"));
break;
case 3: // probably
JOptionPane.showMessageDialog(getDemoPanel(), getString("OptionPaneDemo.component_r4"));
break;
default:
break;
}
}
};
return createButton(a);
}