提问者:小点点

JComboBox 吸引其他 JComboBox ActionListeners


所以我的问题出在JComboBoxs和ActionListeners上。我将制作一个新的简化代码,以尝试从我的原始代码中表示问题。我想要一个JComboBox添加一个JComboBox,然后他将添加第三个JComboBox,依此类推。每次我单击它们时,我都希望它们根据以前的JComboBox显示的内容更改内容。

无论如何,我现在最大的问题是当我在第一个JComboBox“赛车盒”中选择某些东西时。它不仅将“infantrybox”添加到面板中,还添加了我拥有的所有其他JComboBox,而不是仅在我在各自的JComboBox中选择某些内容时才添加它们。

就像当我在赛车箱中选择某些东西时,它开始从其他每个动作中读取代码执行。

奇怪的是,在添加“racebox”之后,JComboBoxes是向后添加的。第一:赛马箱 第二:步兵箱 第三:步兵箱

...
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个答案

匿名用户

不要继续向面板添加组合框。

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

例如:

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();
            }
        });
    }
}