package Student_system;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.*;
/*class Stu implements java.io.Serializable{
String number,name,specialty,grade,borth,sex;
public Stu(){};
public void setNumber(String number){ this.number=number;}
public String getNumber(){ return number;}
public void setName(String name){ this.name=name;}
public String getName(){ return name;}
public void setSex(String sex){ this.sex=sex;}
public String getSex(){ return sex;}
public void setSpecialty(String specialty){ this.specialty=specialty;}
public String getSpecialty(){ return specialty;}
public void setGrade(String grade){ this.grade=grade;}
public String getGrade(){ return grade;}
public void setBorth(String borth){ this.borth=borth;}
public String getBorth(){ return borth;}
}*/
public class asdd extends JFrame{
public static void main(String[] args){
JFrame frame = new JFrame();
frame.setTitle("信息管理系统");
frame.setSize(500, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container container = frame.getContentPane();
container.setLayout(new FlowLayout());
JLabel lb=new JLabel("录入请先输入记录,查询、删除请先输入学号,修改是对查询" +
"内容改后的保存!");
final JTextField 学号;
final JTextField 姓名;
final JTextField 专业;
final JTextField 年级;
final JTextField 出生;
final JRadioButton 男;
final JRadioButton 女;
ButtonGroup group=null;
JButton 录入,查询,删除,修改,显示;
JPanel p1,p2,p3,p4,p5,p6,pv,ph;
学号=new JTextField(10);
姓名=new JTextField(10);
专业=new JTextField(10);
年级=new JTextField(10);
出生=new JTextField(10);
group=new ButtonGroup();
男=new JRadioButton("男");
女=new JRadioButton("女");
group.add(男);
group.add(女);
录入=new JButton("录入");
查询=new JButton("查询");
删除=new JButton("删除");
修改=new JButton("修改");
显示=new JButton("显示");
修改.setEnabled(false);
//添加输入框及文本框
p1=new JPanel();
p1.add(new JLabel("学号:",JLabel.CENTER));
p1.add(学号);
p2=new JPanel();
p2.add(new JLabel("姓名:",JLabel.CENTER));
p2.add(姓名);
p3=new JPanel();
p3.add(new JLabel("性别:",JLabel.CENTER));
p3.add(男);
p3.add(女);
p4=new JPanel();
p4.add(new JLabel("专业:",JLabel.CENTER));
p4.add(专业);
p5=new JPanel();
p5.add(new JLabel("年级:",JLabel.CENTER));
p5.add(年级);
p6=new JPanel();
p6.add(new JLabel("出生:",JLabel.CENTER));
p6.add(出生);
pv=new JPanel();
pv.setLayout(new GridLayout(6,1));
pv.add(p1);
pv.add(p2);
pv.add(p3);
pv.add(p4);
pv.add(p5);
pv.add(p6);
ph=new JPanel();
ph.add(录入);
ph.add(查询);
ph.add(修改);
ph.add(删除);
ph.add(显示);
frame.add(lb);
frame.add(ph);
frame.add(p1);
frame.add(p2);
frame.add(p3);
frame.add(p4);
frame.add(p5);
frame.add(p6);
frame.add(pv);
frame.add(ph);
class MyListener implements ActionListener {
public void actionPerformed (ActionEvent e) {
//判断选中是男是女
/*if(e.getSource().equals(男)){
if(男.isSelected()) {
System.out.println("男被选中了");
} else {
System.out.println("男被取消选中了");
}
}
if(e.getSource().equals(女)){
if(女.isSelected()) {
System.out.println("女被选中了");
} else {
System.out.println("女被取消选中了");
}
}*/
//录入 的功能
if(e.getActionCommand() == "录入") {
String text1 = 学号.getText().trim();
String text2 = 姓名.getText();
String text3 = 专业.getText();
String text4 = 年级.getText();
String text5 = 出生.getText();
String text6 = null;
String text7 = "- - - - -我是分割线 - - - - -";
if(男.isSelected()) {
text6 = "男";
}
if(女.isSelected()) {
text6 = "女";
}
//用texts包含此次所有录入信息
String texts = "\n\n学号:" + text1 + "\n\n" + "姓名:" + text2 + "\n\n" + "专业:" + text3 + "\n\n" + "年级:" + text4 + "\n\n" + "出生:" + text5 + "\n\n" + "性别:" + text6 + "\n\n" ;
//显示框
int m = JOptionPane.showConfirmDialog(null, "是否录入该条记录:" + texts , "录入" , JOptionPane.YES_NO_OPTION);//n = 1/0;
if(m == 0) {
try {
File file = new File("D:/file.txt");
if(!file.exists()) {
file.createNewFile();
}
FileWriter fileWriter = new FileWriter(file,true);
BufferedWriter writer = new BufferedWriter(fileWriter);
writer.write(text1 + "\r\n");
writer.write(text2 + "\r\n");
writer.write(text6 + "\r\n");
writer.write(text3 + "\r\n");
writer.write(text4 + "\r\n");
writer.write(text5 + "\r\n");
writer.write(text7 + "\r\n");
writer.close();
} catch (Exception e1) {
e1.printStackTrace();
}
System.out.println("学号: " + text1 + "\n" + "姓名: "
+ " " + text2 + "\n" + "性别: " + text6 + "\n专业: " + text3 + "\n" + "年级: " + text4 + "\n" + "出生: " + text5 + "\n" + text7);
JOptionPane.showMessageDialog(null, "该次录入成功!!");
} else {
JOptionPane.showMessageDialog(null, "已取消该次录入!!");
}
}
//显示 的功能
if(e.getActionCommand() == "显示") {
String text1 = 学号.getText().trim();
int n = JOptionPane.showConfirmDialog(null, "是否显示所有记录" , "显示" , JOptionPane.YES_NO_OPTION);//n = 1/0;
if(n ==0 ) {
try {
File file = new File("D:/file.txt");
InputStreamReader reader = new InputStreamReader(
new FileInputStream(file));//创建一个输入流对象
BufferedReader bufferReader = new BufferedReader(reader);
String line = "";
String lines = null;
line = bufferReader.readLine();
while(line != null) {
System.out.println(line);
if(lines != null) {
lines = lines + "\n" + line + "\n";
} else {
lines = line + "\n" ;
}
line = bufferReader.readLine();
}
JOptionPane.showMessageDialog(null, lines , "显示",JOptionPane.INFORMATION_MESSAGE);
} catch (Exception e1) {
e1.printStackTrace();
}
} else {
JOptionPane.showMessageDialog(null, "已取消该次查询");
}
}
//查询 的功能
if(e.getActionCommand() == "查询") {
File file = new File("D:/file.txt");
BufferedReader reader = null;
String text7 = "- - - - -我是分割线 - - - - -";
try {
//InputStreamReader reader = new InputStreamReader(
// new FileInputStream(file));//创建一个输入流对象
reader = new BufferedReader(new FileReader(file));
//暂时仅允许查询学号
String text1 = 学号.getText();
int n = JOptionPane.showConfirmDialog(null, "查询的学号为:" + text1 , "查询" , JOptionPane.YES_NO_OPTION);//n = 1/0;
if(n == 0){
String line = null;
String lines = null;
//int cout = 2;
while((line = reader.readLine())!= null) {
if(line.equals(text1)) {
System.out.println(line);
while(!(line.equals(text7))){
line = reader.readLine();
if(lines == null) {
lines = line + "\n";
} else {
lines = lines + line + "\n";
}
System.out.println(line);
}
} else {
continue;
}
//cout++;
}
JOptionPane.showMessageDialog(null, lines , "查询",JOptionPane.INFORMATION_MESSAGE);
reader.close();
} else {
JOptionPane.showMessageDialog(null, "已取消查询功能");
}
} catch (Exception e1) {
e1.printStackTrace();
} finally {
if(reader != null) {
try {
reader.close();
}catch (IOException e1){
}
}
}
}
//删除 的功能
}
}
//注册监听
MyListener listener = new MyListener();
MyListener listen = new MyListener();
显示.addActionListener(listener);
录入.addActionListener(listener);
查询.addActionListener(listener);
删除.addActionListener(listener);
男.addActionListener(listen);
女.addActionListener(listen);
frame.setVisible(true);
}
}
JAVA学生信息管理系统
最新推荐文章于 2024-07-19 22:27:53 发布