如何从java中的另一个类调用combobox选中的项?

-1 java variables swing jcombobox

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);
Run Code Online (Sandbox Code Playgroud)

我正在为我的作业编写公交预订系统,我用它JComboBox来选择目的地和出发城市.我想从另一个班级调用所选项目.在这个课程中,用户将选择他的座位.

如何从其他课程中调用所选项目?请帮帮我..谢谢.

Rei*_*eus 6

你可以创建JComboBox一个类成员变量并添加一个方法来返回结果getSelectedItem:

public class MyGuiApp {

    private JComboBox comboBox;

    // constructor, init method, etc.

    public String getSelectedItem() {
       return (String)comboBox.getSelectedItem();
    }
}
Run Code Online (Sandbox Code Playgroud)