Using Keybinding to get keycode

前端 未结 2 1209
耶瑟儿~
耶瑟儿~ 2021-01-25 08:48

I have a function that display the buffered image. I want mouse listener and key listener to be implemented in JLabel which loads the image. Since JLabel cannot get focus i trie

2条回答
  •  孤街浪徒
    2021-01-25 09:09

    JLabel cannot get focus

    Sure it can.

    import java.awt.Color;
    import java.awt.event.*;
    import javax.swing.*;
    
    class LabelFocus {
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    final JLabel l = new JLabel("Focus now!");
                    l.setFocusable(true);
                    l.setOpaque(true);
                    l.addFocusListener(new FocusListener(){
                        Color focused = Color.CYAN;
                        Color unfocused = Color.ORANGE;
                        public void focusLost(FocusEvent fe) {
                            l.setBackground(unfocused);
                        }
                        public void focusGained(FocusEvent fe) {
                            l.setBackground(focused);
                        }
                    });
                    JOptionPane.showMessageDialog(null, l);
                }
            });
        }
    }
    

    Update

    Now I've read the question more carefully, here is an example of detecting focus, mouse and key typed events in a label.

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    class LabelFocus {
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    JPanel gui = new JPanel(new BorderLayout(3,3));
                    final String eol = System.getProperty("line.separator");
                    final JTextArea ta = new JTextArea(15,60);
                    ta.setEditable(false);
                    gui.add(new JScrollPane(ta), BorderLayout.CENTER);
                    final JLabel l = new JLabel("Focus now!");
                    gui.add(l, BorderLayout.NORTH);
                    l.setFocusable(true);
                    l.setOpaque(true);
    
                    l.addFocusListener(new FocusListener(){
                        Color focused = Color.CYAN;
                        Color unfocused = Color.ORANGE;
                        public void focusLost(FocusEvent fe) {
                            l.setBackground(unfocused);
                            ta.append(fe.toString());
                            ta.append(eol);
                        }
                        public void focusGained(FocusEvent fe) {
                            l.setBackground(focused);
                            ta.append(fe.toString());
                            ta.append(eol);
                        }
                    });
    
                    l.addMouseListener(new MouseAdapter() {
                            @Override
                            public void mouseEntered(MouseEvent me) {
                                ta.append(me.toString());
                                ta.append(eol);
                            }
                            @Override
                            public void mouseExited(MouseEvent me) {
                                ta.append(me.toString());
                                ta.append(eol);
                            }
                        });
                    l.addKeyListener(new KeyAdapter() {
                            @Override
                            public void keyTyped(KeyEvent ke) {
                                ta.append(ke.toString());
                                ta.append(eol);
                            }
                        });
    
                    JOptionPane.showMessageDialog(null, gui);
                }
            });
        }
    }
    

    Example output

    java.awt.FocusEvent[FOCUS_GAINED,permanent,opposite=javax.swing.JButton[OptionPane.button,335,6,39x26,alignmentX=0.0,alignmentY=0.5,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@1813fac,flags=296,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=java.awt.Insets[top=2,left=8,bottom=2,right=8],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=OK,defaultCapable=true],cause=TRAVERSAL_FORWARD] on javax.swing.JLabel[,0,0,663x16,alignmentX=0.0,alignmentY=0.0,border=,flags=25165832,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,horizontalAlignment=LEADING,horizontalTextPosition=TRAILING,iconTextGap=4,labelFor=,text=Focus now!,verticalAlignment=CENTER,verticalTextPosition=CENTER]
    java.awt.event.KeyEvent[KEY_TYPED,keyCode=0,keyText=Unknown keyCode: 0x0,keyChar='s',keyLocation=KEY_LOCATION_UNKNOWN,rawCode=0,primaryLevelUnicode=0,scancode=0] on javax.swing.JLabel[,0,0,663x16,alignmentX=0.0,alignmentY=0.0,border=,flags=25165832,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,horizontalAlignment=LEADING,horizontalTextPosition=TRAILING,iconTextGap=4,labelFor=,text=Focus now!,verticalAlignment=CENTER,verticalTextPosition=CENTER]
    java.awt.event.KeyEvent[KEY_TYPED,keyCode=0,keyText=Unknown keyCode: 0x0,keyChar='d',keyLocation=KEY_LOCATION_UNKNOWN,rawCode=0,primaryLevelUnicode=0,scancode=0] on javax.swing.JLabel[,0,0,663x16,alignmentX=0.0,alignmentY=0.0,border=,flags=25165832,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,horizontalAlignment=LEADING,horizontalTextPosition=TRAILING,iconTextGap=4,labelFor=,text=Focus now!,verticalAlignment=CENTER,verticalTextPosition=CENTER]
    java.awt.event.KeyEvent[KEY_TYPED,keyCode=0,keyText=Unknown keyCode: 0x0,keyChar='f',keyLocation=KEY_LOCATION_UNKNOWN,rawCode=0,primaryLevelUnicode=0,scancode=0] on javax.swing.JLabel[,0,0,663x16,alignmentX=0.0,alignmentY=0.0,border=,flags=25165832,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,horizontalAlignment=LEADING,horizontalTextPosition=TRAILING,iconTextGap=4,labelFor=,text=Focus now!,verticalAlignment=CENTER,verticalTextPosition=CENTER]
    java.awt.event.KeyEvent[KEY_TYPED,keyCode=0,keyText=Unknown keyCode: 0x0,keyChar='s',keyLocation=KEY_LOCATION_UNKNOWN,rawCode=0,primaryLevelUnicode=0,scancode=0] on javax.swing.JLabel[,0,0,663x16,alignmentX=0.0,alignmentY=0.0,border=,flags=25165832,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,horizontalAlignment=LEADING,horizontalTextPosition=TRAILING,iconTextGap=4,labelFor=,text=Focus now!,verticalAlignment=CENTER,verticalTextPosition=CENTER]
    java.awt.event.KeyEvent[KEY_TYPED,keyCode=0,keyText=Unknown keyCode: 0x0,keyChar='d',keyLocation=KEY_LOCATION_UNKNOWN,rawCode=0,primaryLevelUnicode=0,scancode=0] on javax.swing.JLabel[,0,0,663x16,alignmentX=0.0,alignmentY=0.0,border=,flags=25165832,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,horizontalAlignment=LEADING,horizontalTextPosition=TRAILING,iconTextGap=4,labelFor=,text=Focus now!,verticalAlignment=CENTER,verticalTextPosition=CENTER]
    java.awt.event.KeyEvent[KEY_TYPED,keyCode=0,keyText=Unknown keyCode: 0x0,keyChar='f',keyLocation=KEY_LOCATION_UNKNOWN,rawCode=0,primaryLevelUnicode=0,scancode=0] on javax.swing.JLabel[,0,0,663x16,alignmentX=0.0,alignmentY=0.0,border=,flags=25165832,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,horizontalAlignment=LEADING,horizontalTextPosition=TRAILING,iconTextGap=4,labelFor=,text=Focus now!,verticalAlignment=CENTER,verticalTextPosition=CENTER]
    java.awt.FocusEvent[FOCUS_LOST,permanent,opposite=javax.swing.JTextArea[,0,0,4970x240,layout=javax.swing.plaf.basic.BasicTextUI$UpdateHandler,alignmentX=0.0,alignmentY=0.0,border=javax.swing.plaf.basic.BasicBorders$MarginBorder@1855af5,flags=296,maximumSize=,minimumSize=,preferredSize=,caretColor=sun.swing.PrintColorUIResource[r=51,g=51,b=51],disabledTextColor=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],editable=false,margin=javax.swing.plaf.InsetsUIResource[top=0,left=0,bottom=0,right=0],selectedTextColor=sun.swing.PrintColorUIResource[r=51,g=51,b=51],selectionColor=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],colums=60,columWidth=11,rows=15,rowHeight=16,word=false,wrap=false],cause=TRAVERSAL_FORWARD] on javax.swing.JLabel[,0,0,663x16,alignmentX=0.0,alignmentY=0.0,border=,flags=25165832,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,horizontalAlignment=LEADING,horizontalTextPosition=TRAILING,iconTextGap=4,labelFor=,text=Focus now!,verticalAlignment=CENTER,verticalTextPosition=CENTER]
    java.awt.event.MouseEvent[MOUSE_ENTERED,(215,14),absolute(867,418),button=0,clickCount=0] on javax.swing.JLabel[,0,0,663x16,alignmentX=0.0,alignmentY=0.0,border=,flags=25165832,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,horizontalAlignment=LEADING,horizontalTextPosition=TRAILING,iconTextGap=4,labelFor=,text=Focus now!,verticalAlignment=CENTER,verticalTextPosition=CENTER]
    java.awt.event.MouseEvent[MOUSE_EXITED,(220,19),absolute(872,423),button=0,clickCount=0] on javax.swing.JLabel[,0,0,663x16,alignmentX=0.0,alignmentY=0.0,border=,flags=25165832,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,horizontalAlignment=LEADING,horizontalTextPosition=TRAILING,iconTextGap=4,labelFor=,text=Focus now!,verticalAlignment=CENTER,verticalTextPosition=CENTER]
    

提交回复
热议问题