package equ.view;
import equ.model.Equ;
import equ.utils.SqliteDb;
import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Arrays;
/*
顶层容器,从JFrame继承
*/
public class MainView extends JFrame {
JTextField searchText; // 搜索前面的输入框。
static JTable table; // 表格
// 在构造方法中绘制页面
public MainView() {
// 设置窗口的标题
setTitle("设备管理系统");
// 把中间层容器加到顶层容器上。
add(northPanel(), BorderLayout.NORTH);
add(centerPanel(), BorderLayout.CENTER);
// 设置窗口的位置,xy是坐标点,相对于屏幕左上角的位置。
setBounds(200, 100, 800, 600);
// 将窗口设置为可见。
setVisible(true);
}
/*
北边的部分,主要包含增删改查的按钮
*/
private JPanel northPanel() {
JPanel north = new JPanel();
// 组件的布局,5个按钮放到1行,5列
GridLayout grid = new GridLayout(1, 5);
north.setLayout(grid);
JButton addButton = new JButton("添加");
addButton.addActionListener(new AddAction()); // 添加按钮的监听事件
north.add(addButton); // 按钮放到JPanel上
JButton modifyButton = new JButton("修改");
modifyButton.addActionListener(new ModifyAction());
north.add(modifyButton);
JButton deleteButton = new JButton("删除");
deleteButton.addActionListener(new DeleteAction());
north.add(deleteButton);
// 搜索的输入框
searchText = new JTextField();
north.add(searchText);
JButton searchButton = new JButton("搜索");
searchButton.addActionListener(new SearchAction());
north.add(searchButton);
return north;
}
/*
中间的部分,设备的表格
*/
private JPanel centerPanel() {
JPanel center = new JPanel();
GridLayout grid = new GridLayout(1, 1);
center.setLayout(grid);
// 表格
table = new JTable();
// 设置单元格的属性
DefaultTableCellRenderer cr = new DefaultTableCellRenderer();
// 设置对齐方式,居中对齐。
cr.setHorizontalAlignment(JLabel.CENTER);
// 单元格的属性 与 表格 绑定到一起
table.setDefaultRenderer(Object.class, cr);
// 给表格中填充数据
refreshTable(table, SqliteDb.queryAll());
// JScrollPane是滚动面板,有横向纵向的滚动条
JScrollPane jScrollPane = new JScrollPane(table);
center.add(jScrollPane);
return center;
}
/*
刷新表格
java.awt.List java.util.List 中都有List类,名字冲突时带上包名。
*/
public static void refreshTable(JTable table, java.util.List<Equ> equs) {
// 表头
String[] biaoTou = {"编号", "设备名字", "位置", "是否报废",
"购买时间", "报废时间", "维修记录"};
// 将列表转成二维数组
String[][] equArrays = SqliteDb.list2Arrays(equs);
// JTable 用TableModel来保存数据
TableModel tm = table.getModel();
// TableModel 是接口,DefaultTableModel是它的一个实现类。
// TableModel 类型的变量强转成 DefaultTableModel
DefaultTableModel dtm = (DefaultTableModel) tm;
// 填充数据
dtm.setDataVector(equArrays, biaoTou);
}
/*
添加按钮的功能
*/
private class AddAction implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
// System.out.println("你点击了添加设备的按钮!");
// 打开添加设备的页面
new AddView();
}
}
/*
修改按钮的功能
*/
private class ModifyAction implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
// System.out.println("你点击了修改设备的按钮!");
// 获取当前选中行
int row = table.getSelectedRow();
// 获取该行设备信息
String id = (String) table.getValueAt(row, 0);
String name = (String) table.getValueAt(row, 1);
String location = (String) table.getValueAt(row, 2);
String baoFei = (String) table.getValueAt(row, 3);
String buyTime = (String) table.getValueAt(row, 4);
String baoFeiTime = (String) table.getValueAt(row, 5);
String record = (String) table.getValueAt(row, 6);
// 构造设备对象
Equ equ = new Equ(id, name, location, baoFei, buyTime, baoFeiTime, record);
// 打开修改的页面
new ModifyView(equ);
}
}
/*
删除按钮的功能
*/
private class DeleteAction implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
// System.out.println("你点击了删除设备的按钮!");
// 获取选中行
int[] rows = table.getSelectedRows();
System.out.println(Arrays.toString(rows));
String[] ids = new String[rows.length];
// 获取选中行设备的Id
for (int i = 0; i < rows.length; i++) {
ids[i] = (String) table.getValueAt(rows[i], 0); // 第0列为编号
}
System.out.println("选中行的编号为:" + Arrays.toString(ids));
// 弹出确认框,点击确认,则删除,点击取消,不删除。
int msg = JOptionPane.showConfirmDialog(null,
"请确认是否删除编号为" + Arrays.toString(ids) + "的设备?",
"删除确认", JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE);
System.out.println(msg); // 代表用户做的选择
if (msg == 0) { // 表示选择了yes
// 根据id删除设备
for (String id : ids) {
SqliteDb.deleteEqu(id);
}
// 刷新表格
refreshTable(table, SqliteDb.queryAll());
// 弹出提示
JOptionPane.showMessageDialog(null, "删除完成。");
}
}
}
/*
搜索按钮的功能
*/
private class SearchAction implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
// System.out.println("你点击了搜索按钮!");
java.util.List<Equ> equs = new ArrayList<>();
String temp = searchText.getText(); // 获取输入的内容
//如果输入的内容为空,全量搜索
if (temp == null || temp.equals("")) {
equs = SqliteDb.queryAll();
} else {
// 否则,根据名字搜索
equs = SqliteDb.queryByName(temp);
}
// 搜索的结果刷新到表格中
refreshTable(table, equs);
// 清空搜索框
searchText.setText("");
}
}
}
|