目前我有两个复选框,一个显示文本:“数字成绩”,另一个显示:“字母成绩”。我还有一个带有两个字符串的 JComboBox(一个是数字,另一个是字母)。因此,我的目标是,当我选择数字成绩时,JComoBox 会向我显示数字字符串,反之亦然。 我是java新手,虽然我花了大约两天的时间试图弄清楚如何做到这一点,但它没有成功,所以我希望你能帮助我。
英语不是我的母语,所以如果我写错了什么,我会很高兴您纠正它。
public void dropdownlist () {
JComboBox list;
boolean checked = rb1.getState();
String[] numbers = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};
String[] letters = {"F", "D-", "D", "D+", "C-", "C", "C+", "B-", "B", "B+", "A-", "A"};
if(rb1.getState()) {
list = new JComboBox(numbers);
panel.add(list);
System.out.println("rb1 selected");
} else {
list = new JComboBox(letters);
panel.add(list);
System.out.println("rb2 selected");
}
list.addActionListener((ActionEvent e) -> {
});
}
这就是我到目前为止所拥有的。
最佳答案
我认为您在问“如何显示正确的组合框”。我猜你有一个 JComboBox,里面有 JRadioButtons。如果是这种情况,那么类似:
{
...
ActionListener selectedActionListener = new ActionListener() {
public void actionPerformed(ActionEvent ae)
{
AbstractButton ab = (AbstractButton) ae.getSource();
if ("Letter Grade".equals(ab.getText())
{
numericGradeComboBox.setVisible(false);
letterGradeComboBox.setVisible(true);
}
else if ("Numeric Grade".equals(ab.getText())
{
letterGradeComboBox.setVisible(false);
numericGradeComboBox.setVisible(true);
}
}};
numericGradeComboBox.setActionListener(selectedActionListener);
letterGradeComboBox.setActionListener(selectedActionListener);
....
}
可能有用,但如果我们能看到一些代码,会更容易提供帮助。
关于java - 如何制作 if 语句来选择要在 JComboBox 中显示的字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32237880/