0

我在 Swing 方面遇到了一些问题。我有一个JFrameFrameMain. 里面是一个JPanelpanelChoices.

当被调用/创建时,它会用许多对象FrameMain填充对象,其中包含许多s (它是我编写的不同类)。panelChoicesPanelEntriesJPanelJButton

我想要做的是,当我单击PanelEntries对象内的一个按钮时,我想销毁/删除FrameMain它,以及它的其余组件(包括包含 的PanelEntries对象JButton)。

我试过使用super,但它返回的JPanelPanelEntries对象)持有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. 我需要知道如何从对象FrameMain内的按钮PanelEntries进行处理,而不仅仅是处理 JFrame。
4

1 回答 1

2

根据给定的信息,

  1. 如果您想退出应用程序,这没什么大不了的System.exit(0);:)

  2. 如果您要处理框架,jframe.dispose();

  3. 如果你想删除一个组件/你可以使用的所有组件.remove(Component)/.removeAll()等等

如果这没有帮助,请用更多信息重新编写您的问题。

于 2011-12-16T07:15:06.073 回答