2011-12-16 156 views
0

我在擺動方面有點問題。我有一個叫做FrameMainJFrame。它內部是一個JPanel,被稱爲panelChoices控制另一個班級的另一個班級

FrameMain被調用/創建的,它填充了一個數PanelEntries對象,這是一種JPanel與一些在它JButton S(它是一個不同的類,我寫)的panelChoices對象。

我想要做的是,當我點擊PanelEntries對象的內部,我要摧毀/刪除FrameMain,它的部件(包括包含JButtonPanelEntries對象)的其餘部分一起的一個按鈕。

我一直在使用super嘗試,但它返回的JPanel(在PanelEntries對象),它保存JButton而不是FrameMain持有它們放在一起。我怎樣才能做到這一點?

編輯:看來我還不夠清楚,所以這裏有更多的信息來自我的工作。我現在沒有實際的代碼,因爲我在不同的機器上,但我希望這將有助於詳細說明我的問題。

public class FrameMain() { 
    private JFrame frameMain; 
    private JPanel panelChoices; 

    public FrameMain(args) { 
     createGUI(); 
     loadData(); 
    } 

    private void createGUI() { 
     JFrame frameMain = new JFrame(); 
     JPanel panelChoices = new JPanel(new GridLayout(1,1)); 
     frameMain.add(panel); 
     // removed formatting and other design codes since they are not important. 
     pack(); 
    } 

    private void loadData() { 
     boolean available; 
     for (int i = 1; i <= 10; i++) { 
      // do some if/else and give value to boolean available 
      PanelEntries panel = new PanelEntries(i, available); 
      frameMain.add(panel); 
      // more code here to handle data. 
     } 
    } 
} 

public class PanelEntries() extends JPanel { 

    public PanelEntries(int num, boolean avb) { 
     JButton button = new JButton("Button Number " + num); 
     button.setEnabled(avb); 
     add(button); 
     // add action listener to created button so that it calls 'nextScreen()' when clicked. 
     // more code 
     pack(); 
    } 

    private void nextScreen() { 
     // destroy/dispose MainFrame here. 
     // See Notes. 
     AnotherFrame anotherFrame = new AnotherFrame(); 
    } 
} 

  1. 所有課程都是自己.java文件中。
  2. 我需要知道的如何處理PanelEntries對象中的按鈕,而不僅僅配置JFrame的FrameMain
+1

您可以提供一段代碼摘錄。我認爲這會比文字牆更容易理解 – fyr 2011-12-16 07:06:15

回答

2

對於每個給出的信息,

  1. 如果你想退出應用程序,它沒什麼大不了的使用System.exit(0); :)

  2. 如果你的意思是處置框架,jframe.dispose();

  3. 如果你想刪除一個組件/你可以使用所有組件.remove(Component)/.removeAll()

如果這樣做沒有幫助,請重新填寫您的問題並提供更多信息。

相關問題