因为学生工具类已经写好了,所以就不用写太多了。但是你得判断输入的合法性。已经疯了~无力气嘤嘤嘤
package StudentMajor;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.Comparator;
import java.util.Set;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
public class DeleteStudent {
private JFrame jf;
private JPanel jp1;
private JButton jb1,jb2;
private JTextField jtf;
private JLabel jl1,jl2;
private File file = new File("e:\\abc\\file\\StudentInfo.txt");
DeleteStudent(){
}
public static void main(String[] args) {
new DeleteStudent().init();
}
public void init() {
if(!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
jf = new JFrame("学生管理系统");
jf.setSize(300, 150);//窗口大小
jf.setLocationRelativeTo(null);//居中显示
jf.setLayout(new FlowLayout());//布局
jf.setResizable(false);//大小不可变
jp1 = new JPanel();
jl1 = new JLabel("----请输入您要操作的学号----");
jl2 = new JLabel("学号:");
jtf = new JTextField(10);
jb1 = new JButton("确定");
jb2 = new JButton("返回");
JScrollPane jsp = new SearchStudents().jspOfAllStudents();
jp1.add(jl2);
jp1.add(jtf);
jp1.add(jb1);
jp1.add(jb2);
jf.add(jsp);
jf.add(jl1);
jf.add(jp1);
myEvent();
jf.setVisible(true);
jf.pack();
}
public void myEvent() {
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jb1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(jtf.getText().isEmpty()) {
JOptionPane.showMessageDialog(null, "请输入学号", "提示信息", JOptionPane.WARNING_MESSAGE);
return;
}
boolean flag = false;
flag = StudentTools.deleteStudent(jtf.getText());
if(flag) {
Set<StudentUser> set = StudentTools.set;
if(file.exists()) {
file.delete();
try {
file.createNewFile();
StudentTools.writeToStuFile(file,set);
} catch (IOException e1) {
e1.printStackTrace();
}
JOptionPane.showMessageDialog(null, "删除成功", "提示信息", JOptionPane.WARNING_MESSAGE);
}else {
JOptionPane.showMessageDialog(null, "目前还没有学生", "提示信息", JOptionPane.WARNING_MESSAGE);
}
}else {
JOptionPane.showMessageDialog(null, "没有此学号的学生", "提示信息", JOptionPane.WARNING_MESSAGE);
}
}
});
jb2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new TeacherUser().init();
jf.dispose();
}
});
}
}