JOptionPane.showInputDialog中的多个input

有没有办法在JOptionPane.showInputDialog创build多个input,而不是只有一个input?

是。 您知道,您可以将任何Object放入大多数JOptionPane.showXXX methodsObject参数中,并且通常该Object恰好是JPanel

在你的情况下,也许你可以使用一个有几个JTextFieldsJPanel

 import javax.swing.*; public class JOptionPaneMultiInput { public static void main(String[] args) { JTextField xField = new JTextField(5); JTextField yField = new JTextField(5); JPanel myPanel = new JPanel(); myPanel.add(new JLabel("x:")); myPanel.add(xField); myPanel.add(Box.createHorizontalStrut(15)); // a spacer myPanel.add(new JLabel("y:")); myPanel.add(yField); int result = JOptionPane.showConfirmDialog(null, myPanel, "Please Enter X and Y Values", JOptionPane.OK_CANCEL_OPTION); if (result == JOptionPane.OK_OPTION) { System.out.println("x value: " + xField.getText()); System.out.println("y value: " + yField.getText()); } } } 

这是我的解决scheme

 JTextField username = new JTextField(); JTextField password = new JPasswordField(); Object[] message = { "Username:", username, "Password:", password }; int option = JOptionPane.showConfirmDialog(null, message, "Login", JOptionPane.OK_CANCEL_OPTION); if (option == JOptionPane.OK_OPTION) { if (username.getText().equals("h") && password.getText().equals("h")) { System.out.println("Login successful"); } else { System.out.println("login failed"); } } else { System.out.println("Login canceled"); }