简单实现java中JTable表格组件行列增删自由

        JTable表格组件是java的swing组件中最复杂的组件,最近在做一个项目的过程中也遇到了要处理表格增删行列的问题,学习了C站上许多的优质文章,现在总结一下如何简单实现java中JTable的自由增删。

        作为一个新手的我,力求用最简单的知识来完成一个牛逼的表格的增删功能,演示窗口如下:

        实现的关键在于使用DefaultTableModel类作为表格的表格模型,从而能够调用addRow、removeRow、addColumn、removeColumn等方法来操作表格行列的变换。

源代码:

import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.*;

public class Table {
    public static void main(String[] args){
        new Win();
    }
}
//操作的窗口
class Win extends JFrame implements ActionListener {
    JPanel centerPanel = new JPanel();//用centerPanel作为窗口的底层画布
    JButton[] buttons = new JBut
  • 3
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这是一个比较复杂的问题,需要用到Java的GUI编程以及集合和文件操作。以下是简单实现示例,仅供参考: 1. 定义员工类Teacher ```java public class Teacher { private String name; private String sno; private String education; public Teacher(String name, String sno, String education) { this.name = name; this.sno = sno; this.education = education; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSno() { return sno; } public void setSno(String sno) { this.sno = sno; } public String getEducation() { return education; } public void setEducation(String education) { this.education = education; } @Override public String toString() { return "Teacher{" + "name='" + name + '\'' + ", sno='" + sno + '\'' + ", education='" + education + '\'' + '}'; } } ``` 2. 创建GUI界面,并添加JTable和按钮 ```java import javax.swing.*; import javax.swing.table.DefaultTableModel; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.*; import java.util.HashSet; import java.util.Set; public class TeacherGUI extends JFrame { private JTable table; private DefaultTableModel model; private JTextField nameField; private JTextField snoField; private JTextField educationField; public TeacherGUI() { setTitle("教师管理系统"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(600, 400); // 创建JTable model = new DefaultTableModel(); model.addColumn("姓名"); model.addColumn("工号"); model.addColumn("职称"); table = new JTable(model); JScrollPane scrollPane = new JScrollPane(table); // 创建文本框和按钮 nameField = new JTextField(10); snoField = new JTextField(10); educationField = new JTextField(10); JButton addButton = new JButton("添加记录"); addButton.addActionListener(new AddButtonListener()); JButton deleteButton = new JButton("删除记录"); deleteButton.addActionListener(new DeleteButtonListener()); // 创建面板,将组件添加进去 JPanel panel = new JPanel(); panel.add(new JLabel("姓名:")); panel.add(nameField); panel.add(new JLabel("工号:")); panel.add(snoField); panel.add(new JLabel("职称:")); panel.add(educationField); panel.add(addButton); panel.add(deleteButton); // 将面板和JTable添加到窗口 Container contentPane = getContentPane(); contentPane.add(scrollPane, BorderLayout.CENTER); contentPane.add(panel, BorderLayout.SOUTH); setVisible(true); } // 添加记录按钮的监听器 private class AddButtonListener implements ActionListener { @Override public void actionPerformed(ActionEvent e) { String name = nameField.getText(); String sno = snoField.getText(); String education = educationField.getText(); Teacher teacher = new Teacher(name, sno, education); // 将教师对象添加到HashSet和JTable Set<Teacher> teacherSet = readFromFile(); teacherSet.add(teacher); model.addRow(new Object[]{name, sno, education}); // 将教师信息写入文件 writeToFile(teacherSet); } } // 删除记录按钮的监听器 private class DeleteButtonListener implements ActionListener { @Override public void actionPerformed(ActionEvent e) { int selectedRow = table.getSelectedRow(); if (selectedRow >= 0) { // 从HashSet和JTable删除教师对象 Set<Teacher> teacherSet = readFromFile(); String sno = (String)model.getValueAt(selectedRow, 1); teacherSet.removeIf(teacher -> teacher.getSno().equals(sno)); model.removeRow(selectedRow); // 将教师信息写入文件 writeToFile(teacherSet); } } } // 从文件读取教师信息 private Set<Teacher> readFromFile() { Set<Teacher> teacherSet = new HashSet<>(); File file = new File("teacher.txt"); if (file.exists()) { try (BufferedReader reader = new BufferedReader(new FileReader(file))) { String line; while ((line = reader.readLine()) != null) { String[] array = line.split(","); if (array.length == 3) { Teacher teacher = new Teacher(array[0], array[1], array[2]); teacherSet.add(teacher); } } } catch (IOException e) { e.printStackTrace(); } } return teacherSet; } // 将教师信息写入文件 private void writeToFile(Set<Teacher> teacherSet) { File file = new File("teacher.txt"); try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) { for (Teacher teacher : teacherSet) { String line = teacher.getName() + "," + teacher.getSno() + "," + teacher.getEducation(); writer.write(line); writer.newLine(); } } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) { new TeacherGUI(); } } ``` 在这个示例,我们通过HashSet集合来保存教师对象,并且将教师信息写入到文件。当用户点击添加记录按钮时,我们会先从文件读取教师信息,然后将新的教师对象添加到HashSet和JTable,最后再将教师信息写入到文件。当用户点击删除记录按钮时,我们会先从文件读取教师信息,然后从HashSet和JTable删除指定的教师对象,最后再将教师信息写入到文件

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值