Java JComboBox选择到一个变量

Java JComboBox选择到一个变量,java,swing,actionlistener,jcombobox,Java,Swing,Actionlistener,Jcombobox,这是一个相当愚蠢的问题,所以对你来说应该是一个简单的答案。我这里有一些代码: //Severity Row severity = new JLabel("Severity:"); c.fill = GridBagConstraints.HORIZONTAL; c.gridx = 0; c.gridy = 4; c.gridwidth = 1; pane.add(severity, c); severityBox =

这是一个相当愚蠢的问题,所以对你来说应该是一个简单的答案。我这里有一些代码:

     //Severity Row
     severity = new JLabel("Severity:");
     c.fill = GridBagConstraints.HORIZONTAL;
     c.gridx = 0;
     c.gridy = 4;
     c.gridwidth = 1;
     pane.add(severity, c);

     severityBox = new JComboBox(SEVERITY);
     c.fill = GridBagConstraints.HORIZONTAL;
     c.gridx = 1;
     c.gridy = 4;
     c.gridwidth = 1;
     pane.add(severityBox, c);
     severityBox.addActionListener(this);
用户可在JComboBox中选择的选项有:“关键”、“主要”和“次要”

我如何获得它,以便如果用户从组合框中选择“Major”,我可以让它打印“red”,而不是使用getSelectedItem()打印“Major”


提前感谢您的帮助

只需更改要返回的值:

private String sValue;
@Override
public void actionPerformed(ActionEvent evt)
{
  if (evt.getSource() == severityBox )
  {
     sValue = (String)severityBox.getSelectedItem();
     if ( "Major".equals(sValue))
     {
        sValue = "Red";
     }
    System.out.println(sValue);
  }
}
建议使用表示“状态名称”和“状态颜色”的特定对象。例如,此类可能如下所示:

class Item {

    private String name;
    private String color;

    public Item(String name, String color) {
        this.name = name;
        this.color = color;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    @Override
    public String toString() {
        return name;
    }
}
现在,您可以使用上述类的实例构建combobox。请看我的例子:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

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

public class SourceCodeProgram {

    public static void main(String argv[]) throws Exception {
        JComboBox<Item> comboBox = new JComboBox<Item>(new Item[] {
                new Item("Major", "red"), new Item("Critical", "dark"),
                new Item("Minor", "green") });
        comboBox.addActionListener(new ActionListener() {

            @SuppressWarnings("unchecked")
            @Override
            public void actionPerformed(ActionEvent e) {
                JComboBox<Item> comboBox = (JComboBox<Item>) e.getSource();
                Item item = (Item) comboBox.getSelectedItem();
                System.out.println(item.getColor());
            }
        });
        JFrame frame = new JFrame();
        frame.add(comboBox);
        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入javax.swing.JComboBox;
导入javax.swing.JFrame;
公共类源代码程序{
公共静态void main(字符串argv[])引发异常{
JComboBox组合框=新JComboBox(新项[]{
新项目(“主要”、“红色”)、新项目(“关键”、“深色”),
新项目(“次要”、“绿色”)};
comboBox.addActionListener(新ActionListener(){
@抑制警告(“未选中”)
@凌驾
已执行的公共无效操作(操作事件e){
JComboBox组合框=(JComboBox)e.getSource();
Item=(Item)组合框。getSelectedItem();
System.out.println(item.getColor());
}
});
JFrame=新JFrame();
frame.add(组合框);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
如您所见,我将颜色和名称绑定到一个类中。我创建了这个类的3个实例,并将其传递到
JComboBox
构造函数中。
我们可以使用类来链接这些属性,但我认为特定类是最好的解决方案