📄 zystudentsituation.java
字号:
package zhangyan34;
import javax.swing.JFrame;
import java.awt.BorderLayout;
import java.util.*;
import java.io.*;
import javax.swing.*;
import javax.swing.JLabel;
import java.awt.*;
import javax.swing.JTextField;
import javax.swing.JRadioButton;
import javax.swing.JButton;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JTextArea;
import javax.swing.border.TitledBorder;
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2007</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class zyStudentSituation extends JFrame {
Hashtable info=null;
FileInputStream inOne=null;
ObjectInputStream inTwo=null;
FileOutputStream outOne=null;
ObjectOutputStream outTwo=null;
File file=null;
JLabel jLabel1 = new JLabel();
JLabel jLabel2 = new JLabel();
JLabel jLabel3 = new JLabel();
JLabel jLabel4 = new JLabel();
JLabel jLabel5 = new JLabel();
JLabel jLabel6 = new JLabel();
JTextField jTextField1 = new JTextField();
JTextField jTextField2 = new JTextField();
JRadioButton jRadioButton1 = new JRadioButton();
JLabel jLabel7 = new JLabel();
JRadioButton jRadioButton2 = new JRadioButton();
JLabel jLabel8 = new JLabel();
JTextField jTextField3 = new JTextField();
JTextField jTextField4 = new JTextField();
JTextField jTextField5 = new JTextField();
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
TitledBorder titledBorder1 = new TitledBorder("");
TitledBorder titledBorder2 = new TitledBorder("");
public zyStudentSituation() {
try {
jbInit();
} catch (Exception ex) {
ex.printStackTrace();
}
}
public zyStudentSituation(File file) {
super("录入界面");
this.file=file;
try {
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}
private void jbInit() throws Exception {
this.setSize(500,400);
getContentPane().setLayout(null);
jLabel1.setText("学号");
jLabel1.setBounds(new Rectangle(30, 26, 63, 22));
jLabel3.setText("性别");
jLabel3.setBounds(new Rectangle(32, 100, 46, 29));
jLabel4.setText("专业");
jLabel4.setBounds(new Rectangle(30, 146, 55, 28));
jLabel5.setText("年极");
jLabel5.setBounds(new Rectangle(25, 206, 64, 25));
jLabel6.setText("出生");
jLabel6.setBounds(new Rectangle(28, 254, 53, 34));
jTextField1.setText("jTextField1");
jTextField1.setBounds(new Rectangle(103, 18, 129, 37));
jTextField2.setText("jTextField2");
jTextField2.setBounds(new Rectangle(103, 63, 129, 37));
jRadioButton1.setText("jRadioButton1");
jRadioButton1.setBounds(new Rectangle(102, 114, 28, 17));
jLabel7.setText("男");
jLabel7.setBounds(new Rectangle(128, 114, 33, 20));
jRadioButton2.setText("jRadioButton2");
jRadioButton2.setBounds(new Rectangle(166, 116, 19, 15));
jLabel8.setText("女");
jLabel8.setBounds(new Rectangle(196, 115, 45, 19));
jTextField3.setText("jTextField3");
jTextField3.setBounds(new Rectangle(104, 142, 131, 35));
jTextField4.setText("jTextField4");
jTextField4.setBounds(new Rectangle(103, 197, 134, 37));
jTextField5.setText("jTextField5");
jTextField5.setBounds(new Rectangle(104, 248, 133, 42));
jButton1.setBounds(new Rectangle(71, 320, 72, 39));
jButton1.setBorder(titledBorder2);
jButton1.setText("录入");
jButton1.addActionListener(new
zyStudentSituation_jButton1_actionAdapter(this));
jButton2.setBounds(new Rectangle(181, 318, 74, 39));
jButton2.setBorder(titledBorder1);
jButton2.setText("重置");
jButton2.addActionListener(new
zyStudentSituation_jButton2_actionAdapter(this));
this.getContentPane().add(jLabel1);
this.getContentPane().add(jLabel2);
this.getContentPane().add(jLabel4);
this.getContentPane().add(jLabel3);
this.getContentPane().add(jTextField2);
this.getContentPane().add(jTextField1);
this.getContentPane().add(jRadioButton1);
this.getContentPane().add(jLabel7);
this.getContentPane().add(jRadioButton2);
this.getContentPane().add(jLabel8);
this.getContentPane().add(jTextField3);
this.getContentPane().add(jTextField4);
this.getContentPane().add(jTextField5);
this.getContentPane().add(jLabel6);
this.getContentPane().add(jLabel5);
this.getContentPane().add(jButton1);
this.getContentPane().add(jButton2);
jLabel2.setText("姓名");
jLabel2.setBounds(new Rectangle(31, 61, 66, 27));
}
public void jButton1_actionPerformed(ActionEvent e) {
String number="";
number=jTextField1.getText();
if (number.length()>0) // 如果number字符串的长度大于0
{
try { // 读取文件中存放学生信息的的散列表
inOne=new FileInputStream(file);
inTwo=new ObjectInputStream(inOne);
info=(Hashtable)inTwo.readObject();
inOne.close();
inTwo.close();
}
catch(Exception ee)
{
}
if(info.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=null;
name=jTextField2.getText();// 声明字符串对象name,并且将姓名文本框中的内容赋值给它。
String discipling=null;
discipling=jTextField3.getText();//声明字符串对象discipling,并且将专业文本框中的内容赋值给它
String grade=null;
grade=jTextField4.getText();//声明字符串对象grade,并且将年级文本框中的内容赋值给它
String birth=null;
birth=jTextField5.getText();//声明字符串对象borth,并且将出生文本框中的内容赋值给它
String sex=null;//声明字符串对象sex,并且将null值赋给它
if (jRadioButton1.isSelected()) // 如果“男”单选按钮被选中
{
sex=jRadioButton1.getText(); //通过方法getText() 将“男”单选按钮的标签,即“男”字符// 串赋值给sex
}
else
{
sex=jRadioButton2.getText();// 否则通过方法getText()将“女”单选按钮的标签,即“女”//字符串赋值给sex
}
zystudent stu=new zystudent(); // 实例化Student类的对象stu
stu.setNumber(number);//将Student类中的成员变量number赋值为number变量的值(使用方法setNumber())
stu.setName(name);//将Student类中的成员变量name赋值为name变量的值
stu.setDiscipling(discipling);//将Student类中的成员变量discipling赋值为discipling变量的值
stu.setGrade(grade);//将Student类中的成员变量grade赋值为grade变量的值
stu.setBorth(birth);//将Student类中的成员变量borth赋值为borth变量的值
stu.setsex(sex);//将Student类中的成员变量sex赋值为sex变量的值
try{
outOne=new FileOutputStream(file);
outTwo=new ObjectOutputStream(outOne);
info.put(number,stu);
outTwo.writeObject(info);
outTwo.close();
outOne.close();
jTextField1.setText(null); //使用setText(null)方法将学号文本框清空
jTextField2.setText(null);// 将姓名文本框清空
jTextField3.setText(null);// 将专业文本框清空
jTextField4.setText(null);// 将年级文本框清空
jTextField5.setText(null);// 将出生文本框清空
}
catch(Exception ee){ }
}
}
}
else // 如果number的长度不大于0
{ String warning="必须输入学号!";
JOptionPane.showMessageDialog(this,warning,"警告",
JOptionPane.WARNING_MESSAGE);
}
}
public void jButton2_actionPerformed(ActionEvent e) {
jTextField1.setText(null); //使用setText(null)方法将学号文本框清空
jTextField2.setText(null);// 将姓名文本框清空
jTextField3.setText(null);// 将专业文本框清空
jTextField4.setText(null);// 将年级文本框清空
jTextField5.setText(null);// 将出生文本框清空
}
}
class zyStudentSituation_jButton2_actionAdapter implements ActionListener {
private zyStudentSituation adaptee;
zyStudentSituation_jButton2_actionAdapter(zyStudentSituation adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton2_actionPerformed(e);
}
}
class zyStudentSituation_jButton1_actionAdapter implements ActionListener {
private zyStudentSituation adaptee;
zyStudentSituation_jButton1_actionAdapter(zyStudentSituation adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -