package equ.view; /* 顶层容器,从JFrame继承 */ public class MainView extends JFrame { private static Object TableModel; JTextField searchText; // 搜索前面的输入框 static JTable table ; // 表格 //在构造方法中绘制页面 public MainView(){ // 设备窗口的标题 setTitle("设备管理系统"); //把中间层容器加到顶层容器上 add(northPanel(), BorderLayout.NORTH); add(centerPanel(),BorderLayout.CENTER); // 设置窗口的位置,xy是坐标,相对于屏幕左上角的位置 setBounds(200,200,1200,600); //将窗口设置为可见 setVisible(true); } /* 北边的部分,主要包括增删改查的按钮 */ private JPanel northPanel(){ JPanel north = new JPanel(); //组件的布局,五个按钮放置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(); searchText.addActionListener(new SearchTextAction()); 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.util.List<Equ> equs */ public static void refreshTable(JTable table, java.util.List<Equ> equs) { String[] biaotou = {"编号","设备名字","位置","是否报废", "购买时间","报废时间","维修记录"}; // 将列表转成二维数组 String[][] equArrays = SqliteDb.list2Arrays(equs); //JTable 用TableModle来报存数据 TableModel tm = table.getModel(); //TableModel 是接口, DefaultTableModel是它的实现类。 //TableModel 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); } }
|