摆动复选框

时间:2015-01-30 06:54:05

标签: java swing jcheckbox

当运行以下代码并选中Beta复选框时,则选中Alpha复选框,文本显示"选中复选框:Alpha,Beta"不是"选中的复选框:Beta,Alpha"。为什么它们的选择顺序与它们的选择顺序相反?

// Demonstrate check boxes. 

import java.awt.*;  
import java.awt.event.*;  
import javax.swing.*;  

class CBDemo implements ItemListener {  

  JLabel jlabSelected; 
  JLabel jlabChanged; 
  JCheckBox jcbAlpha; 
  JCheckBox jcbBeta; 
  JCheckBox jcbGamma; 

  CBDemo() {  
    // Create a new JFrame container.  
    JFrame jfrm = new JFrame("Demonstrate Check Boxes");  

    // Specify FlowLayout for the layout manager. 
    jfrm.setLayout(new FlowLayout()); 

    // Give the frame an initial size.  
    jfrm.setSize(280, 120);  

    // Terminate the program when the user closes the application.  
    jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  

    // Create empty labels. 
    jlabSelected = new JLabel(""); 
    jlabChanged = new JLabel("");  

    // Make check boxes. 
    jcbAlpha = new JCheckBox("Alpha");  
    jcbBeta = new JCheckBox("Beta");  
    jcbGamma = new JCheckBox("Gamma");  

    // Events generated by the check boxes 
    // are handled in common by the itemStateChanged() 
    // method implemented by CBDemo. 
    jcbAlpha.addItemListener(this); 
    jcbBeta.addItemListener(this); 
    jcbGamma.addItemListener(this); 

    // Add checkboxes and labels to the content pane.  
    jfrm.add(jcbAlpha);   
    jfrm.add(jcbBeta);   
    jfrm.add(jcbGamma);   
    jfrm.add(jlabChanged);  
    jfrm.add(jlabSelected);  

    // Display the frame.  
    jfrm.setVisible(true);  
  }  

  // This is the handler for the check boxes.   
  public void itemStateChanged(ItemEvent ie) { 
    String str = ""; 

    // Obtain a reference to the check box that 
    // caused the event. 
    JCheckBox cb = (JCheckBox) ie.getItem(); 

    // Report what check box changed. 
    if(cb.isSelected())  
      jlabChanged.setText(cb.getText() + " was just selected."); 
    else 
      jlabChanged.setText(cb.getText() + " was just cleared."); 

    // Report all selected boxes. 
    if(jcbAlpha.isSelected()) { 
      str += "Alpha "; 
    }  
    if(jcbBeta.isSelected()) { 
      str += "Beta "; 
    } 
    if(jcbGamma.isSelected()) { 
      str += "Gamma"; 
    } 

    jlabSelected.setText("Selected check boxes: " + str); 
  } 

  public static void main(String args[]) {  
    // Create the frame on the event dispatching thread.  
    SwingUtilities.invokeLater(new Runnable() {  
      public void run() {  
        new CBDemo();  
      }  
    });  
  }  
}

2 个答案:

答案 0 :(得分:2)

当单击任何复选框时,调用itemStateChanged(),字符串的顺序由代码中str + =语句的顺序驱动,而不是单击的时间顺序。

if(jcbAlpha.isSelected()) { 
  str += "Alpha "; 
}  
if(jcbBeta.isSelected()) { 
  str += "Beta "; 
} 
if(jcbGamma.isSelected()) { 
  str += "Gamma"; 
} 

达到理想的行为

  • 将选择事件存储在某种有序结构中,例如, itemStateChanged更新然后显示的列表。
  • 为每个复选框使用不同的ItemListener实例,或使用ItemEvent参数确定事件的来源以相应地更新结构

尝试将3 ifs更改为单个:

if (cb.isSelected()) { 
  selectionOrder.add(cb.getText()); // will return Alpha, Beta depending which is selected
}  

jlabSelected.setText("Selected check boxes: " + selectionOrder); 

其中selectionOrder是CBDemo类顶部的字段

private List<String> selectionOrder = new ArrayList<String>();

这显然会无限期地增加列表,但对于演示来说很好。

答案 1 :(得分:0)

由于您将值附加到String的顺序是      Alpha --then - &gt; Beta - 然后 - &gt; Gamma

// Report all selected boxes. 
if(jcbAlpha.isSelected()) { 
  str += "Alpha "; 
}  
if(jcbBeta.isSelected()) { 
  str += "Beta "; 
} 
if(jcbGamma.isSelected()) { 
  str += "Gamma"; 
} 

所以无论您选择复选框的顺序都是无关紧要的。

要获得所需的输出,请使用

// Report all selected boxes. 
if(jcbAlpha.isSelected()) { 
  str += "Alpha "; 
  jcbAlpha.setSelected(false);// So when Next Time you click on other checkbox this condtion does not append result to Str
}  
if(jcbBeta.isSelected()) { 
  str += "Beta "; 
  jcbBeta.setSelected(false);
} 
if(jcbGamma.isSelected()) { 
  str += "Gamma"; 
 jcbGamma.setSelected(false);
}