Java m来接受这些值,可能是在JButton的ActionListener中,然后如果选择了两个相同的值,则取消选择它们并用JOptionPane警告用户。或者,您可以将监听器(Actionlistener或ItemListener)中的代码添加到两个JComboBox中,检查另一个组合框的选定值以确保它们不相同,如果是,则警告用户并取消选择错误的选择。

Java m来接受这些值,可能是在JButton的ActionListener中,然后如果选择了两个相同的值,则取消选择它们并用JOptionPane警告用户。或者,您可以将监听器(Actionlistener或ItemListener)中的代码添加到两个JComboBox中,检查另一个组合框的选定值以确保它们不相同,如果是,则警告用户并取消选择错误的选择。,java,swing,jcombobox,itemlistener,Java,Swing,Jcombobox,Itemlistener,因此这是完全可能的。我认为最好的方法是检查两个框的选定索引,然后根据该索引值的结果执行逻辑。在下面的示例中,您可以看到这一点。现在的情况是ComboBox1和Combox2上有一个ActionListener。如果值匹配,我使用索引,但您也可以获取字符串值以确保它们不匹配 public class SOF extends JFrame { private JPanel mainPanel, comboPanel; private JComboBox jcb1, jcb2; public SOF

因此这是完全可能的。我认为最好的方法是检查两个框的选定索引,然后根据该索引值的结果执行逻辑。在下面的示例中,您可以看到这一点。现在的情况是ComboBox1和Combox2上有一个ActionListener。如果值匹配,我使用索引,但您也可以获取字符串值以确保它们不匹配

public class SOF extends JFrame {
private JPanel mainPanel, comboPanel;
private JComboBox jcb1, jcb2;

public SOF()
{
    super("Combo Box Example");
    mainPanel = new JPanel();
    mainPanel.add(configureCombo());
    add(mainPanel);
    setSize(200,200);
    setLocationRelativeTo(null);
    setVisible(true);


    jcb1.addActionListener(new ActionListener() {
       @Override
       public void actionPerformed(ActionEvent e)
       {
           //You can replace this with, jcb2.getSelectedItem if you don't know the indexes or if they will be random. 
           if((jcb1.getSelectedIndex() == 2) && (jcb2.getSelectedIndex() == 0))
           {
               JOptionPane.showMessageDialog(null, "Cannot select 3 in both field.");
               jcb1.setSelectedIndex(-1);
               jcb2.setSelectedIndex(-1);
           }
            //ANOTHER OPTION
           If((jcb1.getSelectedValue().equals("three") && (jcb2.getSelectedValue().equals("three")
           {
            LOGIC
           }
       }
    });

    jcb2.addActionListener(new ActionListener() {
       @Override
       public void actionPerformed(ActionEvent e)
       {
           //You can replace this with, jcb2.getSelectedItem if you don't know the indexes or if they will be random. 
           if((jcb2.getSelectedIndex() == 0) && (jcb1.getSelectedIndex() == 2))
           {
               JOptionPane.showMessageDialog(null, "Cannot select 3 in both field.");
               jcb1.setSelectedIndex(-1);
               jcb2.setSelectedIndex(-1);
           }
       }
    });


}

private JPanel configureCombo()
{
    String[] cb1List = {"one", "two", "three"};
    String[] cb2List = {"three", "four"};
    comboPanel = new JPanel(new GridLayout(1,2));
    jcb1 = new JComboBox(cb1List);
    jcb2 = new JComboBox(cb2List);
    comboPanel.add(jcb1);
    comboPanel.add(jcb2);
    return comboPanel;
}
/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    SOF s = new SOF();
}

}

如果选择了任何一个匹配框,将弹出错误消息并取消选择两个组合框

所以这绝对是可能的。我认为最好的方法是检查两个框的选定索引,然后根据该索引值的结果执行逻辑。在下面的示例中,您可以看到这一点。现在的情况是ComboBox1和Combox2上有一个ActionListener。如果值匹配,我使用索引,但您也可以获取字符串值以确保它们不匹配

public class SOF extends JFrame {
private JPanel mainPanel, comboPanel;
private JComboBox jcb1, jcb2;

public SOF()
{
    super("Combo Box Example");
    mainPanel = new JPanel();
    mainPanel.add(configureCombo());
    add(mainPanel);
    setSize(200,200);
    setLocationRelativeTo(null);
    setVisible(true);


    jcb1.addActionListener(new ActionListener() {
       @Override
       public void actionPerformed(ActionEvent e)
       {
           //You can replace this with, jcb2.getSelectedItem if you don't know the indexes or if they will be random. 
           if((jcb1.getSelectedIndex() == 2) && (jcb2.getSelectedIndex() == 0))
           {
               JOptionPane.showMessageDialog(null, "Cannot select 3 in both field.");
               jcb1.setSelectedIndex(-1);
               jcb2.setSelectedIndex(-1);
           }
            //ANOTHER OPTION
           If((jcb1.getSelectedValue().equals("three") && (jcb2.getSelectedValue().equals("three")
           {
            LOGIC
           }
       }
    });

    jcb2.addActionListener(new ActionListener() {
       @Override
       public void actionPerformed(ActionEvent e)
       {
           //You can replace this with, jcb2.getSelectedItem if you don't know the indexes or if they will be random. 
           if((jcb2.getSelectedIndex() == 0) && (jcb1.getSelectedIndex() == 2))
           {
               JOptionPane.showMessageDialog(null, "Cannot select 3 in both field.");
               jcb1.setSelectedIndex(-1);
               jcb2.setSelectedIndex(-1);
           }
       }
    });


}

private JPanel configureCombo()
{
    String[] cb1List = {"one", "two", "three"};
    String[] cb2List = {"three", "four"};
    comboPanel = new JPanel(new GridLayout(1,2));
    jcb1 = new JComboBox(cb1List);
    jcb2 = new JComboBox(cb2List);
    comboPanel.add(jcb1);
    comboPanel.add(jcb2);
    return comboPanel;
}
/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    SOF s = new SOF();
}

}

如果选择了任何一个匹配框,将弹出错误消息并取消选择两个组合框

在构建JComboxes的DefaultListModels时,检查重复项并将其删除将更容易、更方便用户。我在回答中添加了更多信息,以便为您提供新的附加问题。在构建JComboxes的DefaultListModels时,检查重复项并将其删除将更容易、更方便用户JComboxes。我在回答中添加了更多信息,以适应新的附加问题。我想,我不能使用getSelectedIndex()方法,因为当有人(将来)更改元素顺序或元素名称时,一切都会爆炸。。。我说的对吗?没错,您还可以使用getSelectedValue(),它允许您比较元素中包含的字符串值。请看我编辑的答案。我想,我不能使用getSelectedIndex()方法,因为当有人(将来)更改元素顺序或元素名称时,一切都会爆炸。。。我说的对吗?没错,您还可以使用getSelectedValue(),它允许您比较元素中包含的字符串值。请参阅我编辑过的答案。@JacobRutka我在答案中添加了更多信息,以便为您提供新的附加问题。在您的代码中-当我运行应用程序并将框2中的选定值更改为“四”时,框1中的两个值将为“三”。。。因为我在box2中设置了after run selected value等于“三”。@JacobRutka我在一小时前刚注销时就想到了这个问题。您能告诉我您是如何初始化
JComboBox
的吗?或者您是如何在您的程序中添加
ComboBox
模型的?所以我可以给这个问题提供一个具体的解决方案。@JacobRutka我已经更正了你的问题的答案。@JacobRutka我在我的答案中添加了更多信息,以适应你的新问题。在你的代码中-当我运行应用程序并将框2中的选定值更改为“四”时,框1中的两个值将为“三”。。。因为我在box2中设置了after run selected value等于“三”。@JacobRutka我在一小时前刚注销时就想到了这个问题。您能告诉我您是如何初始化
JComboBox
的吗?或者您是如何在您的程序中添加
ComboBox
模型的?这样我就可以对这个问题给出一个具体的解决办法。@JacobRutka我已经纠正了我对你问题的回答。
void boxItemSelected(ItemEvent e){

    //Check the item selected/deselected is "three" else do nothing.
    if (e.getItem().equals("three")){ 

    //Find the box on which this action was not performed to change its model.
        JComboBox<String> oppositeBox;
        if(e.getSource().equals(box1)){
            oppositeBox = box2;
        }else{
            oppositeBox = box1;
        }

    //Check the item is selected or deselected to remove or add "three" to item list.
        if(e.getStateChange() == ItemEvent.SELECTED){
            oppositeBox.removeItem("three");
        }else{
            oppositeBox.addItem("three");
        }
    }
}
if (e.getItem().equals("three"))
if (e.getItem().equals("three") || e.getItem().equals("<new item>") || ....)
oppositeBox.removeItem("three");
oppositeBox.addItem("three");
oppositeBox.removeItem(e.getItem());
oppositeBox.addItem(e.getItem());
List<JComboBox<?>> boxes = new ArrayList<>();
List<String> items = new Vector<>();
items.add("One");
items.add("Two");
......
   Iterator<JComboBox<?>> iterator = boxes.iterator();
   while(iterator.hasNext()){
       JComboBox<?> existing = iterator.next();
       items.remove(existing.getSelectedItem());
   }
box.setModel(new DefaultComboBoxModel(items));
boxes.add(box);
box.addItemListener(new ItemListener(){

    itemStateChanged(ItemEvent e){
        boxItemSelected(e);
    }
});
void boxItemSelected(ItemEvent e){

    //Create an iterator to iterate over the boxes 
    Iterator<JComboBox<?>> iterator = boxes.iterator();

    while(iterator.hasNext()){

        //Get the current instance of comboBox from the list
        JComboBox<?> current = iterator.next();

        //If the box in which the select or de-select 
        //event has occurred is the current comboBox then do nothing.
        if(e.getSource().equals(current)(
            continue;
        }

        //If the event is select then remove the Item from the 
        //current comboBox else add the Item to the current comboBox.
        if(e.getStateChange() == ItemEvent.SELECTED){
            current.removeItem(e.getItem());
        }else{
            current.addItem(e.getItem());
        }
    }
}
public class SOF extends JFrame {
private JPanel mainPanel, comboPanel;
private JComboBox jcb1, jcb2;

public SOF()
{
    super("Combo Box Example");
    mainPanel = new JPanel();
    mainPanel.add(configureCombo());
    add(mainPanel);
    setSize(200,200);
    setLocationRelativeTo(null);
    setVisible(true);


    jcb1.addActionListener(new ActionListener() {
       @Override
       public void actionPerformed(ActionEvent e)
       {
           //You can replace this with, jcb2.getSelectedItem if you don't know the indexes or if they will be random. 
           if((jcb1.getSelectedIndex() == 2) && (jcb2.getSelectedIndex() == 0))
           {
               JOptionPane.showMessageDialog(null, "Cannot select 3 in both field.");
               jcb1.setSelectedIndex(-1);
               jcb2.setSelectedIndex(-1);
           }
            //ANOTHER OPTION
           If((jcb1.getSelectedValue().equals("three") && (jcb2.getSelectedValue().equals("three")
           {
            LOGIC
           }
       }
    });

    jcb2.addActionListener(new ActionListener() {
       @Override
       public void actionPerformed(ActionEvent e)
       {
           //You can replace this with, jcb2.getSelectedItem if you don't know the indexes or if they will be random. 
           if((jcb2.getSelectedIndex() == 0) && (jcb1.getSelectedIndex() == 2))
           {
               JOptionPane.showMessageDialog(null, "Cannot select 3 in both field.");
               jcb1.setSelectedIndex(-1);
               jcb2.setSelectedIndex(-1);
           }
       }
    });


}

private JPanel configureCombo()
{
    String[] cb1List = {"one", "two", "three"};
    String[] cb2List = {"three", "four"};
    comboPanel = new JPanel(new GridLayout(1,2));
    jcb1 = new JComboBox(cb1List);
    jcb2 = new JComboBox(cb2List);
    comboPanel.add(jcb1);
    comboPanel.add(jcb2);
    return comboPanel;
}
/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    SOF s = new SOF();
}

}