package equ.view;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
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);
//设置窗口的位置
setBounds(400,200,1200,600);
//将窗口设为可见
setVisible(true);
}
/*
北边的部分,主要包含增删改查的按钮
*/
private JPanel northPanel(){
JPanel north = new JPanel();//组件
//组件的布局,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 = (String) table.getValueAt(rows,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,"删除完成 |