赞
踩
JPanel panel;//面板放置按钮
JPanel panelCheck;//面板放置选择框
public StudentFrame() {
this.setSize(420,350);
this.setTitle(“学生记录Deal”);
this.setLayout(null);
init();
//关闭窗口就结束程序
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//大小不可变
this.setResizable(false);
//可见
this.setVisible(true);
}
private void init() {
panelCheck = new JPanel();
panelCheck.setSize(310,30);
panelCheck.setLocation(70, 10);
panelCheck.setLayout(new GridLayout(1,4,10,0));
//panel.setBackground(Color.red);
String[] checkNames = new String[] {“添加”, “删除”, “查询”};
checkBoxs = new JCheckBox[3];
for (int i = 0; i < checkBoxs.length; i++) {
checkBoxs[i] = new JCheckBox(checkNames[i]);
checkBoxs[i].addActionListener(this);
panelCheck.add(checkBoxs[i]);
}
this.add(panelCheck);
numLabel=new JLabel(“学号”);
numLabel.setSize(60, 30);//设置控件的大小
numLabel.setLocation(80, 50);//设置控件的左上角坐标
this.add(numLabel);
numText=new JTextField();
numText.setSize(170, 30);
numText.setLocation(140,50);//设置控件的左上角坐标
numText.setEnabled(false);
this.add(numText);
nameLabel=new JLabel(“姓名”);
nameLabel.setSize(60, 30);//设置控件的大小
nameLabel.setLocation(80, 100);//设置控件的左上角坐标
this.add(nameLabel);
nameText=new JTextField();
nameText.setSize(170, 30);
nameText.setLocation(140,100);//设置控件的左上角坐标
nameText.setEnabled(false);
this.add(nameText);
sexLabel=new JLabel(“性别”);
sexLabel.setSize(60, 30);//设置控件的大小
sexLabel.setLocation(80, 150);//设置控件的左上角坐标
this.add(sexLabel);
sexCom=new JComboBox();
sexCom.addItem(“”);
sexCom.addItem(“男”);
sexCom.addItem(“女”);
sexCom.setSize(170, 30);
sexCom.setLocation(140,150);//设置控件的左上角坐标
sexCom.setEnabled(false);
this.add(sexCom);
panel = new JPanel();
panel.setSize(310,30);
panel.setLocation(50, 220);
panel.setLayout(new GridLayout(1,4,10,0));
//panel.setBackground(Color.red);
String[] buttonsNames = new String[] {“添加”, “删除”, “查询”, “清空”};
buttons = new JButton[4];
for (int i = 0; i < buttons.length; i++) {
buttons[i] = new JButton(buttonsNames[i]);
buttons[i].setEnabled(false);
buttons[i].addActionListener(this);
panel.add(buttons[i]);
}
this.add(panel);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource().getClass().toString().contains(“JCheckBox”)) {
JCheckBox check = (JCheckBox) e.getSource();
if(check.getText().equals(“添加”)) {
checkBoxs[1].setSelected(false);
checkBoxs[2].setSelected(false);
numText.setEnabled(true);
nameText.setEnabled(true);
sexCom.setEnabled(true);
buttons[0].setEnabled(true);
buttons[1].setEnabled(false);
buttons[2].setEnabled(false);
buttons[3].setEnabled(true);
return;
}
if(check.getText().equals(“删除”)) {
checkBoxs[0].setSelected(false);
checkBoxs[2].setSelected(false);
numText.setEnabled(true);
nameText.setEnabled(false);
sexCom.setEnabled(false);
buttons[0].setEnabled(false);
buttons[1].setEnabled(true);
buttons[2].setEnabled(false);
buttons[3].setEnabled(true);
return;
}
if(check.getText().equals(“查询”)) {
checkBoxs[0].setSelected(false);
checkBoxs[1].setSelected(false);
numText.setEnabled(true);
nameText.setEnabled(false);
sexCom.setEnabled(false);
buttons[0].setEnabled(false);
buttons[1].setEnabled(false);
buttons[2].setEnabled(true);
buttons[3].setEnabled(true);
return;
}
}
if(e.getSource().getClass().toString().contains(“JButton”)) {
JButton bt = (JButton) e.getSource();
if(bt.getText().equals(“添加”)) {
if(numText.getText().equals(“”) || nameText.getText().equals(“”)
|| sexCom.getSelectedIndex() == 0) {
JOptionPane.showMessageDialog(this,“信息不完整添加失败”);
return;
}
StudentDeal deal = new StudentDeal();
Student student = new Student(numText.getText().trim(), nameText.getText().trim()
, sexCom.getSelectedItem().toString());
deal.addNewStudent(student);
JOptionPane.showMessageDialog(this,“添加成功”);
return;
}
if(bt.getText().equals(“删除”)) {
if(numText.getText().trim().equals(“”)) {
JOptionPane.showMessageDialog(this,“信息不完整删除失败”);
return;
}
StudentDeal deal = new StudentDeal();
Student student = deal.findStuByNum(numText.getText().trim());
if(student == null) {
JOptionPane.showMessageDialog(this, “无此人”);
return;
}
deal.delStudent(numText.getText().trim());
JOptionPane.showMessageDialog(this, “删除成功”);
return;
}
if(bt.getText().equals(“查询”)) {
if(numText.getText().trim().equals(“”)) {
JOptionPane.showMessageDialog(this,“信息不完整查询失败”);
return;
}
StudentDeal deal = new StudentDeal();
Student student = deal.findStuByNum(numText.getText().trim());
if(student == null) {
JOptionPane.showMessageDialog(this, “无此人”);
return;
}
nameText.setText(student.name);
sexCom.setSelectedItem(student.sex);
return;
}
if(bt.getText().equals(“清空”)) {
numText.setText(“”);
nameText.setText(“”);
sexCom.setSelectedIndex(0);
}
}
}
}
class Student
{
public String num;
public String name;
public String sex;
public Student(String num,String name,String sex)
{
this.num=num;
this.name=name;
this.sex=sex;
}
}
class StudentDeal {
//通过学号来查找学生对象
public Student findStuByNum(String num)
{
Student student=null;
try
{
FileReader fr=new FileReader(“d:\student.txt”);
BufferedReader br=new BufferedReader(fr);
//以文件字符输入流对象为参数来创建一个缓冲的字符输入流对象
String temp=br.readLine();//先从文件中读取第一行数据
while(temp!=null)
{
String[] infos=temp.split(“,”);
//使用切割行数将读取到的一行数据分割成一个字符串数组
if(infos[0].equals(num))//判断第一个元素是否等于所给的学号参数
{
student=new Student(infos[0],infos[1],infos[2]);
//通过切割得到的数组元素来实例化一个学生对象
break;
}
temp=br.readLine();//再次读取一行数据
}
br.close();
fr.close();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
return student;
}
}
//往学生记录文件中插入一条学生记录
public void addNewStudent(Student stu)
{
if(stu!=null)//首先判断插入对象不为空
{
if(!checkNumIsExist(stu.num))//判断学号是否已经存在
{
try
{
FileWriter fw=new FileWriter(“d:\student.txt”,true);
//表示在写文件的时候是追加而不是覆盖
BufferedWriter bw=new BufferedWriter(fw);
//通过文件字符输出流对象来创建缓冲的字符输出流对象
StringBuffer str=new StringBuffer();
str.append(stu.num+“,”);
str.append(stu.name+“,”);
str.append(stu.sex);
bw.newLine();//写入之前先换行
bw.write(str.toString());
bw.close();
fw.close();
}
catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}
//通过所给的学号来删除学生记录
public void delStudent(String num)
{
ArrayList students=getAllStudents();
//将文件中的所有的学生记录读取到容器对象中
for(Student temp:students)//用foreach循环来遍历查找所有的学生对象
{
if(temp.num.equals(num))//判断某一个学生对象的学号是否和所给的参数学号是相等的
{
students.remove(temp);//将对应的学生对象移除出容器
break;//退出循环,因为学号是唯一的
}
}
///以下操作为将容器对象中的学生记录覆盖式写入到学生记录文件中
try
{
FileWriter fw=new FileWriter(“d:\student.txt”);
//创建文件字符输出流的时候,应该采用的是覆盖式的写入
BufferedWriter bw=new BufferedWriter(fw);
//创建一个缓冲的字符输出流对象
for(Student temp:students)//用foreach循环来遍历查找所有的学生对象
{
//将某个学生对象转换为标准的字符串
StringBuffer str=new StringBuffer();
str.append(temp.num+“,”);
str.append(temp.name+“,”);
str.append(temp.sex);
//将转换完的字符串写入到学生记录文件
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数Java工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你觉得这些内容对你有帮助,可以扫码获取!!(备注Java获取)
为了让大家能够在Redis上能够加深,所以这次给大家准备了一些Redis的学习资料,还有一些大厂的面试题,包括以下这些面试题
并发编程面试题汇总
JVM面试题汇总
Netty常被问到的那些面试题汇总
Tomcat面试题整理汇总
Mysql面试题汇总
Spring源码深度解析
Mybatis常见面试题汇总
Nginx那些面试题汇总
Zookeeper面试题汇总
RabbitMQ常见面试题汇总
JVM常频面试:
Mysql面试题汇总(一)
Mysql面试题汇总(二)
Redis常见面试题汇总(300+题)
《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》,点击传送门即可获取!
为了让大家能够在Redis上能够加深,所以这次给大家准备了一些Redis的学习资料,还有一些大厂的面试题,包括以下这些面试题
并发编程面试题汇总
JVM面试题汇总
Netty常被问到的那些面试题汇总
Tomcat面试题整理汇总
Mysql面试题汇总
Spring源码深度解析
Mybatis常见面试题汇总
Nginx那些面试题汇总
Zookeeper面试题汇总
RabbitMQ常见面试题汇总
JVM常频面试:
[外链图片转存中…(img-vETSjkxG-1712115381292)]
Mysql面试题汇总(一)
[外链图片转存中…(img-wF3JpizE-1712115381293)]
Mysql面试题汇总(二)
[外链图片转存中…(img-nkvzgV18-1712115381293)]
Redis常见面试题汇总(300+题)
[外链图片转存中…(img-9YK8ZyN0-1712115381293)]
《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》,点击传送门即可获取!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。