Java~如何实现GUI用户界面将(Swing组件、事件处理机制、布局管理器、IO输入输出)相结合

GUI(图形用户界面一)AWT概述、布局管理器(流式布局、边界布局、网格布局、精确布局)
https://blog.csdn.net/Shangxingya/article/details/105920282

事件处理机制(窗口事件、鼠标事件、键盘事件、动作事件)
https://blog.csdn.net/Shangxingya/article/details/105942200

IO输入输出流练习题项目 (优化后的)教师信息类管理 (实现五中查询方法 修改信息方法 增加教师方法 删除教师方法 和批量处理教师方法)
https://blog.csdn.net/Shangxingya/article/details/105692566

需要的相关控件:

      JLabel numLabel, nameLabel, sexLabel;//标签
    JTextField numText, nameText;//文本框
    JComboBox sexCom;//下拉框
    JCheckBox[] checkBoxs;//选择框
    JButton[] buttons;//按钮
    JPanel panel;//面板放置按钮
    JPanel panelCheck;//面板放置选择框

顶级容器设置:

  this.setSize(420,350);
        this.setTitle("学生记录Deal");
        this.setLayout(null);
        init();
        //关闭窗口就结束程序
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //大小不可变
        this.setResizable(false);
        //可见
        this.setVisible(true);

(重点)实现ActionListener监听接口,对事件处理和IO输入输出进行衔接

  • 在动作事件处理时,要对动作进行分辨是选择框还是按钮。
 if (e.getSource().getClass().toString().contains("JCheckBox")) {
 }
  if(e.getSource().getClass().toString().contains("JButton")) {
  }
  • 初始我设置所有的功能控件为setEnabled(false),用户在选择框进行选择要执行什么功能的时候我就开方什么功能。
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(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(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;
  • 对于查询按钮,还是学号文本框判空操作,若为空提示信息不完整,然后调用学生处理对象的查询方法,因为该方法返回的是一个学生对象,所以对其进行判空,若为空则提示无此人,若不为空,则设置姓名文本框和下拉框的setText和setSelectedItem。
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);
            }

完整代码


import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class StudentFrame extends JFrame implements ActionListener {
    JLabel numLabel, nameLabel, sexLabel;//标签
    JTextField numText, nameText;//文本框
    JComboBox sexCom;//下拉框
    JCheckBox[] checkBoxs;//选择框
    JButton[] buttons;//按钮
    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<Student> 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);
                //将转换完的字符串写入到学生记录文件
                bw.write(str.toString());
                bw.newLine();//创建新的一行
            }
            bw.close();
            fw.close();//关闭两个输出流
        }
        catch(FileNotFoundException ex)
        {

        }
        catch(IOException ex)
        {

        }
    }

猜你喜欢

转载自blog.csdn.net/Shangxingya/article/details/105982836