猿问

如何从另一个类的 JComboBox 获取内容

我正在开发一些应用程序。在主页(注册页面)上有一个组合框,其中有几个选项可供选择。根据选择的内容,具体内容将出现在下一个窗口中。问题是如何从其他类中的组合框获取内容。


我假设需要添加一些 if 控制指令,但是每次我添加一些东西时它都会返回一个错误。


有人可以帮助一下代码应该是什么样子吗?例如,如果选择选项“1”,则将背景设置为黑色,如果选择“2”,则将背景设置为粉色等。


注册窗口:


public Main() throws Exception {


        registerWindowFrame = new JFrame("xxx");                                                    

        //registerWindowFrame.setSize(1440,2960);                                                                        

        registerWindowFrame.setSize(500, 750);

        registerWindowFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);                                              

        registerWindowFrame.setLayout(null);

        registerWindowFrame.getContentPane().setBackground(Color.RED);


BloodList bloodList = new BloodList();

        bloodList.setSize(bloodList.getPreferredSize());

        bloodList.setLocation(10, 365);

        registerWindowFrame.add(bloodList);

组合框类:


public class BloodList extends JComboBox <String> {


    int i;


    public String[] bloodList =

            {

                    "1",

                    "2",

                    "3",


            };


    public BloodList() {

        for (i=0; i < bloodList.length; i++)

        {

            this.addItem(bloodList[i]);

        };

    }


}

重新调整窗口后的窗口:


public mainWindowBplus() {

        super();

        bloodBplusFrame = new JFrame("SETTINGS");

        bloodBplusFrame.setSize(500, 750);

        bloodBplusFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        bloodBplusFrame.setLayout(null);

        bloodBplusFrame.getContentPane().setBackground(Color.BLUE);

    bloodBplusFrame.setVisible(true);


米脂
浏览 140回答 1
1回答

九州编程

您应该添加一个action listener来jcombobox获取所选值String,然后在另一个类中使用该值,试试这个:public class BloodList extends JComboBox <String> {&nbsp; &nbsp; private String s="";&nbsp; &nbsp; private String[] bloodList =&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "1",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "2",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "3",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; public BloodList() {&nbsp; &nbsp; &nbsp; &nbsp; for (int i=0; i < bloodList.length; i++)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.addItem(bloodList[i]);&nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; }&nbsp; &nbsp; ActionListener cbActionListener = new ActionListener() {//add actionlistner to listen for change&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void actionPerformed(ActionEvent e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; s = (String) BloodList.this.getSelectedItem();//get the selected string&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; };&nbsp; &nbsp; this.addActionListener(cbActionListener);&nbsp; &nbsp; public String getS(){return s;}}现在您可以String通过使用该getS()方法在另一个类中使用它。
随时随地看视频慕课网APP

相关分类

Java
我要回答