具有多个输入的JOptionPane

时间:2019-02-21 06:22:46

标签: java joptionpane

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

1 个答案:

答案 0 :(得分:0)

如果您的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);
}