gpt4 book ai didi

java - JComboBox 选择变量

转载 作者:行者123 更新时间:2023-12-02 07:08:54 25 4
gpt4 key购买 nike

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

     //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 中选择的选项有:“Critical”、“Major”和“Minor”。

如何获得它,以便如果用户从组合框中选择“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);
}
}

关于java - JComboBox 选择变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15774607/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com