简体   繁体   English

如何添加全局keyevent JComponent

[英]how to add global keyevent JComponent

anyone know how to add Component action key default ? 有谁知道如何添加组件操作键默认值?

i hear about UImanager actionMap, but im not sure i have 3 combo box and 2 text field and 1 table its very wasting time to add each component a key listener press ESC to dispose Dialog like 我听说过UImanager actionMap,但是我不确定我有3个组合框,2个文本字段和1个表,这非常浪费时间,将每个组件添加到按键侦听器中,按ESC来放置Dialog,

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

any solution to make default key if i press ESC from any Component JDialog will dispose ? 如果我从任何组件JDialog按下ESC,将做出默认键的任何解决方案?

I was just playing with code to do this last night. 昨晚我只是在玩代码来做到这一点。 So you get lucky with some spoon fed code. 因此,您将获得一些幸运的代码。

Don't forget to read the Key Bindings tutorial so you understand the solution. 不要忘记阅读“键绑定”教程,以便您了解解决方案。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM