gpt4 book ai didi

java - JDialog - 如何使回车键响应按钮焦点在对话框上

转载 作者:行者123 更新时间:2023-11-29 03:35:20 35 4
gpt4 key购买 nike

我正在开发 Java Swing 应用程序。

焦点通过左右键切换,当我按下 enter 时,按钮焦点处于 Activity 状态。我的问题是如何使按钮焦点响应输入键。

    private void displayGUI(){

JFrame w = new buildFrame();

w.setDef

aultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
w.addWindowListener(new WindowAdapter(){

@Override
public void windowClosing(WindowEvent e){



Object[] options = {"YES","NO","CANCEL"};

JOptionPane optionPane = new JOptionPane("File haven't save yet." +
" \n Are you want to save the file?",
JOptionPane.QUESTION_MESSAGE,
JOptionPane.YES_NO_CANCEL_OPTION,
null,
options);


JDialog dialog = optionPane.createDialog("Confirm Dialog");

Set<AWTKeyStroke> forwardTraversalKeys =
new HashSet<AWTKeyStroke>(dialog.getFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS));
forwardTraversalKeys.add(AWTKeyStroke.getAWTKeyStroke(KeyEvent.VK_RIGHT, KeyEvent.VK_UNDEFINED));

Set<AWTKeyStroke> backwardTraversalKeys =
new HashSet<AWTKeyStroke>(dialog.getFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS));
backwardTraversalKeys.add(AWTKeyStroke.getAWTKeyStroke(KeyEvent.VK_LEFT, KeyEvent.VK_UNDEFINED));


dialog.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, forwardTraversalKeys);
dialog.setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, backwardTraversalKeys);
dialog.setVisible(true);


Object n = optionPane.getValue();

if(n.toString().equals("YES")){
if(helper.saveFile("text.txt", gatherAllContent(), Swing4.this)){
System.exit(0);
}
label.setText("There is something wrong on quit");

}else if(n.toString().equals("NO")){
System.exit(0);
}else if(n.toString().equals("CANCEL")){
dialog.setVisible(false);
dialog.dispose();
}

}
});

w.setSize(600,600);
w.setLocation(700,100);
w.setVisible(true);

}

dialogEnterKeyAction方法代码

最佳答案

参见 Enter Key and Button对于一种解决方案。

关于java - JDialog - 如何使回车键响应按钮焦点在对话框上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15885259/

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