具有多个输入的 JOptionPane

JOptionPane with multiple inputs

我需要使用 JOptionPane 以不同的方式获取输入。具体来说,我需要一个下拉菜单以及默认输入文本字段,它们都出现在同一个 JOptionPane 中。这是可以实现的吗?如果可以,怎么做?

如果您 pane 中需要不同的组件,您可以尝试实现这样的东西:

JTextField firstName = new JTextField();
JTextField lastName = new JTextField();
JPasswordField password = new JPasswordField();
final JComponent[] inputs = new JComponent[] {
        new JLabel("First"),
        firstName,
        new JLabel("Last"),
        lastName,
        new JLabel("Password"),
        password
};
int result = JOptionPane.showConfirmDialog(null, inputs, "My custom dialog", JOptionPane.PLAIN_MESSAGE);
if (result == JOptionPane.OK_OPTION) {
    System.out.println("You entered " +
            firstName.getText() + ", " +
            lastName.getText() + ", " +
            password.getText());
} else {
    System.out.println("User canceled / closed the dialog, result = " + result);
}