Java 将多个JCombox、JButton转换为一个集合<;字符串>;有效地

Java 将多个JCombox、JButton转换为一个集合<;字符串>;有效地,java,swing,collections,set,jcombobox,Java,Swing,Collections,Set,Jcombobox,我的程序使用几个JComboxes和JButton来允许用户显示数据的方式和内容。直到今天,我都是这样做的: if (e.getSource() == someMenü_or_Button){ if(someMenü_or_Button.getSelectedItem()=="showstuff")display.oneOfManySetter(0); } 现在我想我可以收集setattributes=newhashset()中的所有选项 但以我的方式,

我的程序使用几个JComboxes和JButton来允许用户显示数据的方式和内容。直到今天,我都是这样做的:

if (e.getSource() == someMenü_or_Button){
        if(someMenü_or_Button.getSelectedItem()=="showstuff")display.oneOfManySetter(0);            
}
现在我想我可以收集
setattributes=newhashset()中的所有选项

但以我的方式,结果不是代码效率更高,也没有更好的性能,因为我做到了:

public void actionPerformed(ActionEvent e) { 
attributes.clear();

if(someMenü1.getSelectedItem()== "sum"); 
else attributes.add(someMenü.getSelectedItem());
//and so on.

attributes.add(someButton1.getName());
//and so on 

//And in addition:
if (e.getSource() == someButton1){
        if(someButton1.getText()=="option1")original.setText("option2");
        else someButton1.setText("option1");
 }
因此,我的问题是,我能否以某种方式将JComboBox转换为集合,并(我知道这是可能的)将其从集合中删除?然后,我将再次添加JComboBox的选定项

我知道这些按钮的问题是我应该使用开关

编辑16:12

要将此作为我想做的代码示例,请执行以下操作:

//somewhere
private Set<String> attributes = new HashSet<String>();
JComboBox<String> menu;
String[] values = {"option1","option2","option3","option4"};
panel.add (menu = new JComboBox<>(values),gbc);
menu.addActionListener(this);    

public void actionPerformed(ActionEvent e) {

     if(e.getSource() == menu){
           attributes.removeAll(menu.getCollection());     //HOW??????
           attributes.add(menu.getSelectedItem());
     }
}
//某处
私有集属性=新HashSet();
JComboBox菜单;
字符串[]值={“option1”、“option2”、“option3”、“option4”};
panel.add(菜单=新JComboBox(值),gbc);
menu.addActionListener(这个);
已执行的公共无效操作(操作事件e){
如果(如getSource()==菜单){
attributes.removeAll(menu.getCollection());//如何??????
add(menu.getSelectedItem());
}
}

我不确定您为什么要转换菜单。值数组中已经有键。我理解错了什么吗?这个小例子是你想做的吗

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

import javax.swing.JComboBox;
import javax.swing.JFrame;

public class SomeClass extends JFrame implements ActionListener {

    private Set<String> attributes = new HashSet<>();
    private String[] values = { "a", "b", "c", "d" };
    private JComboBox<String> menu = new JComboBox<>(values);

    public SomeClass() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        init();
        pack();
        setVisible(true);
    }

    public static void main(String[] args) {
        new SomeClass();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // attributes.add();
        if (e.getSource() instanceof JComboBox<?>) {
            JComboBox<?> menu = (JComboBox<?>) e.getSource();
            String s = (String) menu.getSelectedItem();
            attributes.removeAll(Arrays.asList(values));
            attributes.add(s);
            System.out.println(s);
            System.out.println(attributes);
        }
    }

    private void init() {
        menu.addActionListener(this);
        add(menu);
    }

}
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.util.array;
导入java.util.HashSet;
导入java.util.Set;
导入javax.swing.JComboBox;
导入javax.swing.JFrame;
公共类SomeClass扩展JFrame实现ActionListener{
私有集属性=新HashSet();
私有字符串[]值={“a”、“b”、“c”、“d”};
私有JComboBox菜单=新JComboBox(值);
公共类(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
init();
包装();
setVisible(真);
}
公共静态void main(字符串[]args){
新类();
}
@凌驾
已执行的公共无效操作(操作事件e){
//attributes.add();
if(例如,JComboBox的getSource()实例){
JComboBox菜单=(JComboBox)e.getSource();
字符串s=(字符串)菜单。getSelectedItem();
attributes.removeAll(Arrays.asList(values));
属性。添加;
系统输出打印项次;
System.out.println(属性);
}
}
私有void init(){
menu.addActionListener(这个);
添加(菜单);
}
}

您能提供一个更完整的示例吗?我看不出这里有什么明显的图案。此外,格式也很糟糕……这很危险:
someMenüu或_按钮。getSelectedItem()=“showtuff”
。我知道这不是您的主要问题,但将字符串与
==
进行比较是危险的代码,因为
==
之后,这些代码通常会在某个时间中断=比较一个对象引用是否与另一个对象引用相同,并且您不关心这一点。相反,您关心的是两个字符串变量是否具有相同的字符串表示形式,为此,请使用
equals(…)
equalsIgnoreCase(…)
方法。可能比为所有操作使用一个ActionListener,然后尝试使用交换机逻辑来决定它应该做什么更好,将使用匿名内部类ActionListener,并使这些侦听器变得简单,也许让它们简单地调用控制类的一个方法。但即使使用此解决方案,我认为操作一个集合也会有一些“优雅”。然后检查一些数据是否是该集合的子集->显示或不显示。至于从组合框中获取项目,您可以从组合框的模型中获取所有显示的项目,因为它有允许您迭代的方法,可以提取每个项目。组合框本身有一个获取所选项目数组的方法。请在API中查找这些内容,因为它将向您展示所有内容。好吧,我认为这是一个愚蠢的问题,是我的糟糕代码的萌芽;)无论如何,我认为你的答案是正确的,但我必须将
值和
菜单从init()中删除。