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; // 表格
// 在構(gòu)造方法中繪制頁面
public MainView() {
// 設(shè)置窗口的標(biāo)題
setTitle("設(shè)備管理系統(tǒng)");
// 把中間層容器加到頂層容器上。
add(northPanel(), BorderLayout.NORTH);
add(centerPanel(), BorderLayout.CENTER);
// 設(shè)置窗口的位置,xy是坐標(biāo)點,相對于屏幕左上角的位置。
setBounds(200, 100, 800, 600);
// 將窗口設(shè)置為可見。
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()); // 添加按鈕的監(jiān)聽事件
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;
}
/*
中間的部分,設(shè)備的表格
*/
private JPanel centerPanel() {
JPanel center = new JPanel();
GridLayout grid = new GridLayout(1, 1);
center.setLayout(grid);
// 表格
table = new JTable();
// 設(shè)置單元格的屬性
DefaultTableCellRenderer cr = new DefaultTableCellRenderer();
// 設(shè)置對齊方式,居中對齊。
cr.setHorizontalAlignment(JLabel.CENTER);
// 單元格的屬性 與 表格 綁定到一起
table.setDefaultRenderer(Object.class, cr);
// 給表格中填充數(shù)據(jù)
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 = {"編號", "設(shè)備名字", "位置", "是否報廢",
"購買時間", "報廢時間", "維修記錄"};
// 將列表轉(zhuǎn)成二維數(shù)組
String[][] equArrays = SqliteDb.list2Arrays(equs);
// JTable 用TableModel來保存數(shù)據(jù)
TableModel tm = table.getModel();
// TableModel 是接口,DefaultTableModel是它的一個實現(xiàn)類。
// TableModel 類型的變量強轉(zhuǎn)成 DefaultTableModel
DefaultTableModel dtm = (DefaultTableModel) tm;
// 填充數(shù)據(jù)
dtm.setDataVector(equArrays, biaoTou);
}
/*
添加按鈕的功能
*/
private class AddAction implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
// System.out.println("你點擊了添加設(shè)備的按鈕!");
// 打開添加設(shè)備的頁面
new AddView();
}
}
/*
修改按鈕的功能
*/
private class ModifyAction implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
// System.out.println("你點擊了修改設(shè)備的按鈕!");
// 獲取當(dāng)前選中行
int row = table.getSelectedRow();
// 獲取該行設(shè)備信息
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);
// 構(gòu)造設(shè)備對象
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("你點擊了刪除設(shè)備的按鈕!");
// 獲取選中行
int[] rows = table.getSelectedRows();
System.out.println(Arrays.toString(rows));
String[] ids = new String[rows.length];
// 獲取選中行設(shè)備的Id
for (int i = 0; i < rows.length; i++) {
ids[i] = (String) table.getValueAt(rows[i], 0); // 第0列為編號
}
System.out.println("選中行的編號為:" + Arrays.toString(ids));
// 彈出確認(rèn)框,點擊確認(rèn),則刪除,點擊取消,不刪除。
int msg = JOptionPane.showConfirmDialog(null,
"請確認(rèn)是否刪除編號為" + Arrays.toString(ids) + "的設(shè)備?",
"刪除確認(rèn)", JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE);
System.out.println(msg); // 代表用戶做的選擇
if (msg == 0) { // 表示選擇了yes
// 根據(jù)id刪除設(shè)備
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(); // 獲取輸入的內(nèi)容
//如果輸入的內(nèi)容為空,全量搜索
if (temp == null || temp.equals("")) {
equs = SqliteDb.queryAll();
} else {
// 否則,根據(jù)名字搜索
equs = SqliteDb.queryByName(temp);
}
// 搜索的結(jié)果刷新到表格中
refreshTable(table, equs);
// 清空搜索框
searchText.setText("");
}
}
}
|