从不同的类控制另一个类

发布于 2024-12-21 18:17:38 字数 2185 浏览 2 评论 0原文

我在 Swing 方面遇到了一些问题。我有一个名为 FrameMainJFrame。它里面有一个名为 panelChoicesJPanel

当调用/创建 FrameMain 时,它会用许多 PanelEntries 对象填充 panelChoices 对象,这是一个 JPanel code> 中包含许多 JButton(这是我编写的另一个类)。

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

我尝试使用 super 但它返回保存 JButtonJPanelPanelEntries 对象),而不是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 对象内的按钮处置 FrameMain,而不仅仅是处置 JFrame。

I am having a bit of problem regarding Swing. I have a JFrame called FrameMain. Inside it is a JPanel called panelChoices.

When FrameMain is called/created, it fills up the panelChoices object with a number of PanelEntries objects, which is a JPanel with a number of JButtons in it (it is a different class that I wrote).

What I want to do is when I click one of the buttons inside the PanelEntries object, I want to destroy/remove FrameMain, along with the rest of it components (including the PanelEntries object that contains the JButton).

I've tried using super but it returns the JPanel (the PanelEntries object) that holds the JButton and not FrameMain that holds them all together. How can I achieve this?

EDIT: It seems that I am not clear enough, so here's a bit more information from my work. I don't have the actual code right now because I am on a different machine but I hope this will help elaborate my question.

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();
    }
}

Notes:

  1. All classes are inside their own .java file.
  2. I need to know how to dispose FrameMain from the button inside the PanelEntries object, not just disposing a JFrame.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小红帽 2024-12-28 18:17:38

根据给定的信息,

  1. 这没什么大不了的
  2. 如果你想处置框架,jframe.dispose();

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

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

As per the given information,

  1. If you want to exit the application, its not a big deal use System.exit(0); :)

  2. If you mean to dispose the frame, jframe.dispose();

  3. If you want to remove a componet / all components you can use .remove(Component) / .removeAll() etc

If this did not help, please re-write your question with more information.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文