简体   繁体   English

如何从Java中的另一个类调用combobox所选项目?

[英]how to call combobox selected item from another class in java?

final JComboBox departure = new JComboBox();
departure.setModel(new DefaultComboBoxModel(new String[] {"city1", "city2", "city3"}));
departure.setBounds(413, 11, 147, 20);
int selectedIndex1=departure.getSelectedIndex();
contentPane.add(departure);

I am coding a bus reservation system for my homework, I use JComboBox to choose destination and departure city. 我正在为自己的作业编写公交预订系统,我使用JComboBox选择目的地和出发城市。 I want to call selected item from another class. 我想从另一个班级中调用选定的项目。 In this class the user will choose his seat. 在此类中,用户将选择他的座位。

How can I call selected item from another class? 如何呼叫另一个班级的所选项目? Please help me.. thanks. 请帮助我..谢谢。

You could make your JComboBox a class member variable & add a method to return result of getSelectedItem : 您可以将JComboBox设为类成员变量,并添加方法以返回getSelectedItem结果:

public class MyGuiApp {

    private JComboBox comboBox;

    // constructor, init method, etc.

    public String getSelectedItem() {
       return (String)comboBox.getSelectedItem();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM