2011-12-16 155 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()

如果这样做没有帮助,请重新填写您的问题并提供更多信息。

相关问题