当然是具体组件,例如
import java.awt.event.*;
import javax.swing.*;public class FocusTest 
{
static JFrame frame = new JFrame();
static JButton bt1 = new JButton("Button1");
static JButton bt2 = new JButton("Button2");
static JButton bt3 = new JButton("Button3");

public static void main(String[] args) 
{
JPanel p = new JPanel();
p.add(bt1);
p.add(bt2);
p.add(bt3);
frame.getContentPane().add(p);
FocusAdapter fa = new MyFocusAdapter();
bt1.addFocusListener(fa);
bt2.addFocusListener(fa);
bt3.addFocusListener(fa);
frame.setBounds(100,100,400,400);
frame.show();
}
static class MyFocusAdapter extends FocusAdapter
{
public void focusLost(FocusEvent e)
{
if(e.getSource()==bt1)
JOptionPane.showMessageDialog(null, "Button1 Focus Lost");
else if(e.getSource()==bt2)
JOptionPane.showMessageDialog(null, "Button2 Focus Lost");
else
JOptionPane.showMessageDialog(null, "Button3 Focus Lost");
}
};}

解决方案 »

  1.   

    是不是所有继承自java.awt.Component的类都可以获得焦点,我给一个JInternalFrame对象添加了FocusAdaptor,但是点击这个对象时并没有相应FocusAdaptor中的FocusGained函数,请问这是为什么?
    java.lang.Object
      |
      +--java.awt.Component
            |
            +--java.awt.Container
                  |
                  +--javax.swing.JComponent
                        |
                        +--javax.swing.JInternalFrame
      

  2.   

    你可能是拼错了吧focusGained,不是FocusGained,这样,框架就把你的FocusGained当成一个普通的函数了,而用FocusAdapter中的一个空的focusGained来处理相应。
    看看我给你的例子吧,其中focusGained中的探出对话框被注释了,因为那样会不停的把焦点在对话框和JInternalFrame之间切换。
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    class Test extends JFrame
    {
    private JDesktopPane desk = new JDesktopPane();
    private JButton bt1 = new JButton("Button1");
    private JButton bt2 = new JButton("Button2");
    private JButton bt3 = new JButton("Button3");
    private FocusAdapter focusListener = new MyFocusAdapter();
    Test()
    {
    setContentPane(desk);
    JInternalFrame iframe = new JInternalFrame();
    desk.add(iframe);
    JPanel p = new JPanel();
    p.add(bt1);
    p.add(bt2);
    p.add(bt3);
    iframe.getContentPane().add(p);
    bt1.addFocusListener(focusListener);
    bt2.addFocusListener(focusListener);
    bt3.addFocusListener(focusListener);
    iframe.reshape(10, 10, 300, 300);
    iframe.show();
    setBounds(100,100,400,400);
    show();
    }
    public static void main(String[] args) 
    {
    Test t = new Test();
    }
    class MyFocusAdapter extends FocusAdapter
    {
    public void focusGained(FocusEvent e)
    {
    //If this is allowed, you will get this diaog always
    //JOptionPane.showMessageDialog(null, "Gained");
    System.out.println("focusGained");
    }
    public void focusLost(FocusEvent e)
    {
    JOptionPane.showMessageDialog(null, "Lost");
    System.out.println("focusLost");
    } };
    }
      

  3.   

    我在程序里没有拼错,是public void focusGained(FocusEvent e),我想问的是JInternalFrame对象是否也能获得焦点?是否所有的component都能获得焦点?
      

  4.   

    好像是不能,可是你为什么要用FocusListener?位是么不用InternalFrameListener?看看下面的代码,也可以呀,而且比FocusListener功能还多。
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    class Test extends JFrame
    {
    private JDesktopPane desk = new JDesktopPane();
    private InternalFrameListener frameListener = new MyFrameListener();
    JInternalFrame iframe1 = new JInternalFrame();
    JInternalFrame iframe2 = new JInternalFrame();
    Test()
    {
    setContentPane(desk);
    desk.add(iframe1);
    desk.add(iframe2);
    iframe1.addInternalFrameListener(frameListener);
    iframe2.addInternalFrameListener(frameListener);
    iframe1.reshape(10, 10, 300, 300);
    iframe2.reshape(40, 40, 300, 300);
    iframe1.show();
    iframe2.show();
    setBounds(100,100,400,400);
    show();
    }
    public static void main(String[] args) 
    {
    Test t = new Test();
    }
    class MyFrameListener implements InternalFrameListener
    {
    public void internalFrameActivated(InternalFrameEvent e) 
    {System.out.println("Activated");}
    public void internalFrameClosed(InternalFrameEvent e) 
    {System.out.println("Closed");}
    public void internalFrameClosing(InternalFrameEvent e) 
    {System.out.println("Closing");}
    public void internalFrameDeactivated(InternalFrameEvent e) 
    {System.out.println("Deactivated");}
    public void internalFrameDeiconified(InternalFrameEvent e) 
    {System.out.println("Deiconified");}
    public void internalFrameIconified(InternalFrameEvent e) 
    {System.out.println("Iconified");}
    public void internalFrameOpened(InternalFrameEvent e) 
    {System.out.println("Opened");}
    };
    }