private void setTestState(boolean state) { textID.setEditable(state); textLocation.setEditable(state); OK.setEnabled(state); cancel.setEnabled(state); }
private void initComponents() { int change = 30; title.setBounds(449, 27, 148, 39); title.setFont(fnt1); labelID.setBounds(100, 180 - change, 131, 30); textID.setBounds(206, 184 - change, 144, 24); labelLocation.setBounds(86, 300 - change, 131, 30); textLocation.setBounds(206, 303 - change, 144, 24); labelID.setFont(fnt); labelLocation.setFont(fnt); userId.setLocation(389, 62); userId.setSize(180, 25); userRole.setLocation(514, 62); userRole.setSize(180, 25); OK.setBounds(86, 533 - change, 120, 40); cancel.setBounds(230, 533 - change, 120, 40); butOut.setBounds(52, 36, 120, 40); butAdd.setBounds(114, 632 - change, 120, 40); butDel.setBounds(336, 632 - change, 120, 40); butFind.setBounds(557, 632 - change, 120, 40); butChange.setBounds(779, 632 - change, 120, 40); this.add(title); this.add(labelID); this.add(textID); this.add(labelLocation); this.add(textLocation); this.add(OK); this.add(cancel); this.add(butOut); this.add(butAdd); this.add(butDel); this.add(butFind); this.add(butChange); this.add(userId); this.add(userRole); String[] columnNames = {"机构编号", "机构所处城市"}; int[] list = {40, 272, 14, 30, 20, 384, 126 - change, 561, 465}; // list里面参数分别为需要的列数,每一列的宽度,设置第一行字体大小,设置第一行行宽, // * 剩下行的行宽,表格setbounds(list[5],list[6], list[7], list[8]) // * table = new Table(); add(table.drawTable(columnNames, list)); InformationFind findInfo = new InformationFind(); ArrayList<InstitutionVO> ins = findInfo.findInstitution(); for (int i = 0; i < ins.size(); i++) { table.setValueAt(i, 1, ins.get(i).getLocation()); table.setValueAt(i, 0, ins.get(i).getID()); } }
private void addListener() { butOut.addMouseListener( new MouseAdapter() { public void mouseClicked(MouseEvent e) { mainFrame.setContentPane(new LoginUi(mainFrame)); } }); butAdd.addMouseListener( new MouseAdapter() { public void mouseClicked(MouseEvent e) { // 设置输入框可编辑 setTestState(true); isAdd = true; } }); butDel.addMouseListener( new MouseAdapter() { public void mouseClicked(MouseEvent e) { int currentLine = table.table.getSelectedRow(); if (currentLine == -1) { JOptionPane.showMessageDialog(InstitutionManageUi.this, "请选择要删除的行!"); } else { int i = table.numOfEmpty(); InformationDelete dele = new InformationDelete(); dele.deleteInstitution(table.getValueAt(currentLine, 0).trim()); for (int j = currentLine; j < i; j++) { table.setValueAt(j, 0, table.getValueAt(j + 1, 0)); table.setValueAt(j, 1, table.getValueAt(j + 1, 1)); } } } }); butFind.addMouseListener( new MouseAdapter() { public void mouseClicked(MouseEvent e) { String inputValue = JOptionPane.showInputDialog(InstitutionManageUi.this, "请输入机构编号:"); int i = table.numOfEmpty(); for (i = i - 1; i >= 0; i--) { if (table.table.getValueAt(i, 0).equals(inputValue)) { break; } } if (i >= 0) { table.table.setRowSelectionInterval(i, i); } else { if (inputValue != null) { JOptionPane.showMessageDialog(InstitutionManageUi.this, "未找到该机构!"); } } } }); butChange.addMouseListener( new MouseAdapter() { public void mouseClicked(MouseEvent e) { // 设置输入框可编辑 setTestState(true); isUpdate = true; // 将被选中机构的详细信息显示出来 int currentLine = table.table.getSelectedRow(); InformationFind find = new InformationFind(); InstitutionVO inst = find.findTheInstitution(table.getValueAt(currentLine, 0)); textID.setText(inst.getID()); textLocation.setText(inst.getLocation()); originalID = textID.getText(); } }); OK.addMouseListener( new MouseAdapter() { public void mouseClicked(MouseEvent e) { if (isAdd) { if (isComplete()) { // 获得第几行为空 int i = table.numOfEmpty(); // 在界面上显示新增机构信息 table.setValueAt(i, 0, textID.getText()); table.setValueAt(i, 1, textLocation.getText()); // 在数据库中存入新增机构信息 InstitutionVO ivo = new InstitutionVO(textID.getText(), textLocation.getText()); InformationAdd add = new InformationAdd(); add.addInstitution(ivo); // 清空输入框 empty(); // 使输入框不可编辑 setTestState(false); isAdd = false; } else { JOptionPane.showMessageDialog(InstitutionManageUi.this, "请将必填信息填写完整"); } } if (isUpdate) { int currentLine = table.table.getSelectedRow(); // 在数据库中修改该机构信息 InstitutionVO ivo = new InstitutionVO(textID.getText(), textLocation.getText()); if (!originalID.equals(textID.getText())) { InformationDelete de = new InformationDelete(); de.deleteInstitution(originalID); } InformationUpdate update = new InformationUpdate(); update.updateInstitution(ivo); // 在界面上修改该机构信息 table.setValueAt(currentLine, 0, textID.getText()); table.setValueAt(currentLine, 1, textLocation.getText()); // 清空输入框 empty(); // 使输入框不可编辑 setTestState(false); isUpdate = false; } } }); cancel.addMouseListener( new MouseAdapter() { public void mouseClicked(MouseEvent e) { // 清空输入框 empty(); // 设置输入框不可编辑 setTestState(false); } }); }