学生管理系统
public class LoginFrame extends JFrame {
private String username;
private String password;
private JLabel lblUsername;
private JLabel lblPassword;
private JTextField txtUsername;
private JPasswordField txtPassword;
private JButton btnOK;
private JButton btnCancel;
private JButton btnSign;
private JPanel panel, panel1, panel2, panel3, panel4;
public LoginFrame(String title) {
super(title);
initGUI();
eventsHandling();
}
private void eventsHandling() {
btnOK.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
username = txtUsername.getText().trim();
password = new String(txtPassword.getPassword());
UserService userService = new UserService();
User user = userService.login(username, password);
if (user != null) {
try {
new Main();
JOptionPane.showMessageDialog(null, "恭喜【"+ username + "】登录成功~");
} catch (IOException ioException) {
System.err.println("失败~");
}
dispose();
} else {
JOptionPane.showMessageDialog(null, "【"+ username + "】登录失败~\n用户名或密码错误~");
txtUsername.setText("");
txtPassword.setText("");
txtUsername.requestFocus();
}
}
});
btnCancel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
btnSign.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new Sign();
}
});
}
private void initGUI() {
panel = (JPanel) getContentPane();
panel1 = new JPanel();
panel2 = new JPanel();
panel3 = new JPanel();
panel4 = new JPanel();
lblUsername = new JLabel("用户名:");
lblPassword = new JLabel("密 码:");
txtUsername = new JTextField(15);
txtPassword = new JPasswordField(15);
btnOK = new JButton("确定[O]");
btnCancel = new JButton("取消[C]");
btnSign = new JButton("注册[I]");
panel1.add(lblUsername);
panel1.add(txtUsername);
panel2.add(lblPassword);
panel2.add(txtPassword);
panel3.add(btnOK);
panel3.add(btnCancel);
panel4.add(btnSign);
panel.setLayout(new GridLayout(4, 1));
panel.add(panel1);
panel.add(panel2);
panel.add(panel3);
panel.add(panel4);
btnOK.setMnemonic(KeyEvent.VK_O);
btnCancel.setMnemonic(KeyEvent.VK_C);
btnSign.setMnemonic(KeyEvent.VK_I);
txtPassword.setEchoChar('*');
setSize(700, 500);
setLocationRelativeTo(null);
setResizable(false);
pack();
setVisible(true);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
public static void main(String[] args) {
new LoginFrame("用户登录");
}
}
public class Main extends JFrame implements ActionListener{
public static void main(String[] args) throws IOException {
new Main();
final int WIDTH=800;
final int HEIGHT=500;
}
private JLabel queryLab = null;
private JTextField queryTxt = null;
private JButton queryBtn = null;
private JButton allBtn = null;
private JTable resultTb = null;
private JScrollPane jsp = null;
private JButton addBtn = null;
private JButton deleteBtn = null;
private JButton updateBtn = null;
private JPanel top = null;
private JPanel bottom = null;
private Model sm = null;
public Main() throws IOException {
super("学生管理系统");
queryLab = new JLabel("请输入姓名:");
queryTxt = new JTextField(10);
queryBtn = new JButton("查询");
allBtn = new JButton("全部");
queryBtn.addActionListener(this);
queryBtn.setActionCommand("query");
allBtn.addActionListener(this);
allBtn.setActionCommand("all");
addBtn = new JButton("添加");
deleteBtn = new JButton("删除");
updateBtn = new JButton("修改");
addBtn.addActionListener(this);
addBtn.setActionCommand("add");
deleteBtn.addActionListener(this);
deleteBtn.setActionCommand("delete");
updateBtn.addActionListener(this);
updateBtn.setActionCommand("update");
top = new JPanel();
top.add(queryLab);
top.add(queryTxt);
top.add(queryBtn);
top.add(allBtn);
bottom = new JPanel();
bottom.add(addBtn);
bottom.add(deleteBtn);
bottom.add(updateBtn);
sm = new Model();
String sql = "select * from stu";
sm.queryStu(sql, null);
resultTb = new JTable(sm);
jsp = new JScrollPane(resultTb);
this.add(top,BorderLayout.NORTH);
this.add(jsp,BorderLayout.CENTER);
this.add(bottom,BorderLayout.SOUTH);
this.setBounds(500,250,500,300);
this.setVisible(true);
this.setResizable(false);
}
@Override
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("query")) {
String name = queryTxt.getText().trim();
if(name.length() != 0) {
String sql = "select * from stu where stuName=?";
String []paras = {name};
jtableUpdate(sql, paras);
} else {
JOptionPane.showMessageDialog(this, "姓名输入不能为空");
}
} else if(e.getActionCommand().equals("add")) {
new Add(this, "添加学生信息", true);
String sql = "select * from stu";
jtableUpdate(sql, null);
} else if(e.getActionCommand().equals("all")) {
String sql = "select * from stu";
jtableUpdate(sql, null);
} else if(e.getActionCommand().equals("delete")) {
int rowNum = this.resultTb.getSelectedRow();
if(rowNum == -1) {
JOptionPane.showMessageDialog(this, "请选择一行");
return ;
}
String stuId = (String)sm.getValueAt(rowNum, 0);
String sql = "delete from stu where stuId=?";
String []paras = {stuId};
Model tmp = new Model();
tmp.cudStu(sql, paras);
sql = "select * from stu";
jtableUpdate(sql, null);
} else if(e.getActionCommand().equals("update")) {
int rowNum = this.resultTb.getSelectedRow();
if(rowNum == -1) {
JOptionPane.showMessageDialog(this, "请选择一行");
return ;
}
new Set(this, "修改学生信息", true, sm, rowNum);
String sql = "select * from stu";
jtableUpdate(sql, null);
}
}
public void jtableUpdate(String sql, String[] paras)
{
sm = new Model();
sm.queryStu(sql, paras);
resultTb.setModel(sm);
}
}