将字符串添加到comboBox

时间:2014-11-18 14:32:21

标签: java string combobox serial-port

我开始使用GUI,我想做的第一件事就是看看哪些串口是活动/连接的。这是由listSerialPorts()函数完成的。我想把这些信息放到ComboBox中。但它似乎不知道' comboBox'当我尝试在函数中使用它时变量。从我的功能中填充组合框的最佳方法是什么?

对于组合框:我只是拖动和放大掉了它。所以这些其他代码都是自动生成的。 我把功能放在哪里(主要之前/之后)是否重要? 顺便说一句。我没有多少java经验。

public class gui_v1 {

    private JFrame frame;

    /**
     * Launch the application.
     */

    /** My input */
    public static String[] listSerialPorts() {
        Enumeration ports = CommPortIdentifier.getPortIdentifiers();
        ArrayList portList = new ArrayList();
        String portArray[] = null;
        while (ports.hasMoreElements()) {
            CommPortIdentifier port = (CommPortIdentifier) ports.nextElement();
            if (port.getPortType() == CommPortIdentifier.PORT_SERIAL) {
                portList.add(port.getName());
            }
        }
        portArray = (String[]) portList.toArray(new String[0]);
        return portArray;
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    gui_v1 window = new gui_v1();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        });
    }

    /**
     * Create the application.
     */
    public gui_v1() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    public void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        JComboBox comboBox = new JComboBox();
        comboBox.setBounds(36, 49, 53, 20);
        frame.getContentPane().add(comboBox);

    } 
}

1 个答案:

答案 0 :(得分:0)

在代码中,将comboBox声明为方法initialize()的变量local。这使得它只在声明comboBox的方法中可见。

要使变量可以在任何地方访问,您可以将其声明为类的静态成员。

public class gui_v1
{
    private JFrame frame;
    public static JComboBox comboBox;

    /**
     * Launch the application.
     */
    /**
     * My input
     */
    public static String[] listSerialPorts()
    {
        Enumeration ports = CommPortIdentifier.getPortIdentifiers();
        ArrayList portList = new ArrayList();
        String portArray[] = null;
        while (ports.hasMoreElements())
        {
            CommPortIdentifier port = (CommPortIdentifier) ports.nextElement();
            if (port.getPortType() == CommPortIdentifier.PORT_SERIAL)
            {
                portList.add(port.getName());
            }
        }
        portArray = (String[]) portList.toArray(new String[0]);
        return portArray;
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                try
                {
                    gui_v1 window = new gui_v1();
                    window.frame.setVisible(true);
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }

            }
        });
    }

    /**
     * Create the application.
     */
    public gui_v1()
    {
        initialize();
        //comboBox is accessible from here too
        comboBox.setBounds(36, 49, 53, 20);
    }

    /**
     * Initialize the contents of the frame.
     */
    public void initialize()
    {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        //This uses a static member comboBox, initializes it with a value.
        //See declaration of member above : public static ComboBox comboBox 
        comboBox = new JComboBox();

        comboBox.setBounds(36, 49, 53, 20);
        frame.getContentPane().add(comboBox);

    }
}