顶层容器 , 从 JFrame 继承
public class MainView extends JFrame {
JTextField searchText;//搜索前面的输入框
static JTable table;//表格
// 在构造方法里绘制页面
public MainView(){
//设置标题
setTitle("设备管理系统");
//把中间层容器加到顶层容器中
add(northPanle(), BorderLayout.NORTH);
add(centerPanel(),BorderLayout.CENTER);
//设置窗口的位置 xy 是坐标点,屏幕左上角是(0,0)
setBounds(400,200,1200,600);
// 将窗口设置为可见
setVisible(true);
}
北边的部分,主要包含增删改查按钮
private JPanel northPanle(){
JPanel north = new JPanel();
// 组件的布局,5个按钮放到1行,5列 GridLayout布局管理器
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) {
new AddView();
}
}
//修改按钮的功能
private class ModifyAction implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
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_CANCEL_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("")){
SqliteDb.queryAll();
}else{
// 否则,根据名字搜索
equs = SqliteDb.queryByName(temp);
}
// 搜索的结果刷新到表格
refreshTable(table,equs);
//清空搜索框
searchText.setText("");
}
} |