- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如何让操作处理程序忽略箭头键?目前,当我尝试使用箭头键导航组合框时,组合框向下/向上移动一次,更改选择,然后触发操作处理程序将焦点移动到按钮。我希望能够使用箭头键导航组合框,并在准备好转到下一个组件时按 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/
如何让操作处理程序忽略箭头键?目前,当我尝试使用箭头键导航组合框时,组合框向下/向上移动一次,更改选择,然后触发操作处理程序将焦点移动到按钮。我希望能够使用箭头键导航组合框,并在准备好转到下一个组件时
我使用 Vaadin 6.8.8。我正在尝试将操作处理程序添加到面板(以获取上下文菜单)。该面板占据了窗口的所有位置,但当我单击鼠标右键时,我导致了浏览器上下文菜单。 public class MyP
我正在尝试通过按如下所示插入this.send()来在Ember中检测ActionHandler#send: Ember.ActionHandler.reopen({ send() { conso
我的问题是,当尝试执行我的代码时,我的actionListener 上不断收到NullPointerException。这是我的登录 View 。经过一些测试后,LoginView 类中出现异常。 p
我使用 javafx 和 scenebuilder 2.0 在 netbeans 中制作了一个基本的登录屏幕!通过复制 YouTube 教程。 如上所示,netbeans 在“#initialize
这是我的代码: seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){ public voi
我是一名优秀的程序员,十分优秀!