带有多个窗口的 Java 程序(JPanel),如何将它们连接到 JFrame

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16637345/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 23:32:01  来源:igfitidea点击:

Java program with multiple windows(JPanel), how to connect them to JFrame

javanetbeansjframejpanel

提问by LogofaT

I have a library application that permits users to log in, and many other things.

我有一个允许用户登录的图书馆应用程序,以及许多其他功能。

I don't really know how to make multiple windows (views), such as a login, and if it is succesfull, closing the current window and opening the other user interface.

我真的不知道如何制作多个窗口(视图),例如登录,如果成功,关闭当前窗口并打开其他用户界面。

I hope that I have been clear.

我希望我已经清楚了。

回答by afsantos

Updated answer: Suppose your three JPanelextensions are called MyLoginPanel, MyWelcomePanel, and MyFormPanel. I'm assuming the form you mention would be the mainclass, among these, instead of the actual components that make a form.

更新的答案:假设你的三个JPanel扩展叫MyLoginPanelMyWelcomePanelMyFormPanel。我假设您提到的表单将是其中的类,而不是构成表单的实际组件。

Each of these panels should take care of its own components.

这些面板中的每一个都应该处理自己的组件。

Looking at the code below, you have only to find-and-replace my names with the ones you're using.

查看下面的代码,您只需查找并用您正在使用的名称替换我的名称。

Suggestion: Make your mainclass implement some interface, to listen to changes on the three distinct panels. E.g.: The user clicked on some button that should make the application progress to the next panel. That button has to inform someone. That someone would be the implementer of the interface.

建议:让你的类实现一些接口,以监听三个不同面板上的变化。例如:用户点击了一些按钮,应该让应用程序进入下一个面板。该按钮必须通知某人。有人将成为接口的实现者。



enum AppState {
    LOGIN, WELCOME, FORM
}


interface ProgressListener {
    void progressFrom(AppState currentState);
}


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MyForm implements Runnable, ProgressListener {
    // instance variables
    private JFrame frame;
    private MyLoginPanel loginPanel;
    private MyWelcomePanel welcomePanel;
    private MyFormPanel formPanel;

    /** Empty constructor of objects of class SomeClassUI. */
    public SomeClassUI() {
        // ...
    }

    /** Interface initialization. */
    @Override
    public void run() {
        // These should handle their own component initialization.
        // They should, at least, receive a reference to the listener.

        loginPanel = new MyLoginPanel(this);
        welcomePanel = new MyWelcomePanel(this);
        formPanel = new MyFormPanel(this);

        frame = new JFrame("Testing");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setContentPane(loginPanel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    /** */
    @Override
    public void progressFrom(AppState whoTriggeredIt) {
        switch (whoTriggeredIt) {
            case LOGIN:
            frame.setContentPane(welcomePanel);
            frame.pack();
            return;

            case WELCOME:
            frame.setContentPane(formPanel);
            frame.pack();
            return;

            default: return;
        }
    }


    /** */
    public void go() {
        SwingUtilities.invokeLater(this);
    }


    /** */
    public static void main(String[] args) {
        new SomeClassUI().go();
    }
}


class MyLoginPanel extends JPanel implements ActionListener {
    private final ProgressListener listener;

    public MyLoginPanel(ProgressListener listener) {
        // Components, etc.
        this.listener = listener;
    }

        ...

    /** */
    @Override
    public void actionPerformed(ActionEvent e) {
        // Validate login, or whatever.
        // All went well, notify listener to progress.
        this.listener.progressFrom(AppState.LOGIN);
    }
}

Instead of adding / removing elements directly from the JFrame, you may want to do these things on some content panelof your own, which you store as an instance variable (see JFrame.setContentPane()).

JFrame您可能希望在自己的某些内容面板上执行这些操作,而不是直接从 中添加/删除元素,并将其存储为实例变量(请参阅 参考资料JFrame.setContentPane())。

If you want all the panels to be easily accessible at the same time, instead of this proceduralstep-by-step change, you may want to look into the CardLayout, as previously suggested in the comments.

如果你想所有的面板是方便在同一时间,而不是此,程序一步一步的变化,你可能想看看的CardLayout,因为以前在意见提出。

The suggestions I give are just general guidelines, as I'm not aware of the details of your needs.

我给出的建议只是一般准则,因为我不知道您的需求的细节。