关于java:JFrame lock在主表单前面的登录表单

JFrame lock Log In Form in front of Main Form

我正在编写一个应用程序,我想将"登录表单"设置为"主表单"的顶部,并且始终位于"主表单"的顶部。我的意思是将其锁定在"主表单"之前,直到"登录表单"关闭,以便用户可以在"登录表单"关闭后访问"主表单"。


您可以使用JDialog而不是JFrame来制作模态框架。以下两个链接可以帮助您入门。

  • 如何在对话框中使用模态
  • Java SE 6中的新模式API


这遵循@Jabir概述的一般思想,但是使用了JOptionPane(使用模式对话框作为显示组件),因为它具有许多便捷的方法,这些方法可以减轻我们在其他情况下需要实现的负担一个普通的模态对话框。

另一方面,对话框比选项窗格更通用。一旦我们开始思考"那太好了,但是我想将其更改为..",那就是直接使用JDialog的时候。除了现有API提供的(许多)选项之外,很难更改选项窗格。

有关使用选项窗格的更多详细信息,请参见如何制作对话框。

Login

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import java.awt.*;
import javax.swing.*;

class LoginForm {

    private JPanel loginForm = new JPanel(new BorderLayout(5,5));
    private JTextField userID = new JTextField(12);
    private JPasswordField password = new JPasswordField(8);

    LoginForm() {
        initializeLoginForm();
    }

    /**
     * Displays the log-in form inside a confirmation option pane.
     * The result (OK/Cancel) of the option pane is returned for inspection.
     */

    public final int displayLoginForm(Component parent) {
        return JOptionPane.showConfirmDialog(
                parent,
                loginForm,
               "Log In",
                JOptionPane.OK_CANCEL_OPTION,
                JOptionPane.QUESTION_MESSAGE);
    }

    private final void initializeLoginForm() {
        JPanel labels = new JPanel(new GridLayout(0,1,5,5));
        loginForm.add(labels, BorderLayout.LINE_START);
        JPanel fields = new JPanel(new GridLayout(0,1,5,5));
        loginForm.add(fields);

        labels.add(new JLabel("User ID:", SwingConstants.TRAILING));
        labels.add(new JLabel("Password:", SwingConstants.TRAILING));

        JPanel userIdConstrain = new JPanel(
                new FlowLayout(FlowLayout.LEADING));
        userIdConstrain.add(userID);
        fields.add(userIdConstrain);

        JPanel passwordConstrain = new JPanel(
                new FlowLayout(FlowLayout.LEADING));
        passwordConstrain.add(password);
        fields.add(passwordConstrain);
    }

    public final String getUserID() {
        return userID.getText();
    }

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

    public static void main(String[] args) {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                JLabel l = new JLabel("<html>"
                        +"<body style='width: 300px; height: 175px;>",
                        SwingConstants.CENTER);
                JFrame f = new JFrame("Secure Application");
                f.add(l);
                // Ensures JVM closes after frame(s) closed and
                // all non-daemon threads are finished
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                // See http://stackoverflow.com/a/7143398/418556 for demo.
                f.setLocationByPlatform(true);

                // ensures the frame is the minimum size it needs to be
                // in order display the components within it
                f.pack();
                // ensures the minimum size is enforced.
                f.setMinimumSize(f.getSize());
                f.setVisible(true);

                LoginForm lif = new LoginForm();
                int result = lif.displayLoginForm(f);
                // do the approrpaite action for this result
                if (result==JOptionPane.OK_OPTION) {
                    l.setText("Welcome" + lif.getUserID());
                } else {
                    l.setText("This application requires authentication!");
                }
            }
        };
        // Swing GUIs should be created and updated on the EDT
        // http://docs.oracle.com/javase/tutorial/uiswing/concurrency
        SwingUtilities.invokeLater(r);
    }
}