📄 insertstudent.java
字号:
import java.awt.FlowLayout;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
public class InsertStudent extends JDialog implements ActionListener {
JTextField id, name, age, address;
JLabel id1, name1, sex1, age1, address1, empty;
JRadioButton sexB, sexG;
JButton yes, no;
ButtonGroup group;
JPanel p1, p2, p3, p4, p5, p6, p7, p8;
String sex = "男";
Student student = null;
InsertStudent(JFrame f, String s, boolean b) {
super(f, s, b);
setLayout(new FlowLayout());
p1 = new JPanel();
p2 = new JPanel();
p3 = new JPanel();
p4 = new JPanel();
p5 = new JPanel();
p6 = new JPanel();
id = new JTextField(10);
name = new JTextField(10);
group = new ButtonGroup();
sexB = new JRadioButton("男", true);
sexG = new JRadioButton("女");
sexB.addActionListener(this);
sexG.addActionListener(this);
age = new JTextField(10);
address = new JTextField(10);
id1 = new JLabel("学号:");
name1 = new JLabel("姓名:");
sex1 = new JLabel("性别: ");
age1 = new JLabel("年龄:");
address1 = new JLabel("住址:");
empty = new JLabel(" ");
yes = new JButton("添加");
no = new JButton("取消");
yes.addActionListener(this);
no.addActionListener(this);
group.add(sexB);
group.add(sexG);
p1.add(id1);
p1.add(id);
p2.add(name1);
p2.add(name);
p3.add(sex1);
p3.add(sexB);
p3.add(empty);
p3.add(sexG);
p4.add(age1);
p4.add(age);
p5.add(address1);
p5.add(address);
p6.add(yes);
p6.add(no);
add(p1);
add(p2);
add(p3);
add(p4);
add(p5);
add(p6);
setSize(230, 301);
setResizable(false);
setLocation(250, 180);
this.setVisible(true);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
Window w = e.getWindow();
w.setVisible(false);
w.dispose();
}
});
validate();
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == yes) {
String name= this.name.getText().trim();
String idStr =this.id.getText().trim();
student = new Student();
boolean flag = false;
boolean flag1 = false;
String message = age.getText();
char[] atmp = message.toCharArray();
char[] idtmp = idStr.toCharArray();
for (int i = 0; i < atmp.length; i++) {
if ('1' <= atmp[i] && atmp[i] <= '9') {
flag = true;
}
}
for (int i = 0; i < idtmp.length; i++) {
if ('1' <= idtmp[i] && idtmp[i] <= '9') {
flag1 = true;
}
}
if (flag1) {
if (flag) {
StudentManager stm = new StudentManager();
student.setId(Integer.parseInt(idStr));
student.setName(name);
student.setSex(sex);
if(stm.insertStudent(student)){
JOptionPane.showMessageDialog(this, "添加成功!!", "消息",
JOptionPane.WARNING_MESSAGE);
id.setText(null);
this.name.setText(null);
this.age.setText(null);
this.address.setText(null);
}else{
JOptionPane.showMessageDialog(this, "添加失败!!", "消息",
JOptionPane.WARNING_MESSAGE);
id.setText(null);
this.name.setText(null);
this.age.setText(null);
this.address.setText(null);
}
} else {
JOptionPane.showMessageDialog(this, "年龄是无效字符!!", "消息",
JOptionPane.WARNING_MESSAGE);
this.age.setText(null);
}
} else {
JOptionPane.showMessageDialog(this, "ID是无效字符!!", "消息",
JOptionPane.WARNING_MESSAGE);
id.setText(null);
}
}
if(e.getSource()==sexB){
sex="男";
}
if(e.getSource()==sexG){
sex="女";
}
if (e.getSource() == no) {
this.setVisible(false);
this.dispose();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -