gpt4 book ai didi

java - 具有多个输入的 JOptionPane.showConfirmDialog 在按下返回键时提前返回

转载 作者:行者123 更新时间:2023-11-30 06:32:16 26 4
gpt4 key购买 nike

使用 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);

当用户输入 % 字段并按回车键时,代码将返回,但组合框选择尚未完成。返回键==单击“确定”按钮。无论如何要解决这个问题,所以在返回之前需要点击“确定”按钮?

最佳答案

两个选项:

三个选项:

(可能更多)

  • 不要使用 JOptionPane,它会让您的 JTextField 输入按默认值来调用代码以“接受”JOptionPane,然后将其关闭。相反,创建您自己的模态 JDialog,并忽略 JTextField 中的 Enter 键或 Tab 到下一个组件。
  • 或者您可以在 JTextField 中重新分配 Enter 键的键绑定(bind),以不执行任何操作或按 Tab 键跳到下一个组件。这有点诡计,但对我来说更有趣,所以我决定尝试一下,结果如下所示:

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,即可通过 Tab 键切换到下一个组件。由于只要获得焦点并按下 Enter 键,JTextField 的 ActionListener 就会被触发,因此当按下 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);
});
}

关于java - 具有多个输入的 JOptionPane.showConfirmDialog 在按下返回键时提前返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45862356/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com