如何从2个单独的组合框中选择项目到第3个组合框以及使用itemstatechanged,在java中?

时间:2014-08-22 05:56:40

标签: java swing jcombobox itemlistener

我已经在netbeans中创建了两个组合框并将一些项目插入到第一个组合框中以及第二个组合框中,当我想在第一个和第二个中选择一个项目时,两个选定的值必须出现在第三个组合框中。如果我想在第一个和第二个中更改选择,它也应该在第三个组合框中进行相同的选择,即e。项目状态已更改。

提前致谢,请帮助我,我们将不胜感激。

1 个答案:

答案 0 :(得分:1)

首先查看How to Use Combo BoxesHow to Write an Action Listeners了解更多详情......

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Combos {

    public static void main(String[] args) {
        new Combos();
    }

    public Combos() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                String[] numbers = new String[10];
                for (int index = 0; index < numbers.length; index++) {
                    numbers[index] = String.valueOf(index);
                }
                String[] letters = new String[10];
                for (int index = 0; index < letters.length; index++) {
                    letters[index] = Character.toString((char)(index + 65));
                }

                JComboBox cb1 = new JComboBox(numbers);
                JComboBox cb2 = new JComboBox(letters);

                String[] everything = new String[numbers.length + letters.length];
                System.arraycopy(numbers, 0, everything, 0, numbers.length);
                System.arraycopy(letters, 0, everything, numbers.length, letters.length);

                JComboBox cb3 = new JComboBox(everything);

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new GridBagLayout());
                GridBagConstraints gbc = new GridBagConstraints();
                gbc.gridx = 0;
                gbc.gridy = 0;
                frame.add(cb1, gbc);
                gbc.gridx++;
                frame.add(cb2, gbc);

                gbc.gridx = 0;
                gbc.gridy++;
                gbc.gridwidth = GridBagConstraints.REMAINDER;
                gbc.fill = GridBagConstraints.HORIZONTAL;
                frame.add(cb3, gbc);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);

                cb1.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        cb3.setSelectedItem(cb1.getSelectedItem());
                    }
                });
                cb2.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        cb3.setSelectedItem(cb2.getSelectedItem());
                    }
                });
            }
        });
    }

}

<强>更新

好的,基本上,对于第三个JComboBox,你需要将其他两个的内容组合成一个统一的模型,例如......

String combined[] = new String[numbers.length * letters.length];
int index = 0;
for (int outter = 0; outter < numbers.length; outter++) {
    for (int inner = 0; inner < numbers.length; inner++) {
        combined[index] = numbers[outter] + " " + letters[inner];
        index++;
    }
}

然后,当前两个中的任何一个发生变化时,您可以将结果组合起来并将其用作第三个中的所选项...

protected void updateCombinedView() {
    String value = cb1.getSelectedItem() + " " + cb2.getSelectedItem();
    cb3.setSelectedItem(value);
}

我仍然不知道为什么,但你有......

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Combos {

    public static void main(String[] args) {
        new Combos();
    }

    private JComboBox<String> cb1;
    private JComboBox<String> cb2;
    private JComboBox<String> cb3;

    public Combos() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                String[] numbers = new String[10];
                for (int index = 0; index < numbers.length; index++) {
                    numbers[index] = String.valueOf(index);
                }
                String[] letters = new String[10];
                for (int index = 0; index < letters.length; index++) {
                    letters[index] = Character.toString((char) (index + 65));
                }

                String combined[] = new String[numbers.length * letters.length];
                int index = 0;
                for (int outter = 0; outter < numbers.length; outter++) {
                    for (int inner = 0; inner < numbers.length; inner++) {
                        combined[index] = numbers[outter] + " " + letters[inner];
                        index++;
                    }
                }

                cb1 = new JComboBox(numbers);
                cb2 = new JComboBox(letters);

                cb3 = new JComboBox(combined);

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new GridBagLayout());
                GridBagConstraints gbc = new GridBagConstraints();
                gbc.gridx = 0;
                gbc.gridy = 0;
                frame.add(cb1, gbc);
                gbc.gridx++;
                frame.add(cb2, gbc);

                gbc.gridx = 0;
                gbc.gridy++;
                gbc.gridwidth = GridBagConstraints.REMAINDER;
                gbc.fill = GridBagConstraints.HORIZONTAL;
                frame.add(cb3, gbc);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);

                cb1.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        updateCombinedView();
                    }
                });
                cb2.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        updateCombinedView();
                    }
                });
            }
        });
    }

    protected void updateCombinedView() {
        String value = cb1.getSelectedItem() + " " + cb2.getSelectedItem();
        cb3.setSelectedItem(value);
    }

}