简体   繁体   English

具有多个输入的JOptionPane

[英]JOptionPane with multiple inputs

I need to get input in different ways using JOptionPane. 我需要使用JOptionPane以不同的方式获取输入。 Specifically, I need a dropdown menu along with the default input text field to both be present in the same JOptionPane. 具体来说,我需要一个下拉菜单以及默认的输入文本字段,以将它们都显示在同一JOptionPane中。 Is this achievable? 这可以实现吗? if so, how? 如果是这样,怎么办?

If you need different components in your pane , you can try to implement something like this: 如果您需要在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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM