【问题标题】:how to add global keyevent JComponent如何添加全局keyevent JComponent
【发布时间】:2014-08-02 14:53:45
【问题描述】:

有人知道如何添加组件动作键默认值吗?

我听说过 UImanager actionMap,但我不确定 我有 3 个组合框和 2 个文本字段和 1 个表格 将每个组件添加一个关键侦听器非常浪费时间按 ESC 来处理对话框 喜欢

     KeyAdapter key=new KeyAdapter() {
        @Override
        public void keyPressed(KeyEvent e) {
            int x=JOptionPane.showConfirmDialog(null, "EXIT APP ?", null, JOptionPane.YES_NO_OPTION);
            if(x==JOptionPane.YES_OPTION)
            {
                dialog.setVisible(false);
                dialog.dispose();
            }
        }            
    };
    combo1.addKeyListener(key);
    combo2.addKeyListener(key);
    combo3.addKeyListener(key);
    table.addKeyListener(key);
    dialog.addKeyListener(key);
    text1.addKeyListener(key);
    text2.addKeyListener(key);

如果我从任何组件 JDialog 中按 ESC,任何设置默认键的解决方案都会处理?

【问题讨论】:

  • 只需将键绑定添加到 JDialog 内容窗格。见How to use Key Bindings。我不明白你为什么需要在所有组件中添加一个
  • @peeskillet Just add a key bind to the JDialog content pane - 是的,我发现这很困难。我最初是将绑定添加到根窗格中,这让我很伤心。昨晚我需要的时候这个答案在哪里:)

标签: java swing keylistener key-bindings jdialog


【解决方案1】:

我昨晚只是在玩代码来做到这一点。所以你很幸运能得到一些勺子喂食的代码。

不要忘记阅读 Key Bindings 教程,以便了解解决方案。

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();
            }
        });
    }
}

【讨论】:

  • 感谢您的解决方案,我已经测试过了,但另一个问题是如果已经使用 consumer() 并且 keylistener 仍然可以使用 event 使用 consume(),则键绑定将不起作用
  • 不知道你在说什么。发布的代码都没有使用消耗。无论如何,您不应该使用 KeyListener。此解决方案的任何部分都无需使用 KeyListener。 Swing 旨在与键绑定一起使用。
猜你喜欢
  • 2021-12-20
  • 2022-12-07
  • 2021-04-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-09
  • 2020-11-06
  • 2023-03-17
相关资源
最近更新 更多