2014-12-03 39 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,從焦點子系統的文檔)來跟蹤從控制焦點每個面板內,然後將該信息發送給我的應用程序。

有沒有更好的方法來做到這一點,我不知道它? 非常感謝。

回答

相關問題