如何限制 20 个 JCombobox 的 1 个选择选项的最大选择数? javafor ex
How can I limit the number of maximum selections for 1 selecting option of 20 JCombobox? javafor ex
我的 JComboBox 有 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]);
}
这里有一个建议可以引导您朝着正确的方向前进。
首先,您必须向每个 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;
}
}
}
您只需稍微重构此解决方案即可按顺序扫描两个数组。
希望对您有所帮助。
如果我正确理解你的问题,我有一个想法,你可以从:
// 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);
}
}
}
}
});
}
我的 JComboBox 有 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]);
}
这里有一个建议可以引导您朝着正确的方向前进。
首先,您必须向每个 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;
}
}
}
您只需稍微重构此解决方案即可按顺序扫描两个数组。
希望对您有所帮助。
如果我正确理解你的问题,我有一个想法,你可以从:
// 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);
}
}
}
}
});
}