?? studentsituation.java
字號:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.*;
public class StudentSituation extends JPanel implements ActionListener
{
Hashtable 基本信息表=null;
JTextField 學號,姓名,專業,年級,出生;
JRadioButton 男,女;
Student 學生=null;
ButtonGroup group=null;
JButton 錄入,重置;
FileInputStream inOne=null;
ObjectInputStream inTwo=null;
FileOutputStream outOne=null;
ObjectOutputStream outTwo=null;
File file=null;
public StudentSituation(File file)
{
this.file=file;
學號=new JTextField(10);
姓名=new JTextField(10);
專業=new JTextField(10);
年級=new JTextField(10);
出生=new JTextField(10);
group=new ButtonGroup();
男=new JRadioButton("男",true);
女=new JRadioButton("女",false);
group.add(男);
group.add(女);
錄入=new JButton("錄入");
重置=new JButton("重置");
錄入.addActionListener(this);
重置.addActionListener(this);
Box box1=Box.createHorizontalBox();
box1.add(new JLabel("學號:",JLabel.CENTER));
box1.add(學號);
Box box2=Box.createHorizontalBox();
box2.add(new JLabel("姓名:",JLabel.CENTER));
box2.add(姓名);
Box box3=Box.createHorizontalBox();
box3.add(new JLabel("性別:",JLabel.CENTER));
box3.add(男);
box3.add(女);
Box box4=Box.createHorizontalBox();
box4.add(new JLabel("專業:",JLabel.CENTER));
box4.add(專業);
Box box5=Box.createHorizontalBox();
box5.add(new JLabel("年級:",JLabel.CENTER));
box5.add(年級);
Box box6=Box.createHorizontalBox();
box6.add(new JLabel("出生:",JLabel.CENTER));
box6.add(出生);
Box boxH=Box.createVerticalBox();
boxH.add(box1);
boxH.add(box2);
boxH.add(box3);
boxH.add(box4);
boxH.add(box5);
boxH.add(box6);
boxH.add(Box.createVerticalGlue());
JPanel pCenter=new JPanel();
pCenter.add(boxH);
setLayout(new BorderLayout());
add(pCenter,BorderLayout.CENTER);
JPanel pSouth=new JPanel();
pSouth.add(錄入);
pSouth.add(重置);
add(pSouth,BorderLayout.SOUTH);
validate();
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==錄入)
{
String number="";
number=學號.getText();
if(number.length()>0)
{
try {
inOne=new FileInputStream(file);
inTwo=new ObjectInputStream(inOne);
基本信息表=(Hashtable)inTwo.readObject();
inOne.close();
inTwo.close();
}
catch(Exception ee)
{
}
if(基本信息表.containsKey(number))
{
String warning="該生基本信息已存在,請到修改頁面修改!";
JOptionPane.showMessageDialog(this,warning,"警告",JOptionPane.WARNING_MESSAGE);
}
else
{
String m="基本信息將被錄入!";
int ok=JOptionPane.showConfirmDialog(this,m,"確認",JOptionPane.YES_NO_OPTION,
JOptionPane.INFORMATION_MESSAGE);
if(ok==JOptionPane.YES_OPTION)
{
String name=姓名.getText();
String discipling=專業.getText();
String grade=年級.getText();
String borth=出生.getText();
String sex=null;
if(男.isSelected())
{
sex=男.getText();
}
else
{
sex=女.getText();
}
學生=new Student();
學生.setNumber(number);
學生.setName(name);
學生.setDiscipling(discipling);
學生.setGrade(grade);
學生.setBorth(borth);
學生.setSex(sex);
try{
outOne=new FileOutputStream(file);
outTwo=new ObjectOutputStream(outOne);
基本信息表.put(number,學生);
outTwo.writeObject(基本信息表);
outTwo.close();
outOne.close();
學號.setText(null);
姓名.setText(null);
專業.setText(null);
年級.setText(null);
出生.setText(null);
}
catch(Exception ee)
{
System.out.println(ee);
}
}
}
}
else
{
String warning="必須要輸入學號!";
JOptionPane.showMessageDialog(this,warning,"警告",JOptionPane.WARNING_MESSAGE);
}
}
if(e.getSource()==重置)
{
學號.setText(null);
姓名.setText(null);
專業.setText(null);
年級.setText(null);
出生.setText(null);
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -