package com.jnzyjsxy.node1; import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.Font; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.JTextField; import javax.swing.ListSelectionModel; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; import com.jnzyjsxy.common.ArticleBean; import com.jnzyjsxy.common.Department; import com.jnzyjsxy.common.TeacherBean; //import com.jnzyjsxy.common.HistrinBeanb; //import com.jnzyjsxy.common.PersonBean; import com.jnzyjsxy.test.KyMain; import com.jnzyjsxy.test.text; /* * 人员删除管理 */ public class Node11Panel extends JPanel implements ListSelectionListener,ActionListener,ItemListener{ //创建上中下面板 JPanel upPanel =new JPanel(); JPanel centerPanel=new JPanel(); JPanel downpanel =new JPanel(); //定义图形界面元素 String DeptID="1";//部门编号 String teacher="1"; JComboBox jComboBox11=null;//显示人员所在部门 JComboBox jComboBox12=null;//显示人员所在部门 JLabel jLabel=new JLabel(); JLabel jLabel1=new JLabel(); JLabel jLabel2=new JLabel(); JLabel jLabel3=new JLabel(); JLabel jLabel4=new JLabel(); JLabel jLabel5=new JLabel(); JLabel jLabel6=new JLabel(); JLabel jLabel7=new JLabel(); JLabel jLabel8=new JLabel(); JLabel jLabel9=new JLabel(); JLabel jLabel10=new JLabel(); JLabel jLabel11=new JLabel(); //JLabel jLabel3=new JLabel(); JTextField jTextField1=new JTextField(15); JTextField jTextField2=new JTextField(15); JTextField jTextField3=new JTextField(15); JTextField jTextField4=new JTextField(15); JTextField jTextField5=new JTextField(15); JTextField jTextField6=new JTextField(15); JTextField jTextField7=new JTextField(15); JTextField jTextField8=new JTextField(15); JTextField jTextField9=new JTextField(15); JTextField jTextField10=new JTextField(15); JTextField jTextField11=new JTextField(15); JButton hq=new JButton(); JButton addInfo=new JButton(); JButton modifyInfo=new JButton(); JButton deleteInfo=new JButton(); JButton clearInfo=new JButton(); JScrollPane jScrollPane1; JScrollPane jScrollPane2; //创建网格包布局对象 GridBagLayout girdBag=new GridBagLayout(); //创建网格包约束类变量 GridBagConstraints girdBagCon; //定义表格变量 JTable jTable; //ListSelectionModel列表选中模式类 用于设置JTable中记录选中的模式 ListSelectionModel ListSelectionModel=null; //字符数组 存储列明 对应person 列名 String[] colName={"编号","题目","作者","期刊/出版社","级别","发表时间","第几期","字数","部门编号","统计日期","备注"}; //二维数组 存储每一行的记录纸行存储记录编号 列存储该记录各列值 String[][] colValue; GridBagLayout girBag =new GridBagLayout(); GridBagConstraints gridBagCon; //构造方法 public Node11Panel() { // TODO Auto-generated constructor stub this.setLayout(new BorderLayout());//边框布局 try { upInit();//上 centenInit(); //panelInit panelInit=new panelInit(); //this.add(panelInit,BorderLayout.CENTER);//中 //downInit();//下 addListener();//监听 } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } } //上布局 public void upInit() { //创建人员数据操作类 以便调用该类方法 ArticleBean bean=new ArticleBean(); //上部采用网格包不几乎 放置人员信息删除标签和包含表格的滚动面板 upPanel.setLayout(girBag); try { //在上部添加标签 jLabel.setText("论文信息管理"); jLabel.setFont(new Font("Dialog", 0, 16)); gridBagCon=new GridBagConstraints(); gridBagCon.gridx=0; gridBagCon.gridy=0; //外部填充 4个参数 top,left,bottom,right //既外部间距 gridBagCon.insets=new Insets(0, 10, 0, 10); girBag.setConstraints(jLabel, gridBagCon); upPanel.add(jLabel); //调用Person类的searchAll 方法 获取所有的人员记录 colValue=bean.searchAllForNode(); //初始化表对象 2个参数 下面是JTable的构造 //JTable(Object[][] rowData, Object[] columnNames) // 构造一个 JTable 来显示二维数组 rowData 中的值,其列名称为 columnNames //即第1个参数二维数组存储每一行的值,第2个参数一维数组存放列名称 jTable=new JTable(colValue,colName); //设置表的大小 jTable.setPreferredScrollableViewportSize(new Dimension(750,350)); //初始化表格选择模式对象 ListSelectionModel=jTable.getSelectionModel(); //设置表格为单选模式 ListSelectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); //添加选择监听事件 ListSelectionModel.addListSelectionListener(this); //把表格放进滚动栏 jScrollPane1=new JScrollPane(jTable); //设置滚动面板大小 jScrollPane1.setPreferredSize(new Dimension(750, 350)); //把滚动面板放在标签的下面 gridBagCon=new GridBagConstraints(); gridBagCon.gridx=0; gridBagCon.gridy=1; gridBagCon.insets=new Insets(0, 0, 0, 0); girBag.setConstraints(jScrollPane1, gridBagCon); upPanel.add(jScrollPane1); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } //添加上部面板 this.add(upPanel,BorderLayout.NORTH); } //中部面板 public void centenInit() throws Exception{ //中间放组件的面板 采用网格包布局 centerPanel.setLayout(girdBag); //第一行组件 //griy=1 实际的第二行 显示4个组件 gridx依次增加 jLabel1.setText("论文编号"); jLabel1.setFont(new Font("Dialog", 0, 12)); girdBagCon=new GridBagConstraints(); girdBagCon.gridx=0; girdBagCon.gridy=1; //外部填充 4个参数 top,left,bottom,right //既外部间距 girdBagCon.insets=new Insets(0, 10, 10, 1); girdBag.setConstraints(jLabel1, girdBagCon); centerPanel.add(jLabel1); girdBagCon=new GridBagConstraints(); girdBagCon.gridx=1; girdBagCon.gridy=1; girdBagCon.insets=new Insets(0, 1, 10, 15); girdBag.setConstraints(jTextField1, girdBagCon); centerPanel.add(jTextField1); jLabel2.setText("论文题目"); jLabel2.setFont(new Font("Dialog", 0, 12)); girdBagCon=new GridBagConstraints(); girdBagCon.gridx=2; girdBagCon.gridy=1; //外部填充 4个参数 top,left,bottom,right //既外部间距 girdBagCon.insets=new Insets(0, 15, 10, 1); girdBag.setConstraints(jLabel2, girdBagCon); centerPanel.add(jLabel2); girdBagCon=new GridBagConstraints(); girdBagCon.gridx=3; girdBagCon.gridy=1; girdBagCon.insets=new Insets(0, 1, 10, 10); girdBag.setConstraints(jTextField2, girdBagCon); centerPanel.add(jTextField2); jLabel3.setText("作者"); jLabel3.setFont(new Font("Dialog", 0, 12)); girdBagCon=new GridBagConstraints(); girdBagCon.gridx=4; girdBagCon.gridy=1; //外部填充 4个参数 top,left,bottom,right //既外部间距 girdBagCon.insets=new Insets(10, 10, 10, 1); girdBag.setConstraints(jLabel3, girdBagCon); centerPanel.add(jLabel3); TeacherBean teacherBean=new TeacherBean(); String[] alltype=teacherBean.searchAllForNode(); //jComboBox11.setPreferredSize(new Dimension(125, 25)); jComboBox11=new JComboBox(alltype); girdBagCon=new GridBagConstraints(); girdBagCon.gridx=5; girdBagCon.gridy=1; girdBagCon.insets=new Insets(10, 1, 10, 15); girdBag.setConstraints(jComboBox11, girdBagCon); centerPanel.add(jComboBox11); jLabel4.setText("期刊名称"); jLabel4.setFont(new Font("Dialog", 0, 12)); girdBagCon=new GridBagConstraints(); girdBagCon.gridx=0; girdBagCon.gridy=2; //外部填充 4个参数 top,left,bottom,right //既外部间距 girdBagCon.insets=new Insets(10, 15, 10, 1); girdBag.setConstraints(jLabel4, girdBagCon); centerPanel.add(jLabel4); girdBagCon=new GridBagConstraints(); girdBagCon.gridx=1; girdBagCon.gridy=2; girdBagCon.insets=new Insets(10, 1, 10, 10); girdBag.setConstraints(jTextField4, girdBagCon); centerPanel.add(jTextField4); jLabel5.setText("级别"); jLabel5.setFont(new Font("Dialog", 0, 12)); girdBagCon=new GridBagConstraints(); girdBagCon.gridx=2; girdBagCon.gridy=2; //外部填充 4个参数 top,left,bottom,right //既外部间距 girdBagCon.insets=new Insets(10, 10, 10, 1); girdBag.setConstraints(jLabel5, girdBagCon); centerPanel.add(jLabel5); girdBagCon=new GridBagConstraints(); girdBagCon.gridx=3; girdBagCon.gridy=2; girdBagCon.insets=new Insets(10, 1, 10, 15); girdBag.setConstraints(jTextField5, girdBagCon); centerPanel.add(jTextField5); jLabel6.setText("发表年份"); jLabel6.setFont(new Font("Dialog", 0, 12)); girdBagCon=new GridBagConstraints(); girdBagCon.gridx=4; girdBagCon.gridy=2; //外部填充 4个参数 top,left,bottom,right //既外部间距 girdBagCon.insets=new Insets(10, 15, 10, 1); girdBag.setConstraints(jLabel6, girdBagCon); centerPanel.add(jLabel6); girdBagCon=new GridBagConstraints(); girdBagCon.gridx=5; girdBagCon.gridy=2; girdBagCon.insets=new Insets(10, 1, 10, 10); girdBag.setConstraints(jTextField6, girdBagCon); centerPanel.add(jTextField6); //第四行显示组件 gridy=4 gridx 依次增加 jLabel7.setText("刊期"); jLabel7.setFont(new Font("Dialog", 0, 12)); girdBagCon=new GridBagConstraints(); girdBagCon.gridx=0; girdBagCon.gridy=3; //外部填充 4个参数 top,left,bottom,right //既外部间距 girdBagCon.insets=new Insets(10, 10, 10, 1); girdBag.setConstraints(jLabel7, girdBagCon); centerPanel.add(jLabel7); girdBagCon=new GridBagConstraints(); girdBagCon.gridx=1; girdBagCon.gridy=3; girdBagCon.insets=new Insets(10, 1, 10, 10); girdBag.setConstraints(jTextField7, girdBagCon); centerPanel.add(jTextField7); jLabel8.setText("字数"); jLabel8.setFont(new Font("Dialog", 0, 12)); girdBagCon=new GridBagConstraints(); girdBagCon.gridx=2; girdBagCon.gridy=3; //外部填充 4个参数 top,left,bottom,right //既外部间距 girdBagCon.insets=new Insets(10, 10, 10, 1); girdBag.setConstraints(jLabel8, girdBagCon); centerPanel.add(jLabel8); girdBagCon=new GridBagConstraints(); girdBagCon.gridx=3; girdBagCon.gridy=3; girdBagCon.insets=new Insets(10, 1, 10, 10); girdBag.setConstraints(jTextField8, girdBagCon); centerPanel.add(jTextField8); jLabel9.setText("所在系部"); jLabel9.setFont(new Font("Dialog", 0, 12)); girdBagCon=new GridBagConstraints(); girdBagCon.gridx=4; girdBagCon.gridy=3; //外部填充 4个参数 top,left,bottom,right //既外部间距 girdBagCon.insets=new Insets(10, 10, 10, 1); girdBag.setConstraints(jLabel9, girdBagCon); centerPanel.add(jLabel9); Department department=new Department(); String[] s=department.searchAllForNode(); //jComboBox12.setPreferredSize(new Dimension(125, 25)); jComboBox12=new JComboBox(s); girdBagCon=new GridBagConstraints(); girdBagCon.gridx=5; girdBagCon.gridy=3; girdBagCon.insets=new Insets(10, 1, 10, 10); girdBag.setConstraints(jComboBox12, girdBagCon); centerPanel.add(jComboBox12); jLabel10.setText("录入日期"); jLabel10.setFont(new Font("Dialog", 0, 12)); girdBagCon=new GridBagConstraints(); girdBagCon.gridx=0; girdBagCon.gridy=4; //外部填充 4个参数 top,left,bottom,right //既外部间距 girdBagCon.insets=new Insets(10, 10, 10, 1); girdBag.setConstraints(jLabel10, girdBagCon); centerPanel.add(jLabel10); girdBagCon=new GridBagConstraints(); girdBagCon.gridx=1; girdBagCon.gridy=4; girdBagCon.insets=new Insets(10, 1, 10, 10); girdBag.setConstraints(jTextField10, girdBagCon); centerPanel.add(jTextField10); jLabel11.setText("备注"); jLabel11.setFont(new Font("Dialog", 0, 12)); girdBagCon=new GridBagConstraints(); girdBagCon.gridx=2; girdBagCon.gridy=4; //外部填充 4个参数 top,left,bottom,right //既外部间距 girdBagCon.insets=new Insets(10, 10, 10, 1); girdBag.setConstraints(jLabel11, girdBagCon); centerPanel.add(jLabel11); girdBagCon=new GridBagConstraints(); girdBagCon.gridx=3; girdBagCon.gridy=4; girdBagCon.insets=new Insets(10, 1, 10, 10); girdBag.setConstraints(jTextField11, girdBagCon); centerPanel.add(jTextField11); //定义部门操作类 //DeptBean dBean=new DeptBean(); //调用方法获取所有部门的字符串 //String[] alltype=dBean.searchAllForNode(); //jComboBox1=new JComboBox(alltype); //girdBagCon=new GridBagConstraints(); //girdBagCon.gridx=1; //girdBagCon.gridy=4; //girdBagCon.gridwidth=3; //girdBagCon.gridheight=1; //girdBagCon.insets=new Insets(10, 1, 10, 15); //girdBag.setConstraints(jComboBox1, girdBagCon); //centerPanel.add(jComboBox1); hq.setText("获取新编号"); hq.setFont(new Font("Dialog", 0, 12)); girdBagCon=new GridBagConstraints(); girdBagCon.gridx=0; girdBagCon.gridy=5; girdBagCon.gridwidth=2; girdBagCon.gridheight=1; //外部填充 4个参数 top,left,bottom,right //既外部间距 girdBagCon.insets=new Insets(10, 10, 10, 10); girdBag.setConstraints(hq, girdBagCon); centerPanel.add(hq); addInfo.setText("增加"); addInfo.setFont(new Font("Dialog", 0, 12)); girdBagCon=new GridBagConstraints(); girdBagCon.gridx=1; girdBagCon.gridy=5; girdBagCon.gridwidth=2; girdBagCon.gridheight=1; //外部填充 4个参数 top,left,bottom,right //既外部间距 girdBagCon.insets=new Insets(10, 10, 10, -100); girdBag.setConstraints(addInfo, girdBagCon); centerPanel.add(addInfo); modifyInfo.setText("修改"); modifyInfo.setFont(new Font("Dialog", 0, 12)); girdBagCon=new GridBagConstraints(); girdBagCon.gridx=2; girdBagCon.gridy=5; //girdBagCon.gridwidth=2; //girdBagCon.gridheight=1; //外部填充 4个参数 top,left,bottom,right //既外部间距 girdBagCon.insets=new Insets(10, 10, 10, -150); girdBag.setConstraints(modifyInfo, girdBagCon); centerPanel.add(modifyInfo); deleteInfo.setText("删除"); deleteInfo.setFont(new Font("Dialog", 0, 12)); girdBagCon=new GridBagConstraints(); girdBagCon.gridx=3; girdBagCon.gridy=5; //girdBagCon.gridwidth=2; //girdBagCon.gridheight=1; //外部填充 4个参数 top,left,bottom,right //既外部间距 girdBagCon.insets=new Insets(10, 10, 10, -150); girdBag.setConstraints(deleteInfo, girdBagCon); centerPanel.add(deleteInfo); deleteInfo.addActionListener((ActionListener) this); clearInfo.setText("清空"); clearInfo.setFont(new Font("Dialog", 0, 12)); girdBagCon=new GridBagConstraints(); girdBagCon.gridx=4; girdBagCon.gridy=5; //girdBagCon.gridwidth=2; //girdBagCon.gridheight=1; //外部填充 4个参数 top,left,bottom,right //既外部间距 girdBagCon.insets=new Insets(10, 10, 10, -170); girdBag.setConstraints(clearInfo, girdBagCon); centerPanel.add(clearInfo); this.add(centerPanel,BorderLayout.CENTER); jTextField1.setEditable(false); deleteInfo.setEnabled(false); modifyInfo.setEnabled(false); addInfo.setEnabled(false); } public void addListener() throws Exception{ //删除按钮 活动监听 deleteInfo.addActionListener(this); hq.addActionListener(this); addInfo.addActionListener(this); modifyInfo.addActionListener(this); clearInfo.addActionListener(this); jComboBox11.addItemListener(this); jComboBox12.addItemListener(new ItemListener() { @Override public void itemStateChanged(ItemEvent e) { // TODO Auto-generated method stub if (e.getStateChange()==ItemEvent.SELECTED) { //获取选中项目值,得到的是一个“1-办公室-秘书室”这样的字符串 //字符串构成:部门编号-一级部门-二级部门 System.out.println("321"); String temStr=""+e.getItem(); //返回指定子字符串"-"在此字符串中第一次出现处的索引 int i=temStr.indexOf("-"); //取部门id子字符串,目的是取前面的部门编号 DeptID=temStr.substring(0,i); } } }); } public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub Object obj=e.getSource(); ArticleBean art=new ArticleBean(); if (obj==hq) { jTextField1.setText(Integer.toString(art.getId())); jTextField2.setText(""); jTextField4.setText(""); jTextField5.setText(""); jTextField6.setText(""); jTextField7.setText(""); jTextField8.setText(""); jTextField10.setText(""); jTextField11.setText(""); addInfo.setEnabled(true); deleteInfo.setEnabled(false); modifyInfo.setEnabled(false); } else if(obj==clearInfo) { jTextField1.setText(""); jTextField2.setText(""); jTextField4.setText(""); jTextField5.setText(""); jTextField6.setText(""); jTextField7.setText(""); jTextField8.setText(""); jTextField10.setText(""); jTextField11.setText(""); deleteInfo.setEnabled(false); modifyInfo.setEnabled(false); } else if (obj==deleteInfo) { ArticleBean articleBean=new ArticleBean(); articleBean.delete(jTextField1.getText()); Node11Panel node13Panel=new Node11Panel(); KyMain.splitPane.setRightComponent(node13Panel); } else if (obj==addInfo) { art.add(jTextField2.getText(),teacher, jTextField4.getText(), jTextField5.getText(), jTextField6.getText(), jTextField7.getText(), jTextField8.getText(),DeptID,jTextField10.getText(), jTextField11.getText()); Node11Panel node13Panel=new Node11Panel(); KyMain.splitPane.setRightComponent(node13Panel); } else if (obj==modifyInfo) { art.modify(jTextField1.getText(), jTextField2.getText(), teacher, jTextField4.getText(), jTextField5.getText(), jTextField6.getText(), jTextField7.getText(), jTextField8.getText(), DeptID, jTextField10.getText(), jTextField11.getText()); Node11Panel node13Panel=new Node11Panel(); KyMain.splitPane.setRightComponent(node13Panel); } //重新生成界面 目的是删除记录后 显示删除后的剩余记录 jTable.revalidate();//重新布局 } @Override public void valueChanged(ListSelectionEvent e) { // TODO Auto-generated method stub //获取表选中的行的索引号 int[] selecteRow=jTable.getSelectedRows(); int[] selecteCol=jTable.getSelectedColumns(); //定义文本框的现实内容 //既在上面显示人员表中选中一条记录 下面上颚文本框中分别年是该记录的 for (int i = 0; i < selecteRow.length; i++) { for (int j = 0; j < selecteCol.length; j++) { jTextField1.setText(colValue[selecteRow[i]][0]); jTextField2.setText(colValue[selecteRow[i]][1]); jTextField4.setText(colValue[selecteRow[i]][3]); jTextField5.setText(colValue[selecteRow[i]][4]); jTextField6.setText(colValue[selecteRow[i]][5]); jTextField7.setText(colValue[selecteRow[i]][6]); jTextField8.setText(colValue[selecteRow[i]][7]); jTextField10.setText(colValue[selecteRow[i]][9]); jTextField11.setText(colValue[selecteRow[i]][10]); } } deleteInfo.setEnabled(true); modifyInfo.setEnabled(true); hq.setEnabled(true); } public void itemStateChanged(ItemEvent e) { // TODO Auto-generated method stub //ItemEvent--项目事件,例如下拉列表框中项被选定或取消选定的事件 //e.getStateChange()--表示选定或取消选定 //ItemEvent.SELECTED--表示选定 //ItemEvent.DESELECTED--表示取消选定 //如果在下拉列表框中选中一个部门 //TeacherBean teacherBean=new TeacherBean(); if (e.getStateChange()==ItemEvent.SELECTED) { //获取选中项目值,得到的是一个“1-办公室-秘书室”这样的字符串 //字符串构成:部门编号-一级部门-二级部门 System.out.println("123"); String temStr=""+e.getItem(); //返回指定子字符串"-"在此字符串中第一次出现处的索引 int i=temStr.indexOf("-"); //取部门id子字符串,目的是取前面的部门编号 teacher=temStr.substring(0,i); } } }

庞玉栋个人博客、人生苦短-我用Python