gpt4 book ai didi

java - JTextField 和 F10 键 - 如何删除键绑定(bind)?

转载 作者:行者123 更新时间:2023-12-01 18:44:02 24 4
gpt4 key购买 nike

我正在尝试从 JTextField 中删除 F10 按钮键绑定(bind),但下面没有任何效果:

getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_F10, KeyEvent.KEY_PRESSED), "none");
getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_F10, KeyEvent.KEY_TYPED), "none");
getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_F10, KeyEvent.KEY_RELEASED), "none");

实际上,我想控制弹出菜单 - 显示/隐藏,但 F10 无法正常工作 - 它正在执行一些其他操作。例如,如果我切换到 F11,一切正常。

据我所知 - Shift + F10 在各种平台上显示弹出窗口。

最佳答案

KeyStroke.getKeyStroke(int, int) 不用于获取按下或释放事件的 KeyStroke,它用于将修饰符应用于 KeyStroke 形式为 KeyEvent.SHIFT_DOWN_MASK 和/或 KeyEvent.CTRL_DOWN_MASK 和/或 KeyEvent.ALT_DOWN_MASK 和/或KeyEvent.ALT_GRAPH_DOWN_MASK 和/或 KeyEvent.META_DOWN_MASK...

而不是 getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_F10, KeyEvent.KEY_PRESSED), "none"); 您应该使用 getInputMap().put(KeyStroke .getKeyStroke(KeyEvent.VK_F10, 0), "无");

当我使用以下内容时,我可以获得在字段聚焦时触发的关键操作...

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestTextFieldKeyStroke {

public static void main(String[] args) {
new TestTextFieldKeyStroke();
}

public TestTextFieldKeyStroke() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public class TestPane extends JPanel {

public TestPane() {
JTextField field = new JTextField(20);
add(field);
JPopupMenu pop = new JPopupMenu();

field.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_F10, 0), "happy");
field.getActionMap().put("happy", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Happy");
}
});

}
}
}

已更新弹出窗口

根据前面的示例,我仍然缺少一些东西,如果我做类似的事情......

final JTextField field = new JTextField(20);
add(field);
JPopupMenu pop = new JPopupMenu();
pop.add(new JLabel("Hello"));
field.setComponentPopupMenu(pop);

field.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_F10, 0), "happy");
field.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_F10, KeyEvent.CTRL_DOWN_MASK), "happy");
field.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_F10, KeyEvent.ALT_DOWN_MASK), "happy");
field.getActionMap().put("happy", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
JPopupMenu popup = field.getComponentPopupMenu();
popup.show(field, 0, field.getHeight());
}
});

我可以让它显示JPopupMenu

enter image description here

关于java - JTextField 和 F10 键 - 如何删除键绑定(bind)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18646886/

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