package equ.view; /* 頂層容器,從JFrame繼承 */ public class MainView extends JFrame { private static Object TableModel; JTextField searchText; // 搜索前面的輸入框 static JTable table ; // 表格 //在構(gòu)造方法中繪制頁(yè)面 public MainView(){ // 設(shè)備窗口的標(biāo)題 setTitle("設(shè)備管理系統(tǒng)"); //把中間層容器加到頂層容器上 add(northPanel(), BorderLayout.NORTH); add(centerPanel(),BorderLayout.CENTER); // 設(shè)置窗口的位置,xy是坐標(biāo),相對(duì)于屏幕左上角的位置 setBounds(200,200,1200,600); //將窗口設(shè)置為可見(jiàn) setVisible(true); } /* 北邊的部分,主要包括增刪改查的按鈕 */ private JPanel northPanel(){ JPanel north = new JPanel(); //組件的布局,五個(gè)按鈕放置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; } /* 中間的部分,設(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è)置居中對(duì)齊 cr.setHorizontalAlignment(JLabel.CENTER); // 單元格的屬性 與 表格 綁定到一起 table.setDefaultRenderer(Object.class,cr); //給表格中填充數(shù)據(jù) refreshTable(table, SqliteDb.queryAll()); // JScrollpane是滾動(dòng)面板,橫向縱向的滾動(dòng)條 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 = {"編號(hào)","設(shè)備名字","位置","是否報(bào)廢", "購(gòu)買時(shí)間","報(bào)廢時(shí)間","維修記錄"}; // 將列表轉(zhuǎn)成二維數(shù)組 String[][] equArrays = SqliteDb.list2Arrays(equs); //JTable 用TableModle來(lái)報(bào)存數(shù)據(jù) TableModel tm = table.getModel(); //TableModel 是接口, DefaultTableModel是它的實(shí)現(xiàn)類。 //TableModel DefaultTableModel dtm = (DefaultTableModel) tm; //填充數(shù)據(jù) dtm.setDataVector(equArrays,biaotou); } /* 添加按鈕的功能 */ private class AddAction implements ActionListener{ @Override public void actionPerformed(ActionEvent e) { // System.out.println("你點(diǎn)擊了添加設(shè)備的按鈕!"); // 打開(kāi)添加設(shè)備的頁(yè)面 new AddView(); } } /* 修改按鈕的功能 */ private class ModifyAction implements ActionListener{ @Override public void actionPerformed(ActionEvent e) { //System.out.println("你點(diǎn)擊了修改設(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è)備對(duì)象 Equ equ = new Equ(id,name,location,baoFei,buyTime,baoFeiTime,record); //打開(kāi)修改的頁(yè)面 new ModifyView(equ); } }
|