当用户单击一个按钮时,我想显示一个应该至少有两个 JTextField 和两个 JLabel 的弹出窗体,所以使用 JOptionPane.showInputDialog
是不可能的。
最佳答案
您至少应该考虑 JOptionPane
之一showInputDialog()
或 showMessageDialog()
等方法。
附录:使用的选择JOptionPane
更多地取决于模态的适用性,而不是显示的组件数量。另见 How to Make Dialogs .
附录:如@camickr 的评论所述,您可以使用 Dialog Focus 中讨论的方法将焦点设置到特定组件。 , 引用 here .
package gui;
import java.awt.EventQueue;
import java.awt.GridLayout;
import javax.swing.*;
/** @see https://stackoverflow.com/a/3002830/230513 */
class JOptionPaneTest {
private static void display() {
String[] items = {"One", "Two", "Three", "Four", "Five"};
JComboBox<String> combo = new JComboBox<>(items);
JTextField field1 = new JTextField("1234.56");
JTextField field2 = new JTextField("9876.54");
JPanel panel = new JPanel(new GridLayout(0, 1));
panel.add(combo);
panel.add(new JLabel("Field 1:"));
panel.add(field1);
panel.add(new JLabel("Field 2:"));
panel.add(field2);
int result = JOptionPane.showConfirmDialog(null, panel, "Test",
JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
if (result == JOptionPane.OK_OPTION) {
System.out.println(combo.getSelectedItem()
+ " " + field1.getText()
+ " " + field2.getText());
} else {
System.out.println("Cancelled");
}
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
display();
}
});
}
}
关于java - 具有至少两个字段的简单弹出 java 表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3002787/