简体   繁体   中英

How do I Assign an Array to the Combo Boxes

In order to make the program work I need to have a drop down menu (combo box) that shows all of the possible units to convert to for Stoichiometry.

I know that you must assign an array to a combo box, but I am not sure how. If somebody could help it would be great.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Conversion extends JFrame
{
    private JLabel molarMass1;
    private JLabel molarMass2;
    private JLabel moles1;
    private JLabel moles2;
    private JLabel amount1;
    private JLabel amount2;

    private JTextField M1M2;
    private JTextField M2M1;
    private JTextField moles1moles2;
    private JTextField moles2moles1;
    private JTextField amount1amount2;
    private JTextField amount2amount1;
    private JComboBox UnitsOne;
    private JComboBox UnitsTwo;

    private static final int WIDTH = 500;
    private static final int HEIGHT = 50;





    Conversion()
    {}

    public void burst()
    {
        setTitle("Stoichiometry");
        Container z = getContentPane();
        z.setLayout(new GridLayout(1, 4));

        molarMass1 = new JLabel("Molar Mass 1: ", SwingConstants.LEFT);
        molarMass2 = new JLabel("Molar Mass 2: ", SwingConstants.RIGHT);
        moles1 = new JLabel("Number of Moles 1: ", SwingConstants.LEFT);
        moles2 = new JLabel("Number of Moles 2: ", SwingConstants.RIGHT);
        amount1 = new JLabel("Amount of substance 1: ", SwingConstants.LEFT);
        amount2 = new JLabel("Amount of substance 2: ", SwingConstants.RIGHT);

        M1M2 = new JTextField(7);
        M2M1 = new JTextField(7);
        moles1moles2 = new JTextField(6);
        moles2moles1 = new JTextField(6);
        amount1amount2 = new JTextField(5);
        amount2amount1 = new JTextField(5);
        UnitsOne = new JComboBox(10);
        UnitsTwo = new JComboBox(10);


        z.add(molarMass1);
        z.add(M1M2);
        z.add(molarMass2);
        z.add(M2M1);

        z.add(moles1);
        z.add(moles1moles2);
        z.add(moles2);
        z.add(moles2moles1);

        z.add(amount1);
        z.add(amount1amount2);
        z.add(amount2);
        z.add(amount2amount1);

        z.add(UnitsOne);
        z.add(UnitsTwo);


        setSize(WIDTH, HEIGHT);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
}
}

I don't see where the array you want to assign to the combo box is in your code, but there is a constructor which do exactly what you need: http://docs.oracle.com/javase/7/docs/api/javax/swing/JComboBox.html#JComboBox(E[])

Alternatively you can add items to the JComboBox later using the addItem() method

Also have a look here: https://docs.oracle.com/javase/tutorial/uiswing/components/combobox.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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