使用if语句从复选框获取事件侦听器

时间:2017-05-02 07:35:02

标签: java swing

在我的代码中我声明了复选框,如果选中了一个复选框,则使用组合框显示其内容。 问题是如果用户选择第一个复选框,它显示组合框(它重新发生事件),但如果他选择了另一个,则不会执行任何操作。 这是事件处理程序的代码。

private static class HandlerClass implements ItemListener {
  @Override

  public void itemStateChanged(ItemEvent event) {
      GridBagConstraints cc = new GridBagConstraints();
      cc.insets= new Insets(10,10,10,10);
    String[] colums= { "year","version","author","ssn"};
     String[] colums2= { "ID","Name","Degree","Mobile","Email"};
     String[] colums3={"Author_ID","Fname","Lname"};
     String[] colums4={"Admin username","admin password"};
     String[] colums5={"catogery_ID","catogery_name"};
     String[] colums6={"Author_ID","ISBN"};
     String[] colums7={"student_ID","book_ISBN","copyno","end_Date","start_date"};
     String[] colums8={"Author_ID","Fname","Lname"};
 if(checkbox.isSelected()){
    for(int j=0; j<=colums.length;j++){
        String s=colums[j];
        cc.gridx=0;
        cc.gridy=1;
     combo.addItem(s);

     panel.add(combo,cc);}}
    else if(checkbox.isSelected()==false){
        combo.removeAllItems();
       panel.remove(combo);}


      if(checkbox2.isSelected()) 
    { for(int j=0; j<=colums2.length;j++){

     combo2.addItem(colums2[j]);
        cc.gridx=1;
        cc.gridy=1;
     panel.add(combo2,cc);}}
    else if(checkbox2.isSelected()==false){
        combo2.removeAllItems();
       panel.remove(combo2);}

     if( checkbox3.isSelected())
    {
       for(int j=0; j<=colums3.length;j++){
     combo3.addItem(colums3[j]);
        cc.gridx=2;
        cc.gridy=1;
     panel.add(combo3,cc);}}
    else if(checkbox3.isSelected()==false){
        combo3.removeAllItems();
       panel.remove(combo3);}

if( checkbox4.isSelected())
    {
       for(int j=0; j<=colums4.length;j++){
     combo4.addItem(colums4[j]);
        cc.gridx=3;
        cc.gridy=1;
     panel.add(combo4,cc);}}
    else if(checkbox4.isSelected()==false){
        combo4.removeAllItems();
       panel.remove(combo4);}

 if( checkbox5.isSelected())
    {
       for(int j=0; j<=colums5.length;j++){
     combo5.addItem(colums5[j]);
        cc.gridx=1;
        cc.gridy=1;
     panel1.add(combo5,cc);}}
    else if(checkbox5.isSelected()==false){
        combo5.removeAllItems();
       panel.remove(combo5);}

  if( checkbox6.isSelected())
    {
       for(int j=0; j<=colums6.length;j++){
           cc.gridx=2;
           cc.gridy=1;
     combo6.addItem(colums6[j]);
     panel1.add(combo6,cc);}}
    else if(checkbox6.isSelected()==false){
        combo6.removeAllItems();
       panel.remove(combo6);}

   if( checkbox7.isSelected())
    {
       for(int j=0; j<=colums7.length;j++){
     combo7.addItem(colums7[j]);
        cc.gridx=3;
        cc.gridy=1;
     panel1.add(combo7,cc);}}
    else if(checkbox7.isSelected()==false){
        combo7.removeAllItems();
       panel.remove(combo7);}

    if( checkbox8.isSelected())
    {
       for(int j=0; j<=colums8.length;j++){
     combo8.addItem(colums4[j]);
        cc.gridx=4;
        cc.gridy=1;
     panel1.add(combo8,cc);}}
    else if(checkbox8.isSelected()==false){
        combo8.removeAllItems();
       panel.remove(combo8);}

  }
  }

0 个答案:

没有答案