【问题标题】:JComboBox engaging other JComboBoxes ActionListenersJComboBox 参与其他 JComboBoxes ActionListeners
【发布时间】:2017-01-03 05:14:30
【问题描述】:

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

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

就像当我在 Racebox 中选择某个东西时,它开始从所有其他 actionPerformed 中读取代码。

一个奇怪的事情是 JComboBoxes 在添加“racebox”之后向后添加。 第一种:赛车盒 二:步兵nmrbox 第三:步兵箱

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

【问题讨论】:

    标签: java swing actionlistener jcombobox


    【解决方案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();
                }
            });
        }
    }
    

    【讨论】:

    • 感谢您的回复。其中一些代码我根本不明白。抱歉,我的代码有点不清楚。这些框只会在原始代码中添加一次。但问题是它们会一次全部添加。我希望所有这些都被添加,但不是同时添加。
    • @Richovic, But the problem is that they get added all at once. - 将数据添加到组合框后,将 ActionListener 添加到组合框。如果您需要更多帮助,请发布正确的 minimal reproducible example 来说明问题。
    • 没关系,我现在明白了。我使用了“setModel(new DefaultComboBoxModel());”在某些方面,我得到了它的工作。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2023-03-07
    • 2019-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-02
    • 1970-01-01
    相关资源
    最近更新 更多