1

我目前有两个框架,当您运行应用程序时,显示的第一个 JFrame 是登录,它有两个输入字段和一个按钮。当用户登录并通过验证时,我想关闭框架并启动第二个框架。

所以,我唯一能想到的就是setVisible(false)为登录框架和setVisible(true)框架做Main

有没有更好的方法来做到这一点,或者这是唯一的方法?

4

2 回答 2

2

就个人而言,我会立即启动您的第二个JFrame框架,并JDialogJFrame.

另请参阅The Use of Multiple JFrames, Good/Bad Practice?

这是我建议的基本演示:

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestLogin {
    private JFrame frame;
    private boolean authenticated;
    private JTextField login;
    private JPasswordField password;

    protected void initUI() {
        frame = new JFrame(TestLogin.class.getSimpleName());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(600, 600);
        frame.setVisible(true);
    }

    protected void showLoginDialog() {
        authenticated = false;
        final JDialog dialog = new JDialog(frame, "Please provide your credentials");
        dialog.setModal(true);
        JPanel panel = new JPanel(new GridBagLayout());
        JPanel buttonPanel = new JPanel();
        login = new JTextField(40);
        password = new JPasswordField(20);
        JLabel loginLabel = new JLabel("Login:");
        JLabel passwordLabel = new JLabel("Password:");
        JButton ok = new JButton("OK");
        ok.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                // Here perform authentication and set authentication flag to appropriate value
                authenticated = true;
                if (authenticated) {
                    setUpFrame();
                    dialog.dispose();
                }
            }
        });
        JButton cancel = new JButton("Cancel");
        cancel.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        dialog.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosed(WindowEvent e) {
                if (!authenticated) {
                    System.exit(0);
                }
            }
        });
        dialog.getRootPane().setDefaultButton(ok);
        buttonPanel.add(ok);
        buttonPanel.add(cancel);
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(5, 5, 5, 5);
        gbc.fill = GridBagConstraints.HORIZONTAL;
        panel.add(loginLabel, gbc);
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        gbc.weightx = 1.0;
        panel.add(login, gbc);
        gbc.gridwidth = 1;
        gbc.weightx = 0;
        panel.add(passwordLabel, gbc);
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        gbc.weightx = 1.0;
        panel.add(password, gbc);
        panel.add(buttonPanel, gbc);
        dialog.add(panel);
        dialog.pack();
        dialog.setLocationRelativeTo(frame);
        while (!authenticated) {
            dialog.setVisible(true);
        }
    }

    protected void setUpFrame() {
        frame.add(new JLabel("Successfully authenticated"));
        frame.revalidate();
        frame.repaint();
    }

    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException,
            UnsupportedLookAndFeelException {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                TestLogin testLogin = new TestLogin();
                testLogin.initUI();
                testLogin.showLoginDialog();
            }

        });
    }

}
于 2013-01-18T22:54:34.370 回答
0

有几种方法可以做到这一点。
例如,您可以重复使用您的 1st JFrame。因此,删除您在第一个框架上获得的组件,为第二个框架添加组件,然后添加repaint()框架。
但我不认为这是一种好的做法。
正如 Andrew Thompson 建议的那样,您也可以使用 aCardLayout来初始化 one JFrame,显示您的登录卡,然后切换到完全初始化的第二张完整应用程序卡。这样,您将摆脱那些重绘。
您还可以显示您的第二帧(首先是您的应用程序),然后使用模式JDialog让用户登录。

于 2013-01-18T22:56:14.463 回答