/*
* StudentDelete.java
*
* Created on 2008年10月16日, 下午11:19
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package manage;
import javax.swing.*;
import java.awt.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.io.*;
public class StudentDelete extends JPanel {
JLabel lblInputNo;
JLabel lblNo;
JLabel lblName;
JLabel lblShowNo;
JLabel lblShowName;
JTextField txtInputNo;
JButton btnCheck;
JButton btnDelete;
String no;
String name;
Font font=new Font("Dialog",Font.PLAIN,16);
public StudentDelete(JLabel lblWelcome) {
lblWelcome.setText("学生界面——删除学生信息");
setLayout(null);
lblInputNo=new JLabel("请输入学生学号:");
lblNo=new JLabel();
lblName=new JLabel();
lblShowNo=new JLabel();
lblShowName=new JLabel();
txtInputNo=new JTextField(15);
btnCheck=new JButton("查看信息");
btnDelete=new JButton("确定删除");
lblInputNo.setFont(font);
lblNo.setFont(font);
lblName.setFont(font);
lblShowNo.setFont(font);
lblShowName.setFont(font);
txtInputNo.setFont(font);
btnCheck.setFont(font);
btnDelete.setFont(font);
add(lblInputNo);
add(lblNo);
add(lblName);
add(lblShowNo);
add(lblShowName);
add(txtInputNo);
add(btnCheck);
add(btnDelete);
lblInputNo.setBounds(30,40,150,30);
lblNo.setBounds(30,110,100,30);
lblName.setBounds(30,180,100,30);
lblShowNo.setBounds(140,110,100,30);
lblShowName.setBounds(140,180,100,30);
txtInputNo.setBounds(170,40,150,30);
btnCheck.setBounds(220,110,100,30);
btnDelete.setBounds(220,180,100,30);
btnCheck.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
lblNo.setText("");
lblName.setText("");
lblShowName.setText("");
lblShowNo.setText("");
boolean isFind=false;
if(txtInputNo.getText().equals("")) {
Object[] options = {"确定"};
JOptionPane.showOptionDialog(null, "请输入要删除的学生学号!", "警告",
JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null, options, options[0]);
}else if(IsNumber.isNumber(txtInputNo.getText())==false) {
Object[] options = {"确定"};
JOptionPane.showOptionDialog(null, "学生学号必须是数字!", "警告",
JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null, options, options[0]);
}else if(isStudentExits()) {
lblNo.setText("学生学号:");
lblName.setText("学生姓名:");
lblShowNo.setText(no);
lblShowName.setText(name);
} else {
lblNo.setText("没有此学生!");
}
}
});
btnDelete.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(txtInputNo.getText().equals("")) {
Object[] options = {"确定"};
JOptionPane.showOptionDialog(null, "请输入要删除的学生学号!", "警告",
JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null, options, options[0]);
}else if(IsNumber.isNumber(txtInputNo.getText())==false) {
Object[] options = {"确定"};
JOptionPane.showOptionDialog(null, "学生学号必须是数字!", "警告",
JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null, options, options[0]);
}else if(isStudentExits()) {
int choose=JOptionPane.showConfirmDialog(null,"您删除此学生信息吗?","确认",JOptionPane.YES_NO_OPTION);
if (choose==JOptionPane.YES_OPTION) {
try {
File stuData=new File("studentData.txt");
File stuDataTemp=new File("studentDataTemp.txt");
FileReader fr=new FileReader("studentData.txt");
FileWriter fw=new FileWriter("studentDataTemp.txt");
BufferedReader br=new BufferedReader(fr);
BufferedWriter bw=new BufferedWriter(fw);
no=br.readLine();
name=br.readLine();
while(no!=null) {
if(!txtInputNo.getText().equals(no)) {
bw.write(no);
bw.newLine();
bw.write(name);
bw.newLine();
}
no=br.readLine();
name=br.readLine();
}
bw.flush();
br.close();
bw.close();
stuData.delete();
stuDataTemp.renameTo(new File("studentData.txt"));
} catch(IOException e1) {
}
}
if (choose==JOptionPane.NO_OPTION) {
}
} else {
lblNo.setText("没有此学生!");
}
}
});
}
private boolean isStudentExits() {
boolean isFind=false;
try {
FileReader fr=new FileReader("studentData.txt");
BufferedReader br=new BufferedReader(fr);
no=br.readLine();
name=br.readLine();
while(no!=null) {
if(txtInputNo.getText().equals(no)) {
fr.close();
br.close();
return true;
}
no=br.readLine();
name=br.readLine();
}
fr.close();
br.close();
} catch(IOException e1) {
}
return false;
}
}