📄 loginframe.java
字号:
package exam.gui;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
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;
import exam.dao.StudentFromCfg;
import exam.model.Student;
/**
* 登录界面
*
* @author 刘东
*
*/
public class LoginFrame implements ActionListener {
private JFrame frame;
private JButton login, cancel;
private JTextField id;
private JPasswordField pass;
private JLabel titleL, idL, passL;
private JPanel ps, pc, pn, pc1, pc2;
public LoginFrame() {
frame = new JFrame("考生登陆");
login = new JButton("登陆");
cancel = new JButton("取消");
id = new JTextField(15);
pass = new JPasswordField(15);
titleL = new JLabel("考生登陆");
idL = new JLabel("学号:");
passL = new JLabel("密码:");
pn = new JPanel();
pc = new JPanel();
ps = new JPanel();
pc1 = new JPanel();
pc2 = new JPanel();
init();
}
private void init() {
pn.add(titleL);
pc1.add(idL);
pc1.add(id);
pc2.add(passL);
pc2.add(pass);
pass.addActionListener(this);
pc.setLayout(new GridLayout(2, 1));
pc.add(pc1);
pc.add(pc2);
ps.add(login);
login.addActionListener(this);
ps.add(cancel);
cancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
frame.dispose();
}
});
frame.add(pn, BorderLayout.NORTH);
frame.add(pc, BorderLayout.CENTER);
frame.add(ps, BorderLayout.SOUTH);
frame.setSize(240, 180);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setResizable(false);
setThisLocation();
}
public void actionPerformed(ActionEvent arg0) {
StudentFromCfg sfc = new StudentFromCfg();
String inputId = id.getText().trim();
String inputPassword = String.valueOf(pass.getPassword());
if (inputId.equals("")) {
JOptionPane.showMessageDialog(frame, "学号不能为空!", "操作提示",
JOptionPane.WARNING_MESSAGE);
} else if (inputPassword.equals("")) {
JOptionPane.showMessageDialog(frame, "密码不能为空!", "操作提示",
JOptionPane.WARNING_MESSAGE);
} else if (isDigit(inputId)) {
Student s = sfc
.getStudent(Integer.parseInt(inputId), inputPassword);
if (s == null) {
JOptionPane.showMessageDialog(frame, "学号或密码错误!", "操作提示",
JOptionPane.WARNING_MESSAGE);
int selection = 0;
if (selection == JOptionPane.OK_OPTION) {
pass.setText("");
}
} else {
frame.dispose();
new SelectSubjectFrame(s);
}
} else {
JOptionPane.showMessageDialog(frame, "学号格式错误!", "操作提示",
JOptionPane.WARNING_MESSAGE);
}
}
private boolean isDigit(String s) {
char[] c = s.toCharArray();
for (int i = 0; i < c.length; i++) {
if (Character.isDigit(c[i]))
continue;
else
return false;
}
return true;
}
private void setThisLocation() {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
frame.setLocation((screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) / 2);
frame.setVisible(true);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -