2014-12-03 44 views
1

我有一个应用程序,它显示一个JFrame,其中有几个JPanel(A)。每个JPanel A都有一些控件,还有一些复杂的组件包含额外的控件。现在,我的应用程序需要知道正在使用的一组由一个JPanel一个包含控件之一,因此它可以突出显示的JPanel A.如何根据重点控制知道哪个顶级容器当前“专注”?

下面是一些代码为例:

public class MyApp extends JFrame 
{ 
    private JPanel A1 = new JPanel(); 
    private JPanel A2 = new JPanel(); 
    private JPanel A3 = new JPanel(); 

    public MyApp() 
    { 
     // Add components to the layout, initialize things, etc 
     ... 

     // Lets suppose each JPanel has 2 components 
     JComponent C1 = new JComponent(); 
     JComponent C2 = new JComponent(); 
     A1.add(C1); 
     A1.add(C2); 

     JComponent C3 = new JComponent(); 
     JComponent C4 = new JComponent(); 
     A2.add(C3); 
     A2.add(C4); 

     JComponent C5 = new JComponent(); 
     JComponent C6 = new JComponent(); 
     A3.add(C5); 
     A4.add(C6); 
    } 
} 

所以我想知道哪个JPanel(A1,A2或A3)目前正在使用JComponent的“聚焦”。一个重要的是我不能直接访问主框架中的所有控件,因为这些JComponent被定义为我应用程序中其他位置的类。我可以修改这些,但我不能说“如果它是C1内的textfield1,然后是A1”。

我一直在读,到目前为止,我想我要做的就是实现一个PropertyChangeListener(如图所示部分Tracking Focus Changes to Multiple Components,从焦点子系统的文档)来跟踪从控制焦点每个面板内,然后将该信息发送给我的应用程序。

有没有更好的方法来做到这一点,我不知道它? 非常感谢。

回答

相关问题