JOptionPane.showConfirmDialog 有多个输入 return 早 return 键按下

JOptionPane.showConfirmDialog with multiple inputs returns early when return key hit

对多个输入使用 JOptionPane.showConfirmDialog:

int result = JOptionPane.showConfirmDialog(null, panel,
            prompt, JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);

面板按以下方式构建:

JTextField percentField = new JTextField(5);

JComboBox cb = new JComboBox(movingAveragesList);            
JPanel myPanel = new JPanel();
myPanel.add(new JLabel("Enter %:"));
myPanel.add(percentField);
myPanel.add(Box.createHorizontalStrut(5)); // a spacer
myPanel.add(new JLabel("Select MA:"));
myPanel.add(cb); 

当用户输入 % 字段并点击 return 时,没有组合框选择的代码 returns 完成。 return 键 == 单击确定按钮。无论如何要解决此问题,因此需要在 returning?

之前点击确定按钮

两个选项:

三个选项:

(可能更多)

  • 不要使用 JOptionPane,它会让您的 JTextField 输入按默认值调用代码 "accept" JOptionPane,然后关闭它。相反,创建您自己的模态 JDialog,并忽略 JTextField 中的输入键或跳转到下一个组件。
  • 您可以在 JTextField 中重新分配 enter 键的键绑定以不执行任何操作或切换到下一个组件。这有点坑爹,但对我来说更有趣一点,所以我决定尝试一下,结果如下图:

public static void main(String[] args) {
    SwingUtilities.invokeLater(() -> {
        JTextField percentField = new JTextField(5);

        // get the JTextField's action and input map
        ActionMap actionMap = percentField.getActionMap();
        int condition = JComponent.WHEN_FOCUSED; // only interested in the input where we have focus
        InputMap inputMap = percentField.getInputMap(condition);

        // get the key for the enter key press in the input map
        Object enterKey = inputMap.get(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0));

        // now re-assign its entry in the action map to tab to next component
        actionMap.put(enterKey, new AbstractAction() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
                manager.focusNextComponent();
            }
        });

        String[] myData = { "One", "Two", "Three", "Four" };
        JComboBox cb = new JComboBox(myData);
        JPanel myPanel = new JPanel();
        myPanel.add(new JLabel("Enter %:"));
        myPanel.add(percentField);
        myPanel.add(Box.createHorizontalStrut(5)); // a spacer
        myPanel.add(new JLabel("Select MA:"));
        myPanel.add(cb);

        String prompt = "Please Select";
        int result = JOptionPane.showConfirmDialog(null, myPanel, prompt,
                JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
    });
}

第三个更简单的选项:

  • 只需为您的 JTextField 提供一个 ActionListener,它可以切换到下一个组件。由于 JTextField 的 ActionListener 在获得焦点并按下 enter 时被触发,这将导致您在按下 enter 时所需的操作。

public static void main(String[] args) {
    SwingUtilities.invokeLater(() -> {
        JTextField percentField = new JTextField(5);
        percentField.addActionListener(e -> {
            KeyboardFocusManager manager = KeyboardFocusManager
                    .getCurrentKeyboardFocusManager();
            manager.focusNextComponent();
        });

        String[] myData = { "One", "Two", "Three", "Four" };
        JComboBox cb = new JComboBox(myData);
        JPanel myPanel = new JPanel();
        myPanel.add(new JLabel("Enter %:"));
        myPanel.add(percentField);
        myPanel.add(Box.createHorizontalStrut(5)); // a spacer
        myPanel.add(new JLabel("Select MA:"));
        myPanel.add(cb);

        String prompt = "Please Select";
        int result = JOptionPane.showConfirmDialog(null, myPanel, prompt,
                JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
    });
}