我正在尝试在带有拆分 Pane 的 JFrame 中实现热键,其中一个 Pane 上有一个 JTree。键绑定(bind)工作得很好,除了当用户编辑 JTree 节点的名称时,如果他们按下具有键绑定(bind)的键,则在文本区域中键入击键并且它会触发键绑定(bind)。关于如何允许编辑节点,同时仍然实现热键,有什么想法吗?
下面是演示该行为的示例。 “1”和“2”键已绑定(bind),因此如果您在文本区域中键入其中一个,您将看到弹出窗口。
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JToolBar;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
public class KeyBindingTest {
static JButton button1;
static JButton button2;
static JPanel panel;
public KeyBindingTest() {
panel = new JPanel(new BorderLayout());
JToolBar tb = new JToolBar();
tb.setFloatable(false);
button1 = new JButton("First Button");
button1.addActionListener(new ButtonAction());
button2 = new JButton("Second Button");
button2.addActionListener(new ButtonAction());
tb.add(button1);
tb.add(button2);
panel.add(tb, BorderLayout.PAGE_START);
JTextArea ta = new JTextArea(10, 30);
JScrollPane opts = new JScrollPane(ta);
panel.add(opts, BorderLayout.PAGE_END);
setKeyBindings(tb);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
KeyBindingTest test = new KeyBindingTest();
test.createAndShowUI();
}
});
}
private void setKeyBindings(JToolBar tb) {
tb.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_1, 0), "first");
tb.getActionMap().put("first", new ButtonAction());
tb.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_2, 0), "second");
tb.getActionMap().put("second", new ButtonAction());
}
private void createAndShowUI() {
JFrame frame = new JFrame();
frame.getRootPane().setDefaultButton(button1);
frame.setLayout(new BorderLayout());
frame.add(panel, BorderLayout.CENTER);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public class ButtonAction extends AbstractAction {
private static final long serialVersionUID = 1L;
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(panel, "You pressed a button \n" + e.paramString());
}
}
}
最佳答案
这就是为什么热键通常具有 Alt 或 Control 修饰符的原因。
您可以修改操作来确定哪个组件具有焦点:
@Override
public void actionPerformed(ActionEvent e)
{
KeyboardFocusManager kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager();
Component focusedComponent = kfm.getFocusOwner();
if (focusedComponent instanceof JTextArea)
return;
JOptionPane.showMessageDialog(panel, "You pressed a button \n"+e.paramString());
}
关于java - 如何防止在 TextArea 中输入也触发按键绑定(bind)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46919007/