登录并注销 [英] login and logout in swing
问题描述
我已经制作了一个包含用户名和密码的表单...并提交了按钮
所以点击提交后...我必须在单独的程序中进行验证部分,在获得结果为true时将允许用户导航到内部主页...添加禁用登录页面。如果登录详细信息错误,它将显示消息对话框..显示错误的用户名和密码..
I have made a form containing username and password...and submit button So after clicking on submit...I have to do validation part in separate program, Which on getting result as true will allow user to navigate to inner homepage...add disable login page.And if login details are wrong it will show message dialog..showing wrong username and password..
public void actionPerformed(ActionEvent ae)
{
if (ae.getSource() == submit)
{
//to do...
}
}
推荐答案
不知道如何验证用户详细信息,可以肯定地说你不想在EDT的上下文中调用它。
Not knowing how the user details are validated, its safe to say that you won't want to be calling it within the context of the EDT.
相反,我使用过 SwingWorker
为我做的工作(你必须填写)。它只是将 true
或 false
值返回给EDT。
Instead, I've used a SwingWorker
to do the work for me (you'll have to fill that in). It simply returns a true
or false
value back to the EDT.
如果登录失败,我们只需显示 JOptionPane
显示登录失败消息。否则我们可以简单地处理登录对话框......
If the login in failed, we simply show a JOptionPane
showing a "Failed login" message. Otherwise we can simply dispose of the login dialog...
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();
}
}
}
}
}
这篇关于登录并注销的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!