单击jComboBox中的项目时,如何在jTextField中设置值?
就像当我在jComboBox中选择Platinum时,jTextField将显示其值,例如15%或Gold,并将jTextField设置为10%
membox.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
if(e.getStateChange() == ItemEvent.SELECTED) {
jTextField6.setText((String) membox.getSelectedItem());
}
}
});
我卡在这里,它只显示我在jComboBox中单击的内容,将不胜感激,谢谢您提前
最佳答案
建立课程
public class ItemClass {
private String name;
private String value;
public ItemClass(String name1, String value1) {
name = name1;
value = value1;
}
@Override
public String toString()
{
return name;
}
public String getValue() {
return value;
}
}
并在您的代码中添加此代码...
ItemClass oro = new ItemClass("gold","10%");
ItemClass platino = new ItemClass("platinum","15%");
JComboBox jc = new JComboBox();
jc = membox;
jc.addItem(oro);
jc.addItem(platino);
membox.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
if(e.getStateChange() == ItemEvent.SELECTED) {
Object obj=(Object) membox.getSelectedItem();
ItemClass itemclass=(ItemClass)obj;
String value = itemclass.getValue();
jTextField6.setText(value);
}
}
});