为什么JButton上可以加FocusListener,而JLabel上则不行?

05-12-19 rosefish
这是我加在JButton上的Focus事件:

private JButton getJButton1() {

if (jButton1 == null) {

jButton1 = new JButton();

jButton1.setSize(new java.awt.Dimension(47,25));

jButton1.setLocation(new java.awt.Point(28,30));

jButton1.addFocusListener(new java.awt.event.FocusListener() {

public void focusGained(java.awt.event.FocusEvent e) {

System.out.println("focusGained()"); }

public void focusLost(java.awt.event.FocusEvent e) {

System.out.println("lost");

}

});

}

return jButton1;

}

在控制台运行Applet,点击该Btton可以输出"focusGained()"

但是同理我把Focus加在JLabel上则没任何反映:

private JPanel getJPanel() {

if (jPanel == null) {

jPanel = new JPanel();

jPanel.setLayout(gridLayout);

String s[]={"1","2","3","4","5","6","7","8","wen","wht1"};

Image imgs[]=new Image[s.length];

for(int i=0;i<imgs.length;i++)

{

Label label=new JLabel(new MyImage("img/"+s+".jpg"));

jPanel.add(label);

label.addFocusListener(new java.awt.event.FocusListener(){

public void focusGained(java.awt.event.FocusEvent e) {

System.out.println("focusGained()");

}

public void focusLost(java.awt.event.FocusEvent e) {

System.out.println("Lost()");

}

});

}

}

return jPanel;

}

运行时点击JLabel里的图片,控制台没有输出,请问高手这是什么原因??怎样在JLabel上正确添加FocusListener呢?

还有,在上面的JButton上加Focus事件后,运行时非得点击另一个JButton才能有focusLost事件,而点击其他的控件如JScrollPane,JLabel则不会触发focusLost,这是为什么???

猜你喜欢