Java GUI 打开一个新的 JFrame

Java GUI Opening a new JFrame

我在网上浏览过如何从现有的 JFrame 打开一个新的 JFrame。我发现显然最好的方法是处理现有的 JFrame 并打开新的 JFrame - 但这是一个问题。

我有一个登录表单,一个用户登录,登录框架被处理,主框架被设置为可见。

import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class client {

    public static void main(String[] args) {
        initialize();
    }

    private static void initialize() {
        JFrame loginFrame = new JFrame("Login");
        loginFrame.setBounds(100, 100, 300, 300);
        loginFrame.setResizable(false);
        loginFrame.setLocationRelativeTo(null);
        loginFrame.setDefaultCloseOperation(loginFrame.HIDE_ON_CLOSE);
        loginFrame.getContentPane().setLayout(null);
        JFrame mainFrame = new JFrame("Main");
        mainFrame.setBounds(100, 100, 300, 197);
        mainFrame.setResizable(false);
        mainFrame.setLocationRelativeTo(null);
        mainFrame.setDefaultCloseOperation(mainFrame.EXIT_ON_CLOSE);
        mainFrame.getContentPane().setLayout(null);
        JButton loginButton = new JButton("Login");
        loginButton.setBounds(102, 133, 89, 23);
        loginButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                loginButton.setEnabled(false);
                loginFrame.dispose();
                mainFrame.setVisible(true);
            }
        });
        loginFrame.getContentPane().add(loginButton);
        loginFrame.setVisible(true);
    }
}

但是,如果用户启动客户端,然后决定不登录并关闭它,该进程仍然 运行 在后台?

我觉得这是一个非常愚蠢的问题,如果是的话我很抱歉,但我环顾四周并找不到任何解决方法。我可以不处理登录框并隐藏它并将它们都设置为 EXIT_ON_CLOSE 吗?

提前致谢!

因此,主要问题是,您有两个框架,虽然不可见,但都已 "realised",这意味着在处理完所有应用程序 windows 之前,事件调度线程获胜't exit,表示JVM不会退出。

因此,我建议稍微改变方法。与其以这种方式使用两个框架,登录 "window" 应该基于模态对话框,并且在您需要之前不应创建应用程序框架。

模态对话框会在代码可见时停止代码的执行,不会阻塞事件调度线程(这是黑魔法),这意味着您可以使用它循环来继续提示用户获取凭据,直到他们通过身份验证或 close/cancel 对话框。

我也强烈建议使用 JPanels 作为基础组件,允许基于 window 的 类 只是容器,这隔离了责任,分离了代码和整体上提供更可重用的解决方案。

您可以查看 How to Make Dialogs 了解更多详情

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                LoginPane loginPane = new LoginPane();
                boolean authenticated = false;
                boolean exit = false;
                do {
                    int option = JOptionPane.showOptionDialog(null,
                                    loginPane,
                                    "login",
                                    JOptionPane.OK_CANCEL_OPTION,
                                    JOptionPane.PLAIN_MESSAGE,
                                    null,
                                    new Object[]{"Login", "Cancel"},
                                    "Login");
                    if (option == 0) {
                        // Authenticate
                        authenticated = true;
                    } else if (option == JOptionPane.CLOSED_OPTION || option == 1) {
                        exit = true;
                    }
                } while (!authenticated && !exit);
                if (authenticated) {
                    JFrame frame = new JFrame();
                    frame.add(new MainPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            }
        });
    }

    public class LoginPane extends JPanel {

        private JTextField userName;
        private JPasswordField password;

        public LoginPane() {
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;

            add(new JLabel("User name: "), gbc);
            gbc.gridy++;
            add(new JLabel("Password: "), gbc);

            gbc.gridx++;
            gbc.gridy = 0;

            userName = new JTextField(10);
            password = new JPasswordField(10);

            add(userName, gbc);
            gbc.gridy++;
            add(password, gbc);
        }

        public String getName() {
            return userName.getText();
        }

        public char[] getPassword() {
            return password.getPassword();
        }

    }

    public class MainPane extends JPanel {

        public MainPane() {
            setBorder(new EmptyBorder(50, 50, 50, 50));
            add(new JLabel("Super awesome app"));
        }

    }

}

我也鼓励 DISPOSE_ON_CLOSE 而不是 HIDE_ON_CLOSE,它将释放本机对等点并从应用程序 window 缓存

中删除 window

现在,如果您真的想要一个更具挑战性的方法,您可以看看 Java and GUI - Where do ActionListeners belong according to MVC pattern?,它提供了一个基于 MVC 的登录实现