Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JAVA在第一个组合框中选择后更新第二个组合框_Java_Swing_Jcombobox - Fatal编程技术网

JAVA在第一个组合框中选择后更新第二个组合框

JAVA在第一个组合框中选择后更新第二个组合框,java,swing,jcombobox,Java,Swing,Jcombobox,我一直在努力掌握Java(只是因为:))。现在我被困在一个“计算器”上。我的意图是通过一个组合框选择一个整体主题,然后第二个组合框显示可以为该主题计算哪些单位 我的问题是,第二个组合框没有更新,我很难找到我的疏忽。有人能告诉我哪里出了问题吗 注意:unitstring的术语目前是占位符:) 请原谅我的无知,但是在subjectsel中进行选择之后,您希望第二个组合,unitsel会发生什么?这可能不是您的无知,而是我缺乏编码技能。”unitsel“应使用包含适合于“subjectsel”的单位的

我一直在努力掌握Java(只是因为:))。现在我被困在一个“计算器”上。我的意图是通过一个组合框选择一个整体主题,然后第二个组合框显示可以为该主题计算哪些单位

我的问题是,第二个组合框没有更新,我很难找到我的疏忽。有人能告诉我哪里出了问题吗

注意:unitstring的术语目前是占位符:)


请原谅我的无知,但是在
subjectsel
中进行选择之后,您希望第二个组合,
unitsel
会发生什么?这可能不是您的无知,而是我缺乏编码技能。”unitsel“应使用包含适合于“subjectsel”的单位的数组进行更新。组合显示的列表位于其模型中。方法
getModel()
将返回组合的模型。如果您想更改显示的列表,您需要更改模型-或用新模型替换它。啊哈!谢谢你,艾布拉,
public class UserInterface implements Runnable{

    String unitstring = "Select a subject";

    @Override
    public void run() {
        JFrame frame = new JFrame("Radiation Calculator 2.0");
        frame.setPreferredSize(new Dimension(350, 250));
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        createComponents(frame.getContentPane());

        frame.pack();
        frame.setVisible(true);
    }

    public void createComponents(Container container) {
        GridLayout layout = new GridLayout(4, 2);
        container.setLayout(layout);
        JLabel subject = new JLabel("Select a subject");

        String[] subjectStrings = {"Wavelenght", "Radioactive Decay", "Radiation Dose"};
        JComboBox subjectsel = new JComboBox(subjectStrings);
        subjectsel.addActionListener(this::actionPerformed);

        JLabel unit = new JLabel("Select a unit");

        String[] unitStrings = {unitstring};

        JComboBox unitsel = new JComboBox(unitStrings);

        JLabel input = new JLabel("Select a input");
        JTextField userinput = new JTextField("");

        JButton calculate = new JButton("Calculate");
        JTextArea result = new JTextArea("");

        container.add(subject);
        container.add(subjectsel);
        container.add(unit);
        container.add(unitsel);
        container.add(input);
        container.add(userinput);
        container.add(calculate);
        container.add(result);
    }

    public void actionPerformed(ActionEvent e) {
        JComboBox cb = (JComboBox) e.getSource();
        int print = cb.getSelectedIndex();
        System.out.println(print);
        unitArray(print);
    }

    public void unitArray(int x) {
        if (x == 0) {
            unitstring = "Lambda";
        }
        if (x == 1) {
            unitstring = "Bequerel";
        }
        if (x == 2) {
            unitstring = "Gray";
        }
        System.out.println(unitstring);
        }
}