当我点击jComboBox中的Item时,如何在jTextField中设置值?

时间:2021-07-22 15:50:04

How do i set the value in jTextField when i click an Item in jComboBox? Like when i select Platinum in jComboBox the jTextField will display its value like 15% or Gold and set the jTextField to 10%

当我点击jComboBox中的Item时,如何在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());
    }
}

});

});

im stuck here it only displays what i clicked in the jComboBox any help will be so much appreciated thank you in advance

我卡在这里它只显示我在jComboBox中点击的任何帮助将非常感谢提前谢谢

1 个解决方案

#1


1  

Create a class

创建一个类

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;
}
}

and add this code in yours...

并在你的...中添加此代码

    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);
    }
}

}); 

#1


1  

Create a class

创建一个类

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;
}
}

and add this code in yours...

并在你的...中添加此代码

    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);
    }
}

});