Java 如何添加全局keyevent JComponent
有人知道如何添加组件操作键默认值吗 我听说过UImanager actionMap,但我不确定 我有3个组合框、2个文本字段和1个表格 将每个组件添加到一个按键侦听器中是非常浪费时间的,请按ESC键以释放对话框 像Java 如何添加全局keyevent JComponent,java,swing,keylistener,key-bindings,jdialog,Java,Swing,Keylistener,Key Bindings,Jdialog,有人知道如何添加组件操作键默认值吗 我听说过UImanager actionMap,但我不确定 我有3个组合框、2个文本字段和1个表格 将每个组件添加到一个按键侦听器中是非常浪费时间的,请按ESC键以释放对话框 像 如果我从任何组件按ESC键,JDialog会处理任何设置默认键的解决方案吗?我昨晚只是在玩代码。所以你有一些填鸭式的密码就很幸运了 不要忘记阅读密钥绑定教程,以便理解解决方案 import java.awt.*; import java.awt.event.*; import jav
如果我从任何组件按ESC键,JDialog会处理任何设置默认键的解决方案吗?我昨晚只是在玩代码。所以你有一些填鸭式的密码就很幸运了 不要忘记阅读密钥绑定教程,以便理解解决方案
import java.awt.*;
import java.awt.event.*;
import java.util.List;
import javax.swing.*;
import javax.swing.event.*;
/**
** This class will close a JDialog (or a window) when the Escape key is used.
*/
public class EscapeAction extends AbstractAction
{
public void actionPerformed(ActionEvent e)
{
Component c = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
SwingUtilities.windowForComponent(c).dispose();
}
private static void createAndShowGUI()
{
JDialog dialog = new JDialog();
JMenuBar menuBar = new JMenuBar();
dialog.setJMenuBar( menuBar );
JMenu menu = new JMenu( "File" );
menuBar.add( menu );
menu.add( new JMenuItem("FileMenuA") );
JMenu subMenu = new JMenu( "SubFileMenu" );
menu.add( subMenu );
subMenu.add( new JMenuItem("SubFileMenuA") );
menu.add( new JMenuItem("FileMenuB") );
menu.add( new JMenuItem("FileMenuC") );
JPopupMenu popup = new JPopupMenu();
popup.add( new JMenuItem("SubMenuA") );
popup.add( new JMenuItem("SubMenuB") );
popup.add( new JMenuItem("SubMenuC") );
popup.add( new JMenuItem("SubMenuD") );
String[] items = { "Select Item", "Color", "Shape", "Fruit" };
JComboBox<String> comboBox = new JComboBox<String>( items );
dialog.add(comboBox, BorderLayout.NORTH);
JTextField textField = new JTextField("Right Click For Popup");
textField.setComponentPopupMenu(popup);
dialog.add(textField);
dialog.setDefaultCloseOperation( JDialog.DISPOSE_ON_CLOSE );
dialog.setSize(200, 200);
dialog.setLocationRelativeTo(null);
dialog.setVisible( true );
// Add the Key Bindings to the content pane for the EscapeAction
JPanel contentPane = (JPanel)dialog.getContentPane();
String escapeText = "ESCAPE";
KeyStroke escapeKeyStroke = KeyStroke.getKeyStroke(escapeText);
contentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(escapeKeyStroke, escapeText);
contentPane.getActionMap().put(escapeText, new EscapeAction());
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
}
}
import java.awt.*;
导入java.awt.event.*;
导入java.util.List;
导入javax.swing.*;
导入javax.swing.event.*;
/**
**当使用Escape键时,此类将关闭JDialog(或窗口)。
*/
公共类EscapeAction扩展了AbstractAction
{
已执行的公共无效操作(操作事件e)
{
组件c=KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
windowForComponent(c.dispose();
}
私有静态void createAndShowGUI()
{
JDialog dialog=新建JDialog();
JMenuBar menuBar=新的JMenuBar();
setJMenuBar(菜单栏);
JMenu菜单=新JMenu(“文件”);
菜单栏。添加(菜单);
添加(新JMenuItem(“FileMenuA”);
JMenu子菜单=新建JMenu(“子文件菜单”);
菜单.添加(子菜单);
添加(新JMenuItem(“SubFileMenuA”);
添加(新JMenuItem(“FileMenuB”);
添加(新JMenuItem(“FileMenuC”);
JPopupMenu popup=新的JPopupMenu();
添加(新项目(“子菜单”);
添加(新项(“子菜单”);
添加(新项目(“子菜单”);
添加(新项目(“子菜单”);
字符串[]项={“选择项”、“颜色”、“形状”、“水果”};
JComboBox组合框=新的JComboBox(项目);
添加(组合框,BorderLayout.NORTH);
JTextField textField=新建JTextField(“右键单击弹出窗口”);
textField.setComponentPopupMenu(弹出菜单);
添加(文本字段);
setDefaultCloseOperation(JDialog.DISPOSE\u ON\u CLOSE);
对话框。设置大小(200200);
对话框.setLocationRelativeTo(空);
对话框.setVisible(true);
//将键绑定添加到EscapeAction的内容窗格
JPanel contentPane=(JPanel)dialog.getContentPane();
字符串escapeText=“ESCAPE”;
击键escapeKeyStroke=击键.getKeyStroke(escapeText);
getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(escapeKeyStroke,escapeText);
contentPane.getActionMap().put(escapeText,新的EscapeAction());
}
公共静态void main(字符串[]args)
{
invokeLater(新的Runnable()
{
公开募捐
{
createAndShowGUI();
}
});
}
}
只需在JDialog内容窗格中添加一个键绑定即可。看见我不明白你为什么要在所有的数据中添加一个compoents@peeskillet只需在JDialog内容窗格中添加一个键绑定即可
——是的,我很难找到答案。我最初是在根窗格中添加绑定的,这让我很伤心。昨晚我需要它的时候,这个答案在哪里:)谢谢你的解决方案,我已经测试过了,但是另一个问题是如果已经使用()并且keylistener仍然工作,那么键绑定将无法工作。事件使用consume()不知道你在说什么。发布的代码都没有使用consume。无论如何,您不应该使用KeyListener。此解决方案的任何部分都不需要使用KeyListener。Swing设计用于键绑定。
import java.awt.*;
import java.awt.event.*;
import java.util.List;
import javax.swing.*;
import javax.swing.event.*;
/**
** This class will close a JDialog (or a window) when the Escape key is used.
*/
public class EscapeAction extends AbstractAction
{
public void actionPerformed(ActionEvent e)
{
Component c = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
SwingUtilities.windowForComponent(c).dispose();
}
private static void createAndShowGUI()
{
JDialog dialog = new JDialog();
JMenuBar menuBar = new JMenuBar();
dialog.setJMenuBar( menuBar );
JMenu menu = new JMenu( "File" );
menuBar.add( menu );
menu.add( new JMenuItem("FileMenuA") );
JMenu subMenu = new JMenu( "SubFileMenu" );
menu.add( subMenu );
subMenu.add( new JMenuItem("SubFileMenuA") );
menu.add( new JMenuItem("FileMenuB") );
menu.add( new JMenuItem("FileMenuC") );
JPopupMenu popup = new JPopupMenu();
popup.add( new JMenuItem("SubMenuA") );
popup.add( new JMenuItem("SubMenuB") );
popup.add( new JMenuItem("SubMenuC") );
popup.add( new JMenuItem("SubMenuD") );
String[] items = { "Select Item", "Color", "Shape", "Fruit" };
JComboBox<String> comboBox = new JComboBox<String>( items );
dialog.add(comboBox, BorderLayout.NORTH);
JTextField textField = new JTextField("Right Click For Popup");
textField.setComponentPopupMenu(popup);
dialog.add(textField);
dialog.setDefaultCloseOperation( JDialog.DISPOSE_ON_CLOSE );
dialog.setSize(200, 200);
dialog.setLocationRelativeTo(null);
dialog.setVisible( true );
// Add the Key Bindings to the content pane for the EscapeAction
JPanel contentPane = (JPanel)dialog.getContentPane();
String escapeText = "ESCAPE";
KeyStroke escapeKeyStroke = KeyStroke.getKeyStroke(escapeText);
contentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(escapeKeyStroke, escapeText);
contentPane.getActionMap().put(escapeText, new EscapeAction());
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
}
}