import java.awt.*; |
import java.awt.event.*; |
import java.io.*; |
public class StudentInformation implements ActionListener { |
Frame mainFrame; |
Frame inputFrame; |
Frame searchFrame; |
TextArea stuInfo; |
Label lb[] = new Label[ 9 ]; |
Label lb2[] = new Label[ 2 ]; |
TextField tf[] = new TextField[ 9 ]; |
TextField tf2[] = new TextField[ 2 ]; |
Button btn[] = new Button[ 3 ]; |
Button btn2[] = new Button[ 2 ]; |
Panel p1, p2, p3, p4; |
IOOperation ioo; |
Student stu; |
Student student[] = new Student[ 100 ]; |
public StudentInformation() { |
/** |
* set mainframe |
*/ |
mainFrame = new Frame( "Student Information" ); |
mainFrame.addWindowListener( new WindowAdapter() { |
public void windowClosing(WindowEvent e) { |
System.exit( 0 ); |
} |
}); |
MenuItem item1 = new MenuItem( "Record" ); |
MenuItem item2 = new MenuItem( "Search" ); |
MenuItem item3 = new MenuItem( "Modify" ); |
MenuItem item4 = new MenuItem( "Exit" ); |
MenuItem item5 = new MenuItem( "About" ); |
item1.addActionListener( this ); |
item2.addActionListener( this ); |
item3.addActionListener( this ); |
item4.addActionListener( this ); |
item5.addActionListener( this ); |
Menu menu1 = new Menu( "File" ); |
menu1.add(item1); |
menu1.add(item2); |
menu1.add(item3); |
menu1.addSeparator(); |
menu1.add(item4); |
Menu menu2 = new Menu( "Help" ); |
menu2.add(item5); |
MenuBar mb = new MenuBar(); |
mb.add(menu1); |
mb.add(menu2); |
mainFrame.setMenuBar(mb); |
stuInfo = new TextArea(); |
stuInfo.setFont( new Font( "serif" , Font.PLAIN, 18 )); |
mainFrame.add(stuInfo); |
mainFrame.setSize( 400 , 250 ); |
mainFrame.setLocation( 200 , 100 ); |
mainFrame.setVisible( true ); |
/** |
* set inputFrame which is used to record student information |
*/ |
inputFrame = new Frame(); |
inputFrame.addWindowListener( new WindowAdapter() { |
public void windowClosing(WindowEvent e) { |
inputFrame.setVisible( false ); |
} |
}); |
p1 = new Panel( new GridLayout( 9 , 2 )); |
p2 = new Panel(); |
String lbname[] = { "Code:" , "Name:" , "Sex:" , "BirthPlace:" , |
"Class:" , "Chinese:" , "Math:" , "English:" , "TotalScore:" }; |
String btnname[] = { "Save" , "Delete" , " Exit " }; |
for ( int i= 0 ; i< 9 ; i++) { |
lb[i] = new Label(lbname[i]); |
tf[i] = new TextField( 15 ); |
p1.add(lb[i]); |
p1.add(tf[i]); |
} |
for ( int i= 0 ; i< 3 ; i++) { |
btn[i] = new Button(btnname[i]); |
btn[i].addActionListener( this ); |
p2.add(btn[i]); |
} |
btn[ 2 ].setActionCommand( "input" ); |
inputFrame.add(p1, BorderLayout.CENTER); |
inputFrame.add(p2, BorderLayout.SOUTH); |
inputFrame.pack(); |
inputFrame.setLocationRelativeTo(mainFrame); |
/** |
* set searchFrame which is used to search student information |
*/ |
searchFrame = new Frame( "Search student" ); |
searchFrame.addWindowListener( new WindowAdapter() { |
public void windowClosing(WindowEvent e) { |
searchFrame.setVisible( false ); |
} |
}); |
p3 = new Panel( new GridLayout( 2 , 2 )); |
p4 = new Panel(); |
String lbname2[] = { "Code:" , "Name:" }; |
String btnname2[] = { "Search" , " Exit " }; |
for ( int i= 0 ; i< 2 ; i++) { |
lb2[i] = new Label(lbname2[i]); |
tf2[i] = new TextField( 15 ); |
p3.add(lb2[i]); |
p3.add(tf2[i]); |
} |
for ( int i= 0 ; i< 2 ; i++) { |
btn2[i] = new Button(btnname2[i]); |
btn2[i].addActionListener( this ); |
p4.add(btn2[i]); |
} |
btn2[ 1 ].setActionCommand( "search" ); |
searchFrame.add(p3, BorderLayout.CENTER); |
searchFrame.add(p4, BorderLayout.SOUTH); |
searchFrame.pack(); |
searchFrame.setLocationRelativeTo(mainFrame); |
/** |
* IO operation object |
*/ |
ioo = new IOOperation(); |
student = ioo.getAllStudent(); |
} |
public void actionPerformed(ActionEvent e) { |
/** |
* MenuItem action |
*/ |
if (e.getSource() instanceof MenuItem) { |
MenuItem mi = (MenuItem) e.getSource(); |
if (mi.getLabel().equals( "Record" )) { |
inputFrame.setTitle( "Record" ); |
for ( int i= 0 ; i< 9 ; i++) |
tf[i].setText( "" ); |
p2.remove(btn[ 1 ]); |
btn[ 0 ].setActionCommand( "input" ); |
inputFrame.setVisible( true ); |
} |
else if (mi.getLabel().equals( "Search" )) { |
searchFrame.setVisible( true ); |
} |
else if (mi.getLabel().equals( "Modify" )) { |
inputFrame.setTitle( "Modify" ); |
if (stu != null ) { |
tf[ 0 ].setText(stu.getCode()); |
tf[ 1 ].setText(stu.getName()); |
tf[ 2 ].setText(stu.getSex()); |
tf[ 3 ].setText(stu.getBirthPlace()); |
tf[ 4 ].setText(stu.getStuClass()); |
tf[ 5 ].setText(stu.getChinese() + "" ); |
tf[ 6 ].setText(stu.getMath() + "" ); |
tf[ 7 ].setText(stu.getEnglish() + "" ); |
tf[ 8 ].setText(stu.getTotalScore() + "" ); |
} |
p2.remove(btn[ 2 ]); |
p2.add(btn[ 1 ]); |
p2.add(btn[ 2 ]); |
btn[ 0 ].setActionCommand( "modify" ); |
inputFrame.setVisible( true ); |
} |
else if (mi.getLabel().equals( "Exit" )) |
System.exit( 0 ); |
else if (mi.getLabel().equals( "About" )) { |
final Dialog progInfo = new Dialog(mainFrame, "ProgInfo" , true ); |
progInfo.addWindowListener( new WindowAdapter() { |
public void windowClosing(WindowEvent e) { |
progInfo.dispose(); |
} |
}); |
progInfo.setLayout( new FlowLayout()); |
Label l = new Label( "Student Information System" ); |
progInfo.add(l); |
progInfo.setSize( 200 , 80 ); |
progInfo.setLocationRelativeTo(mainFrame); |
progInfo.setVisible( true ); |
} |
} |
/** |
* Button action |
*/ |
else { |
Button btn = (Button)e.getSource(); |
if (btn.getLabel().equals( "Save" )) { |
if (!tf[ 0 ].getText().equals( "" ) && |
!tf[ 1 ].getText().equals( "" ) && |
!tf[ 2 ].getText().equals( "" ) && |
!tf[ 3 ].getText().equals( "" ) && |
!tf[ 4 ].getText().equals( "" ) && |
!tf[ 5 ].getText().equals( "" ) && |
!tf[ 6 ].getText().equals( "" ) && |
!tf[ 7 ].getText().equals( "" ) && |
!tf[ 8 ].getText().equals( "" )) { |
Student s = new Student(tf[ 0 ].getText(), tf[ 1 ].getText(), |
tf[ 2 ].getText(), tf[ 3 ].getText(), tf[ 4 ].getText(), |
Integer.parseInt(tf[ 5 ].getText()), |
Integer.parseInt(tf[ 6 ].getText()), |
Integer.parseInt(tf[ 7 ].getText()), |
Integer.parseInt(tf[ 8 ].getText())); |
if (btn.getActionCommand().equals( "input" )) { |
for ( int i= 0 ; i<student.length; i++) { |
if (student[i] == null ) { |
student[i] = s; |
break ; |
} |
} |
ioo.write(student); |
} |
else { |
for ( int i= 0 ; i<student.length; i++) { |
if (student[i].equals(stu)) { |
student[i] = s; |
break ; |
} |
} |
ioo.write(student); |
} |
} |
inputFrame.setVisible( false ); |
stuInfo.setText( "" ); |
} |
else if (btn.getLabel().equals( "Delete" )) { |
int index = 200 ; |
if (stu != null ) { |
for ( int i= 0 ; i<student.length; i++) { |
if (student[i]!= null && student[i].equals(stu)) { |
index = i; |
if (i != student.length- 1 ) |
student[i] = student[i+ 1 ]; |
else |
student[i] = null ; |
} |
if (i==index && student[i+ 1 ]== null ) |
break ; |
else if (i>index && i<student.length- 1 ) { |
student[i] = student[i+ 1 ]; |
if (i == student.length - 1 ) |
student[i] = null ; |
} |
} |
ioo.write(student); |
} |
stu = null ; |
inputFrame.setVisible( false ); |
stuInfo.setText( "" ); |
/*for(int i=0; i<student.length; i++) { |
if(student[i]!=null) |
System.out.println(i + " " + student[i].getCode()); |
}*/ |
} |
else if (btn.getLabel().equals( "Search" )) { |
stu = null ; |
if (!tf2[ 0 ].getText().equals( "" ) || |
!tf2[ 1 ].getText().equals( "" )) { |
String condition = "" ; |
if (!tf2[ 0 ].getText().equals( "" )) { |
condition = tf2[ 0 ].getText(); |
} |
else |
condition = tf2[ 1 ].getText(); |
for ( int i= 0 ; i<student.length; i++) { |
if (student[i] != null ) { |
if (student[i].getCode().equals(condition) || |
student[i].getName().equals(condition)) { |
stu = student[i]; |
break ; |
} |
} |
} |
} |
if (stu != null ) { |
stuInfo.setText( "Code: " + stu.getCode() + "\n" + |
"Name: " + stu.getName() + "\n" + |
"Sex: " + stu.getSex() + "\n" + |
"BirthPlace: " + stu.getBirthPlace() + "\n" + |
"Class: " + stu.getStuClass() + "\n" + |
"Chinese: " + stu.getChinese() + "\n" + |
"Math: " + stu.getMath() + "\n" + |
"English: " + stu.getEnglish() + "\n" + |
"TotalScore: " + stu.getTotalScore()); |
} |
searchFrame.setVisible( false ); |
} |
else if (btn.getLabel().equals( " Exit " )) |
if (btn.getActionCommand().equals( "input" )) |
inputFrame.setVisible( false ); |
else |
searchFrame.setVisible( false ); |
} |
} |
public static void main(String[] args) { |
new StudentInformation(); |
} |
} |
/** |
* class which is used to store student information |
*/ |
class Student implements Serializable { |
private String code; |
private String name; |
private String sex; |
private String birthPlace; |
private String stuClass; |
private int Chinese; |
private int Math; |
private int English; |
private int totalScore; |
public Student(String code, String name, String sex, String birthPlace, |
String stuClass, int chinese, int math, int english, int totalScore) { |
super (); |
this .code = code; |
this .name = name; |
this .sex = sex; |
this .birthPlace = birthPlace; |
this .stuClass = stuClass; |
Chinese = chinese; |
Math = math; |
English = english; |
this .totalScore = totalScore; |
} |
public String getBirthPlace() { |
return birthPlace; |
} |
public int getChinese() { |
return Chinese; |
} |
public String getCode() { |
return code; |
} |
public int getEnglish() { |
return English; |
} |
public int getMath() { |
return Math; |
} |
public String getName() { |
return name; |
} |
public String getSex() { |
return sex; |
} |
public String getStuClass() { |
return stuClass; |
} |
public int getTotalScore() { |
return totalScore; |
} |
public boolean equals(Object obj) { |
if (obj != null && (obj instanceof Student)) |
if ( this .getCode().equals(((Student)obj).getCode()) && |
this .getName().equals(((Student)obj).getName()) && |
this .getSex().equals(((Student)obj).getSex()) && |
this .getBirthPlace().equals(((Student)obj).getBirthPlace()) && |
this .getStuClass().equals(((Student)obj).getStuClass()) && |
this .getChinese() == ((Student)obj).getChinese() && |
this .getMath() == ((Student)obj).getMath() && |
this .getEnglish() == ((Student)obj).getEnglish() && |
this .getTotalScore() == ((Student)obj).getTotalScore()) |
return true ; |
return false ; |
} |
} |
/** |
* class which is used to do I/O operation |
*/ |
class IOOperation { |
private File file = new File( "C:\\stuinfo.txt" ); |
public IOOperation() { |
try { |
if (!file.exists()) |
file.createNewFile(); |
} catch (IOException e) { |
e.printStackTrace(); |
} |
} |
/** |
* write to file |
*/ |
public void write(Student[] s) { |
try { |
FileOutputStream fos = new FileOutputStream(file); |
ObjectOutputStream objOut = new ObjectOutputStream(fos); |
objOut.writeObject(s); |
objOut.close(); |
fos.close(); |
} catch (Exception e) { |
e.printStackTrace(); |
} |
} |
/** |
* read all student information from file |
*/ |
public Student[] getAllStudent() { |
Student ss[] = new Student[ 100 ]; |
try { |
if (file.length() > 0 ) { |
FileInputStream fis = new FileInputStream(file); |
ObjectInputStream ois = new ObjectInputStream(fis); |
ss = (Student[]) ois.readObject(); |
ois.close(); |
fis.close(); |
} |
} catch (Exception e) { |
e.printStackTrace(); |
} |
return ss; |
} |
} |
初级程序员
by: SHark 发表于:2019-07-11 10:47:39 顶(0) | 踩(0) 回复
代码完整版呢....
回复评论