📄 bjsetframe.java
字号:
/*zysetFrame.java专业设置窗口 * * @author 方景 */import java.awt.*;import java.awt.event.*;import javax.swing.*;import java.sql.*;public class bjsetFrame extends JFrame implements ActionListener{ public final static int TO_INSEART=1,TO_UPDATE=2; int insql; DataBaseManager db=new DataBaseManager(); ResultSet rs; JLabel labels[],label; JComboBox boxs[]; JTextField fields[],field; JButton bt[]; String labelNames[]={"系名称","专业名称","年级名称"}; String tlabels[]={"班级编号","班级名称","班主任","班级人数"}; String btn[]={"自动生成按钮","确定按钮","取消按钮"}; String xName="",zName="",nName="",bName="",nNo=""; int counter=1,i; JPanel p1,p2,p3,p4,p5,p6; Box box; /** Creates a new instance of bjsetFrame */ public bjsetFrame(int inseartOrUpdate) { super("班级设置"); insql=inseartOrUpdate; p1=new JPanel(); p2=new JPanel(); p3=new JPanel(); p4=new JPanel(); p5=new JPanel(); p6=new JPanel(); box=Box.createVerticalBox(); p1.setLayout(new GridLayout(4,1,0,15)); p2.setLayout(new GridLayout(4,1,0,10)); labels=new JLabel[3]; boxs=new JComboBox[3]; bt=new JButton[3]; for(int i=0;i<3;i++){ labels[i]=new JLabel(labelNames[i]); p1.add(labels[i]); boxs[i]=new JComboBox(); p2.add(boxs[i]); bt[i]=new JButton(btn[i]); bt[i].addActionListener(this); } try{ rs=db.getResult("Select xName From yuanxi"); while(rs.next()){ boxs[0].addItem(rs.getString("xName")); } }catch(SQLException e){ System.out.println("在执行数据库访问时发生了错误22!"); e.printStackTrace(); } for(int i=0;i<3;i++){ boxs[i].addActionListener(this); } label=new JLabel("班级数"); field=new JTextField(6); p3.add(label); p3.add(field); box.add(Box.createVerticalStrut(25)); box.add(p3); box.add(Box.createVerticalStrut(25)); box.add(bt[0]); box.add(Box.createVerticalStrut(25)); fields=new JTextField[4]; for(int i=0;i<4;i++){ fields[i]=new JTextField(10); fields[i].setText(tlabels[i]); p4.add(fields[i]); } p5.add(bt[1]); p5.add(bt[2]); p6.setLayout(new BorderLayout()); p6.add(p1,BorderLayout.WEST); p6.add(p2,BorderLayout.CENTER); p6.add(box,BorderLayout.EAST); Container c=getContentPane(); c.add(p6,BorderLayout.NORTH); c.add(p4,BorderLayout.CENTER); c.add(p5,BorderLayout.SOUTH); } /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here bjsetFrame b=new bjsetFrame(bjsetFrame.TO_INSEART); b.pack(); b.show(); } public void actionPerformed(ActionEvent e) { if(e.getSource()==bt[2]){ db.closeConnection(); this.dispose(); } else if(e.getSource()==boxs[0]){ xName=((String)(boxs[0].getItemAt(boxs[0].getSelectedIndex()))).trim(); boxs[1].removeActionListener(this); boxs[1].removeAllItems(); try{ rs=db.getResult("Select zName From yuanxi y,zhuanye z where xName="+"'"+xName.trim()+"'and y.xNo=z.xNo"); while(rs.next()){ boxs[1].addItem(rs.getString("zName")); } }catch(SQLException ex){ System.out.println("在执行数据库访问时发生了错误11!"); ex.printStackTrace(); } boxs[1].addActionListener(this); } else if(e.getSource()==boxs[1]){ zName=((String)(boxs[1].getItemAt(boxs[1].getSelectedIndex()))).trim(); boxs[2].removeActionListener(this); boxs[2].removeAllItems(); try{ rs=db.getResult("Select nName From zhuanye z,nianji n where zName="+"'"+zName+"'and z.zNo=n.zNo"); while(rs.next()){ boxs[2].addItem(rs.getString("nName")); } }catch(SQLException ex){ System.out.println("在执行数据库访问时发生了错误11!"); ex.printStackTrace(); } boxs[2].addActionListener(this); } else if(e.getSource()==boxs[2]){ nName=((String)(boxs[2].getItemAt(boxs[2].getSelectedIndex()))).trim(); try{ rs=db.getResult("Select nNo From nianji where nName="+"'"+nName+"'"); while(rs.next()){ nNo=rs.getString("nNo"); } }catch(SQLException ex){ System.out.println("在执行数据库访问时发生了错误222221!"); ex.printStackTrace(); } } else if(e.getSource()==bt[0]){ i=Integer.parseInt(field.getText().trim()); fields[0].setText(nNo.trim()+1); fields[1].setText(zName+nName+1+"班"); counter++; } else if(e.getSource()==bt[1]){ if(insql==1){ fields[0].setText(nNo.trim()+counter); fields[1].setText(zName+nName+counter+"班"); String sql=""; if(counter<=i){ try{ String strSQL="insert into banji(bNo,bName,bManager,bRenShu,nNo) values('"+ fields[0].getText().trim()+"','"+ fields[1].getText().trim()+"','"+ fields[2].getText().trim()+"',"+ fields[3].getText().trim()+",'"+ nNo.trim()+"')"; System.out.println(strSQL); if(db.updateSql(strSQL)){ JOptionPane.showMessageDialog(null,"添加信息成功!"); //this.dispose(); } else{ JOptionPane.showMessageDialog(null,"添加信息失败请认真填写各项信息!"); //this.dispose(); } db.closeConnection(); }catch(Exception ex){ System.out.println(ex.toString()); } } } //--修改窗口功能-- else{ try{ String strSQL="update banji set bNo='"+ fields[0].getText().trim()+"',"+ "bName='"+fields[1].getText().trim()+"',"+ "bManager='"+fields[1].getText().trim()+"',"+ "nNo='"+nNo.trim()+ " where bNo='"+fields[0].getText().trim()+"'"; if(db.updateSql(strSQL)){ JOptionPane.showMessageDialog(null,"修改信息成功!"); //this.dispose(); } else{ JOptionPane.showMessageDialog(null,"添加信息失败请认真填写各项信息!"); //this.dispose(); } db.closeConnection(); }catch(Exception ex){ System.out.println(ex.toString()); } } } } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -