如何从Java中的另一个类调用combobox所选项目? [英] how to call combobox selected item from another class in java?

查看:96
本文介绍了如何从Java中的另一个类调用combobox所选项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

I am coding a bus reservation system for my homework, I use JComboBox to choose destination and departure city. 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.

推荐答案

您可以将JComboBox设为类成员变量&添加方法以返回getSelectedItem的结果:

You could make your JComboBox a class member variable & add a method to return result of getSelectedItem:

public class MyGuiApp {

    private JComboBox comboBox;

    // constructor, init method, etc.

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

这篇关于如何从Java中的另一个类调用combobox所选项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆