123456789778
立即下载
资源介绍:
123456789778
package com;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class AddGUI extends JFrame{
//private StudentGui studentGui;
private DefaultTableModel tableModel;
public AddGUI(StudentGui studentGui) {
setResizable(false);
setSize(400, 300);
// 设置标题
setTitle("新增学生");
setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
JPanel jp1 = new JPanel();
JLabel jl1 = new JLabel("学号:");
JTextField id = new JTextField(10);
jp1.setLayout(new FlowLayout());
jp1.add(jl1);
jp1.add(id);
add(jp1);
JPanel jp2 = new JPanel();
JLabel jl2 = new JLabel("姓名:");
JTextField name = new JTextField(10);
jp2.setLayout(new FlowLayout());
jp2.add(jl2);
jp2.add(name);
add(jp2);
ButtonGroup bg = new ButtonGroup();
JPanel jp3 = new JPanel();
jp3.setLayout(new FlowLayout());
JLabel jl3 = new JLabel("性别:");
JRadioButton jrb1 = new JRadioButton("男");
JRadioButton jrb2 = new JRadioButton("女");
bg.add(jrb1);
bg.add(jrb2);
jp3.add(jl3);
jp3.add(jrb1);
jp3.add(jrb2);
add(jp3);
//年级
JPanel jp4 = new JPanel();
jp4.setLayout(new FlowLayout());
JLabel jl4 = new JLabel("年级:");
String[] jg = { "一年级", "二年级", "三年级", "四年级" ,"五年级","六年级"};
JComboBox jComboBox = new JComboBox(jg);
jp4.add(jl4);
jp4.add(jComboBox);
add(jp4);
//班级
JPanel jp5 = new JPanel();
jp4.setLayout(new FlowLayout());
JLabel jl5 = new JLabel("年级:");
String[] jg2 = { "一班", "二班", "三班"};
JComboBox jComboBox2 = new JComboBox(jg2);
jp5.add(jl5);
jp5.add(jComboBox2);
add(jp5);
//出生年月
JPanel jp6 = new JPanel();
JLabel jl6 = new JLabel("出生年月:");
JTextField batch = new JTextField(10);
jp6.setLayout(new FlowLayout());
jp6.add(jl6);
jp6.add(batch);
add(jp6);
//保存
JPanel jp7 = new JPanel();
JButton add=new JButton("保存");
jp7.add(add);
add.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String id1 = id.getText().trim();
String name1=name.getText().trim();
String sex="";
String grade=(String) jComboBox.getSelectedItem();
String classname=(String) jComboBox2.getSelectedItem();
String bath1=batch.getText().trim();
if (jrb1.isSelected()) {
sex= "男";
} else if (jrb2.isSelected()) {
sex= "女";
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
sdf.setLenient(false); // 严格解析日期
try {
Date parse = sdf.parse(bath1);
if (!id1.isEmpty()&&!name1.isEmpty()&&!sex.equals("")&&!grade.isEmpty()&&!classname.isEmpty()) {
DateBaseUtil dateBaseUtil=new DateBaseUtil();
if (dateBaseUtil.queryStudent(id1).equals("-1")){
dispose();//关闭该窗口
System.out.println(bath1);
Student student=new Student(id1,name1,sex,grade,classname,parse);
dateBaseUtil.addStudent(student);
studentGui.loadStudents();
// saveNameToDatabase(name);
JOptionPane.showMessageDialog(AddGUI.this, "保存成功!");
}else {
JOptionPane.showMessageDialog(AddGUI.this, "该学号已存在请重新输入!", "错误", JOptionPane.ERROR_MESSAGE);
}
} else {
JOptionPane.showMessageDialog(AddGUI.this, "请输入完整信息!", "错误", JOptionPane.ERROR_MESSAGE);
}
} catch (ParseException e1) {
JOptionPane.showMessageDialog(AddGUI.this, "日期格式不正确,请按yyyy年MM月dd日格式重新输入!", "错误", JOptionPane.ERROR_MESSAGE);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
});
add(jp7);
setVisible(true);
}
}