package com.file;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class DoWindow extends JFrame {
private
static final long serialVersionUID = 158951615131406887L;
private
JLabel userName = new JLabel("用户名:");
private
JTextField userNameField = new JTextField(10);
private
JLabel password = new JLabel("密码:");
private
JPasswordField passwordField = new JPasswordField(10);
private
JButton loginButton = new JButton("登录");
private
JButton register = new JButton("注册");
private
JPanel panel = new JPanel();
public
DoWindow(String name, Point p) {
super(name);
setLocation(p);
GridBagLayout layout = new GridBagLayout();
GridBagConstraints constraints = new GridBagConstraints();
panel.setLayout(layout);
constraints.fill = GridBagConstraints.BOTH;
constraints.weightx = 1.0;
layout.setConstraints(userName, constraints);
constraints.gridwidth = GridBagConstraints.REMAINDER;
layout.setConstraints(userNameField, constraints);
constraints.gridwidth = GridBagConstraints.RELATIVE;
layout.setConstraints(password, constraints);
constraints.gridwidth = GridBagConstraints.REMAINDER;
layout.setConstraints(passwordField, constraints);
panel.add(userName);
panel.add(userNameField);
panel.add(password);
panel.add(passwordField);
panel.add(loginButton);
panel.add(register);
loginButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String username = userNameField.getText();
char[] passwordStrAry = passwordField.getPassword();
String password = passwordStrAry != null ? new String(
passwordStrAry) : null;
if (username == null || password == null ||
"".equals(username)
|| "".equals(password)) {
JOptionPane.showMessageDialog(null, "密码和用户名不能为空", "注意",
JOptionPane.ERROR_MESSAGE);
if (username == null || "".equals(username))
userNameField.grabFocus();
else
passwordField.grabFocus();
return;
}
boolean checkResult = UserManager.loginCheck(username,
password);
if (!checkResult) {
JOptionPane.showMessageDialog(null, "密码或用户名不正确", "注意",
JOptionPane.ERROR_MESSAGE);
} else {
new MessageFrame("登录成功,欢迎光临!", getLocation());
dispose();
}
}
});
register.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new RegisterFrame("注册界面", getLocation());
dispose();
}
});
setLayout(new FlowLayout());
add(panel);
setSize(250, 130);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}