JLabel がフォーカスを取得できません
確かにそれはできます。
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);
}
});
}
}
アップデート
これで、質問をより注意深く読みました。これは、ラベルでフォーカス、マウス、およびキー入力イベントを検出する例です。
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);
}
});
}
}
出力例
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]