JComboBox与其他JComboBoxes ActionListeners合作

时间:2017-01-03 05:14:30

标签: java swing actionlistener jcombobox

所以我的问题在于JComboBoxs和ActionListeners。我将制作一个新的简化代码,尝试用原始代码表示问题。我想要一个JComboBox添加一个JComboBox,然后添加第三个JComboBox等等。每次我点击它们,我希望它们根据之前的JComboBox显示内容来更改内容。

无论如何,我现在最大的问题是当我在第一个JComboBox“racebox”中选择一些内容时。它不仅向面板添加“infantrybox”,它还添加了我拥有的所有其他JComboBox,而不是仅在我在相应的JComboBox中选择内容时添加它们。

就像当我在racebox中选择某些内容时,它开始从其他每个actionPerformed中读取代码。

一个奇怪的事情是在添加“racebox”后向后添加JComboBoxes。 第一:赛车 第二名:infantrynmrbox 第三名:infantrybox

...
public void Attacker(){

    racebox = new JComboBox(array);
    infantrybox = new JComboBox();
    infantrynmrbox = new JComboBox();

    panel.add(racebox);
    panel.revalidate();
    panel.repaint();

    racebox.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            JComboBox cb = (JComboBox)e.getSource();
            race = (String)cb.getSelectedItem();

            infantrybox.removeAllItems();
            for(String s : otherarray){
                infantrybox.addItem(s);
            }

            panel.add(infantrybox);
            panel.revalidate();
            panel.repaint();
        }
    });

    infantrybox.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            JComboBox cb = (JComboBox)e.getSource();
            infantry = (String)cb.getSelectedItem();

            infantrynmrbox.removeAllItems();
            for(String s : nmr){
                infantrynmrbox.addItem(s);
                System.out.println(s + " ");
            }

            panel.add(infantrynmrbox);
            panel.revalidate();
            panel.repaint();
        }
    });

    infantrynmrbox.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            JComboBox cb = (JComboBox)e.getSource();
            infantrynmr = Integer.parseInt((String)cb.getSelectedItem());
        }
    });
    ...
}

1 个答案:

答案 0 :(得分:1)

请勿继续向面板添加组合框。

相反,您只需更改现有组合框的模型。

例如:

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.plaf.basic.*;

public class ComboBoxTwo extends JPanel implements ActionListener
{
    private JComboBox<String> mainComboBox;
    private JComboBox<String> subComboBox;
    private Hashtable<String, String[]> subItems = new Hashtable<String, String[]>();

    public ComboBoxTwo()
    {
        String[] items = { "Select Item", "Color", "Shape", "Fruit" };
        mainComboBox = new JComboBox<String>( items );
        mainComboBox.addActionListener( this );

        //  prevent action events from being fired when the up/down arrow keys are used
        mainComboBox.putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE);
        add( mainComboBox );

        //  Create sub combo box with multiple models

        subComboBox = new JComboBox<String>();
        subComboBox.setPrototypeDisplayValue("XXXXXXXXXX"); // JDK1.4
        add( subComboBox );

        JButton arrow = SwingUtils.getDescendantOfType(JButton.class, subComboBox, "Text", "");
        Dimension d = arrow.getPreferredSize();
        System.out.println(arrow.getClass());
        System.out.println(d);
        d.width = 100;
        arrow.setPreferredSize(d);

        String[] subItems1 = { "Select Color", "Red", "Blue", "Green" };
        subItems.put(items[1], subItems1);

        String[] subItems2 = { "Select Shape", "Circle", "Square", "Triangle" };
        subItems.put(items[2], subItems2);

        String[] subItems3 = { "Select Fruit", "Apple", "Orange", "Banana" };
        subItems.put(items[3], subItems3);
    }

    public void actionPerformed(ActionEvent e)
    {
        String item = (String)mainComboBox.getSelectedItem();
        Object o = subItems.get( item );

        if (o == null)
        {
            subComboBox.setModel( new DefaultComboBoxModel() );
        }
        else
        {
            subComboBox.setModel( new DefaultComboBoxModel( (String[])o ) );
        }
    }

    private static void createAndShowUI()
    {
        try
        {
//          UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        }
        catch (Exception e) { }
        JFrame frame = new JFrame("SSCCE");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add( new ComboBoxTwo() );
        frame.setLocationByPlatform( true );
        frame.pack();
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowUI();
            }
        });
    }
}