Studnet.java
package studentManage;
/*
* 初始化界面,使主界面位于屏幕中间,且用户不能改变大小
*/
import javax.swing.UIManager;
import java.awt.*;
//import screen
public class Student {
boolean packFrame = false;
// JFrame frame=new JFrame();
public Student() {
Mainframe frame = new Mainframe();
if (packFrame) {
frame.pack();//调整此窗口的大小,以适合其子组件的首选大小和布局。
} else {
frame.validate();//验证此容器及其所有子组件。
}
//设置屏幕大小
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);
}
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
new Student();
}
}
package studentManage;
import java.awt.Color;
import java.awt.Event;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
/*
*/
public class Mainframe extends JFrame implements ActionListener {
static Mainframe s;
static JPanel pan = new JPanel();
static JLabel label1 = new JLabel("用户名");
String text = label1.getText();
static JLabel label2 = new JLabel("密 码");
static JTextField textField3 = new JTextField();
static int m = (int) (Math.random() * 9000 + 1000);
static String s1 = String.valueOf(m);
static JLabel label3 = new JLabel(s1);
String num = s1;
static JTextField textField1 = new JTextField();
static JPasswordField password = new JPasswordField();
static JButton button1 = new JButton("进入系统");
static JRadioButton radioButton1 = new JRadioButton("管理员");
static JRadioButton radioButton2 = new JRadioButton("学生");
ButtonGroup buttonGroup = new ButtonGroup();
public String getA() {
return this.text;
}
public Mainframe() {
JLabel label = new JLabel();
label.setSize(400, 400);
pan.add(label);
pan.setLayout(null);
pan.setSize(400, 400);
this.setTitle("学籍管理系统");
this.setSize(400, 400);
this.setLayout(null);
label1.setBounds(100, 25, 50, 20);
textField1.setBounds(155, 24, 120, 20);
label2.setBounds(100, 62, 50, 20);
password.setBounds(155, 61, 120, 20);
textField3.setBounds(155, 100, 120, 20);
label3.setBounds(100, 100, 50, 20);
label3.setForeground(Color.RED);
label3.setFont(new java.awt.Font("ITALIC", 0, 14));
label3.setBorder(BorderFactory.createLineBorder(Color.lightGray, 1));
label3.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
int m = (int) (Math.random() * 9000 + 1000);
String s1 = String.valueOf(m);
label3.setText(s1);
num = s1;
}
});
button1.setBounds(150, 200, 101, 22);
radioButton1.setBounds(105, 140, 80, 20);
radioButton2.setBounds(180, 140, 60, 20);
button1.addActionListener(this);
buttonGroup.add(radioButton1);
buttonGroup.add(radioButton2);
this.add(radioButton1);
this.add(radioButton2);
this.add(label1);
this.add(label2);
this.add(textField1);
this.add(password);
this.add(label3);
this.add(textField3);
this.add(button1);
this.add(pan);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args) {
new Mainframe();
}
public void actionPerformed(ActionEvent e) {
if (radioButton1.isSelected()) {
if (textField1.getText().equals("syc")
&& password.getText().equals("123")
&& textField3.getText().equals(num)) {
MInterface b = new MInterface();
this.dispose();
} else if ("".equals(textField1.getText())
|| "".equals(password.getText())) {
JOptionPane.showMessageDialog(null, "用户名或密码为空");
} else {
JOptionPane.showMessageDialog(null, "用户名密码或验证码有误");
}
}
if (radioButton2.isSelected()) {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException ce) {
JOptionPane.showMessageDialog(s, ce.getMessage());
}
try {
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/stu", "root", "123");
Statement stmt = con.createStatement();
ResultSet rs = stmt
.executeQuery("select * from infor where stuId= '"
+ textField1.getText() + "' and password= '"
+ password.getText() + "'");
if (textField1.getText().equals("")
|| password.getText().equals("")) {
JOptionPane.showMessageDialog(this, "用户名或密码为空");
} else if (rs.next() && textField3.getText().equals(num))
{
try {
new SCx();
} catch (Exception e1) {
e1.printStackTrace();
}
}
else {
JOptionPane.showMessageDialog(this, "用户名密码或验证码有误");
}
// stmt.close();
}
catch (SQLException se) {
JOptionPane.showMessageDialog(s, se.getMessage());
}
}
}
}
从一个机器搬到另一个机器上,出现了大量乱码,为了美观,所以把相关注释都删掉了。