当用户单击按钮时,我想显示一个弹出窗体,该窗体至少应具有两个JTextField和两个JLabel,因此JOptionPane.showInputDialog不可能使用。
JOptionPane.showInputDialog
你至少应考虑JOptionPane诸如showInputDialog()或的方法之一showMessageDialog()。
JOptionPane
showInputDialog()
showMessageDialog()
附录:使用JOptionPane铰链的选择更多地取决于模态的适用性,而不是所显示的组件数量。另请参见如何制作对话框。
附录:如@camickr的评论中所述,你可以使用Dialog Focus中讨论的方法将焦点设置为特定组件,此处引用。
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(); } }); } }