0

我的 JComboBoxes 有 9 个(+ 默认)可选选项(20 个都一样)。
这 20 个是 2 个 JComboBox 数组的一部分。(每个 10-10 个)。我想像这样限制它们:
如果从(例如)选项 4 中选择了 4 个并且用户选择了其中的第 5
个,其中一个会跳回默认值以保持 4 的限制。

我该怎么做 ?
例如:

for (int i = 0; i < roles1.length; i++) {
            roles1[i] = new JComboBox();
            roles1[i].setModel(new DefaultComboBoxModel(new String[] { "Not Selected", "PartnerInCrime", "Driver",
                    "Gunman", "Coordinator", "Hacker", "Distraction", "GadgetGuy", "Bruglar", "ConMan" }));
            roles1[i].setBounds(boxPlace, 200, 100, 20);
            boxPlace += 105;
            getFrame().getContentPane().add(roles1[i]);

        }
4

2 回答 2

2

这是一个建议,应该会引导您朝着正确的方向前进。

首先,您必须为每个 ComboBox 添加一个 ActionListener,它调用一个方法,将所有选择与当前选择进行比较。

roles1[i].addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {

       // get the current selection
       JComboBox currentBox = (JComboBox)e.getSource();
       String currentSelection = (String)currentBox.getSelectedItem();

       // call the method and hand in your current selection
       checkForSelectionExceeding(currentSelection);            

    }
});

在您的扫描方法中,您应该在扫描时记住匹配的数量。如果超出限制,请将当前框重置为默认值并停止扫描。像这样的东西:

private void checkForSelectionExceeding(String currentSelection){
   int matches = 0;

   for(int i=0; i<roles1.length; i++) {
      if(roles1[i].getSelectedItem().equals(currentSelection)) {
         matches++;
      }

      if(matches > 4) {
         roles1[i].setSelectedItem("Not Selected");
         break;
      }
   }
}

您只需稍微重构此解决方案即可按顺序扫描两个阵列。

希望这可以帮助。

于 2017-04-20T21:30:12.197 回答
1

如果我正确理解你的问题,我有一个想法,你可以从:

// create global HashMap that can records the occurrence of the selection of each item
Map<String, Integer> reference = new HashMap<String, Integer>(); 
// populate it
reference.put("PartnerInCrime", 0);
reference.put("Driver", 0);
reference.put("Gunman", 0);
reference.put("Coordinator", 0);
reference.put("Hacker", 0);
reference.put("Distraction", 0);
reference.put("GadgetGuy", 0);
reference.put("Bruglar", 0);
reference.put("ConMan", 0);

// then for every JComboBox in your array -> add action item listener to observe and control the selection like this


for(JComboBox<String> jcb : roles1){
    jcb.addItemListener(new ItemListener(){
      public void itemStateChanged(ItemEvent ie){
        if(ie.getStateChange() == ItemEvent.DESELECTED){ // decrement record
           if(!ie.getItem().toString().equals("Not Selected")){
             reference.put(ie.getItem().toString(), reference.get(ie.getItem().toString()) -1);
           }
        }
         else if(ie.getStateChange() == ItemEvent.SELECTED){
           if(!ie.getItem().toString().equals("Not Selected")){
             if(reference.get(ie.getItem().toString())>=4){ // if already selected 4 of them
                jcb.setSelectedIndex(0); // return to the default
              } 
              else{ // else record the selection 
                   reference.put(ie.getItem().toString(), reference.get(ie.getItem().toString()) +1);
              }    
          } 
        }
      }
   });
}
于 2017-04-20T21:30:58.893 回答