gpt4 book ai didi

java - 使 ActionHandler 忽略箭头键

转载 作者:行者123 更新时间:2023-12-02 13:14:22 26 4
gpt4 key购买 nike

如何让操作处理程序忽略箭头键?目前,当我尝试使用箭头键导航组合框时,组合框向下/向上移动一次,更改选择,然后触发操作处理程序将焦点移动到按钮。我希望能够使用箭头键导航组合框,并在准备好转到下一个组件时按 Enter。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class test {

JFrame window = new JFrame("testGUI");
JPanel windowPanel = new JPanel();

public static JLabel labelSize;
public static JComboBox<String> comboSize;

public static JLabel labelButton;
public static JButton buttonButton;

public test () {
super();

labelSize = new JLabel("Monster Size:");
String[] sizeChoices = { "None", "Tiny", "Small", "Medium", "Large", "Huge", "Colossal"};
comboSize = new JComboBox<String>(sizeChoices);
comboSize.setToolTipText("The creature's size.");
comboSize.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_ENTER){
comboSize.showPopup();
}
}
@Override
public void keyReleased(KeyEvent e) {
comboSize.showPopup();
}
@Override
public void keyPressed(KeyEvent e) {
}
});

labelButton = new JLabel("Button:");
buttonButton = new JButton();
buttonButton.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_ENTER)
buttonButton.doClick();
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
buttonButton.doClick();
}
});


windowPanel.setLayout(new FlowLayout());
windowPanel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
windowPanel.add(labelSize);
windowPanel.add(comboSize);
windowPanel.add(labelButton);
windowPanel.add(buttonButton);
windowPanel.setVisible(true);

window.setSize(500, 500);
window.setLayout(new FlowLayout());
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setVisible(true);
window.add(windowPanel);

comboSize.addActionListener(handler);
buttonButton.addActionListener(handler);
}

ActionHandler handler = new ActionHandler();
public class ActionHandler implements ActionListener {
public void actionPerformed(ActionEvent eventFocus){
if (eventFocus.getSource() == comboSize){
buttonButton.requestFocusInWindow();
}
if (eventFocus.getSource() == buttonButton){
comboSize.requestFocusInWindow();
}
}
}

@SuppressWarnings("unused")
public static void main(String[] args) {
test GUITest = new test();
}
}

最佳答案

the combo box moves down/up once, changes the selection, and then triggers the Action Handler moving the focus to the button. I would like to be able to navigate the combo box with the arrow keys, and hit Enter when I'm ready to move on to the next component.

您可以使用以下命令在组合框上设置一个属性,以便仅在按下 Enter 时生成 ActionEvent:

comboBox.putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE);

完整示例:

/*
This works on non editable combo boxes
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.plaf.basic.*;
import javax.swing.text.*;

public class ComboBoxAction extends JFrame implements ActionListener
{
public ComboBoxAction()
{
JComboBox<String> comboBox = new JComboBox<String>();
comboBox.addActionListener( this );

comboBox.addItem( "Item 1" );
comboBox.addItem( "Item 2" );
comboBox.addItem( "Item 3" );
comboBox.addItem( "Item 4" );

// This prevents action events from being fired when the
// up/down arrow keys are used on the dropdown menu

comboBox.putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE);

getContentPane().add( comboBox );
getContentPane().add( new JTextField(), BorderLayout.SOUTH );
}

public void actionPerformed(ActionEvent e)
{
System.out.println( e.getModifiers() );

JComboBox comboBox = (JComboBox)e.getSource();
System.out.println( comboBox.getSelectedItem() );

// make sure popup is closed when 'isTableCellEditor' is used

// comboBox.hidePopup();
}

public static void main(String[] args)
{
ComboBoxAction frame = new ComboBoxAction();
frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
frame.pack();
frame.setVisible( true );
}
}

参见Combo Box No Action了解更多信息。

关于java - 使 ActionHandler 忽略箭头键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43830825/

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