目录
前言:
目标:
实现:
导入包:
组件文本准备:
总代码:
关于Swing进行一些创作和样品设计,不过笔者还是更加推荐去网上寻找一些视频教程,这里直接看代码的话会比较的蒙,建议初学者以视频讲解类的内容为主。
下图是使用以下代码实现的成品结构图。
import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.tree.DefaultMutableTreeNode;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
创建相关的容器主键,这里文本中有了一定的注释,具体还有那里不懂可以私聊笔者进行询问。
JFrame jf = new JFrame("简单表格");
//创建一维数组.存储标题
Object[] titles = {"姓名", "年龄", "性别"};
//创建二维数组,存储数据
Object[][] data = {
{"李清照", 29, "女"},
{"苏格拉底", 56, "男"},
{"李白", 35, "男"},
{"弄玉", 18, "女"},
{"虎头", 2, "男"}
};
初始化界面:
初始化界面也就是对组件进行一定的封装,将小的组件封装到容器中并实现。
private Vector titlesV = new Vector(); //存储标题
private Vector dataV = new Vector<>(); //存储数据
public void init() {
//组装视图
for (int i = 0; i < titles.length; i++) {
titlesV.add(titles[i]);
}
for (int i = 0; i < data.length; i++) {
Vector t = new Vector<>();
for (int j = 0; j < data[i].length; j++) {
t.add(data[i][j]);
}
dataV.add(t);
}
//创建JTable对象
// JTable jTable = new JTable(dataV,titlesV);
//通过
DefaultTableModel model = new DefaultTableModel(dataV, titlesV);
JTable jTable = new JTable(model);
//创建按钮
JButton addRow = new JButton("添加一行");
JButton addColum = new JButton("添加一列");
JButton deleteRow = new JButton("删除一行");
addRow.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//需要给表格添加一行
model.addRow(new Object[]{"刘岩", 18, "女"});
}
});
addColum.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//给表格添加一列
model.addColumn("职业");
}
});
deleteRow.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//获取选中的行
int selectedRow = jTable.getSelectedRow();
model.removeRow(selectedRow);
}
});
JPanel panel = new JPanel();
panel.add(addRow);
panel.add(addColum);
panel.add(deleteRow);
jf.add(panel, BorderLayout.SOUTH);
jf.add(new JScrollPane(jTable));
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.pack();
jf.setVisible(true);
}
接下来就是完整的代码实现了
package com.swing12;
import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.tree.DefaultMutableTreeNode;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
/**
* 表格JTable_DefaultTableModel
*/
public class Demo123 {
JFrame jf = new JFrame("简单表格");
//创建一维数组.存储标题
Object[] titles = {"姓名", "年龄", "性别"};
//创建二维数组,存储数据
Object[][] data = {
{"李清照", 29, "女"},
{"苏格拉底", 56, "男"},
{"李白", 35, "男"},
{"弄玉", 18, "女"},
{"虎头", 2, "男"}
};
private Vector titlesV = new Vector(); //存储标题
private Vector dataV = new Vector<>(); //存储数据
public void init() {
//组装视图
for (int i = 0; i < titles.length; i++) {
titlesV.add(titles[i]);
}
for (int i = 0; i < data.length; i++) {
Vector t = new Vector<>();
for (int j = 0; j < data[i].length; j++) {
t.add(data[i][j]);
}
dataV.add(t);
}
//创建JTable对象
// JTable jTable = new JTable(dataV,titlesV);
//通过
DefaultTableModel model = new DefaultTableModel(dataV, titlesV);
JTable jTable = new JTable(model);
//创建按钮
JButton addRow = new JButton("添加一行");
JButton addColum = new JButton("添加一列");
JButton deleteRow = new JButton("删除一行");
addRow.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//需要给表格添加一行
model.addRow(new Object[]{"刘岩", 18, "女"});
}
});
addColum.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//给表格添加一列
model.addColumn("职业");
}
});
deleteRow.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//获取选中的行
int selectedRow = jTable.getSelectedRow();
model.removeRow(selectedRow);
}
});
JPanel panel = new JPanel();
panel.add(addRow);
panel.add(addColum);
panel.add(deleteRow);
jf.add(panel, BorderLayout.SOUTH);
jf.add(new JScrollPane(jTable));
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.pack();
jf.setVisible(true);
}
public static void main(String[] args) {
new com.swing12.Demo123().init();
}
}