-2

我已经制作了一个包含用户名和密码的表单......并提交按钮所以在点击提交后......我必须在单独的程序中进行验证部分,将结果设为 true 将允许用户导航到内部主页......添加禁用登录页面。如果登录详细信息错误,它将显示消息对话框..显示错误的用户名和密码..

public void actionPerformed(ActionEvent ae)
{
    if (ae.getSource() == submit) 
    {
        //to do...
    }
}
4

1 回答 1

2

不知道如何验证用户详细信息,可以肯定地说您不想在 EDT 的上下文中调用它。

相反,我使用 aSwingWorker为我完成工作(您必须填写)。它只是将一个truefalse值返回给 EDT。

如果登录失败,我们只会显示JOptionPane“登录失败”消息。否则我们可以简单地处理登录对话框......

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.ExecutionException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestLogin {

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

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

                JDialog frame = new JDialog((Frame) null, "Login", true);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);

                System.exit(0);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.insets = new Insets(4, 4, 4, 4);
            gbc.anchor = GridBagConstraints.WEST;
            add(new JLabel("User Name:"), gbc);
            gbc.gridy++;
            add(new JLabel("Password:"), gbc);
            gbc.gridx++;
            gbc.gridy = 0;
            add(new JTextField(10), gbc);
            gbc.gridy++;
            add(new JPasswordField(10), gbc);

            JButton okay = new JButton("Okay");
            JButton cancel = new JButton("Cancel");

            gbc.gridx = 0;
            gbc.gridy++;
            gbc.anchor = GridBagConstraints.CENTER;
            add(okay, gbc);
            gbc.gridx++;
            add(cancel, gbc);

            okay.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    setFormEnabled(false);
                    new LoginWorker().execute();
                }
            });

            cancel.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    SwingUtilities.windowForComponent((Component) e.getSource()).dispose();
                }
            });
        }

        protected void setFormEnabled(boolean enabled) {
            for (Component comp : getComponents()) {
                comp.setEnabled(enabled);
            }
        }

        protected void loginSuccessful() {
            SwingUtilities.windowForComponent(this).dispose();
        }

        protected void loginFailed() {
            JOptionPane.showMessageDialog(this, "Login failed", "Fail", JOptionPane.ERROR_MESSAGE);
            setFormEnabled(true);
        }

        public class LoginWorker extends SwingWorker<Boolean, Void> {

            @Override
            protected Boolean doInBackground() throws Exception {
                boolean login = false;
                Thread.sleep(5000);
                login = (int) Math.round(Math.random() * 1) == 0 ? false : true;
                return login;
            }

            @Override
            protected void done() {
                try {
                    Boolean login = get();
                    if (login) {
                        loginSuccessful();
                    } else {
                        loginFailed();
                    }
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                    loginFailed();
                } catch (ExecutionException ex) {
                    ex.printStackTrace();
                    loginFailed();
                }
            }
        }
    }
}
于 2013-04-10T07:26:29.920 回答