用数组替换JComboBox中的项目

时间:2023-01-27 13:14:04

I was wondering if there was an easy way to do this. I'm trying to remove the items from the weapons combobox (with success), and then add the array into the combo box like how it was when it was first instantiated. I don't want to just create and copy a new one, because there are body constraints already set to the liking. Here is the code...

我想知道是否有一种简单的方法可以做到这一点。我正在尝试从武器组合框中删除项目(成功),然后将数组添加到组合框中,就像它首次实例化时一样。我不想只是创建和复制一个新的,因为已经设置了喜欢的身体约束。这是代码......

import org.apache.commons.lang3.ArrayUtils;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
import javax.swing.border.Border;

public class TestGui extends JFrame {
    private final String[] guiCharSelDefault = {"---  Select Character ---"};
    private final String[] characters = {"charOne", "charTwo", "charThree", "charFour"};
    private final String[] GuiCharSel = (String[]) ArrayUtils.addAll(guiCharSelDefault, characters);
    private final String[] weapon = {"Weapon"};
    private final String[][] allWeapons = {
            {
                    "weakWeaponOne", "strongWeaponOne", "shortWeaponOne", "longWeaponOne"
            },
            {
                    "weakWeaponTwo", "strongWeaponTwo", "shortWeaponTwo", "longWeaponTwo"
            },
            {
                    "weakWeaponThree", "strongWeaponThree", "shortWeaponThree", "longWeaponThree"
            },
            {
                    "weakWeaponFour", "strongWeaponFour", "shortWeaponFour", "longWeaponFour"
            }
    };
    private JComboBox charCombo = new JComboBox(GuiCharSel);
    private JComboBox weaponsCombo = new JComboBox(weapon);
    private JPanel centerFrame = createCenterFrame();

    //**************************************************************************************

    private GridBagConstraints setGbc(int gridx, int gridy, int ipadx, int ipady, String anchorLocation, double weightx, double weighty, Insets insets){
        GridBagConstraints gbc = new GridBagConstraints();

        if (anchorLocation.toUpperCase().equals("NORTHWEST")){
            gbc.anchor = GridBagConstraints.NORTHWEST;
        } else if (anchorLocation.toUpperCase().equals("NORTH")){
            gbc.anchor = GridBagConstraints.NORTH;
        } else if (anchorLocation.toUpperCase().equals("NORTHEAST")){
            gbc.anchor = GridBagConstraints.NORTHEAST;
        } else if (anchorLocation.toUpperCase().equals("WEST")){
            gbc.anchor = GridBagConstraints.WEST;
        } else if (anchorLocation.toUpperCase().equals("EAST")){
            gbc.anchor = GridBagConstraints.EAST;
        } else if (anchorLocation.toUpperCase().equals("SOUTHWEST")){
            gbc.anchor = GridBagConstraints.SOUTHWEST;
        } else if (anchorLocation.toUpperCase().equals("SOUTH")){
            gbc.anchor = GridBagConstraints.SOUTH;
        } else if (anchorLocation.toUpperCase().equals("SOUTHEAST")){
            gbc.anchor = GridBagConstraints.SOUTHEAST;
        } else {
            gbc.anchor = GridBagConstraints.CENTER;
        }

        gbc.gridx = gridx;
        gbc.gridy = gridy;
        gbc.ipadx = ipadx;
        gbc.ipady = ipady;
        gbc.weightx = weightx;
        gbc.weighty = weighty;
        gbc.insets = insets;

        return gbc;
    }

    private Insets setInsets(int top, int left, int bottom, int right){
        Insets insets = new Insets(top,left,bottom,right);
        return insets;
    }

    //**************************************************************************************

    private JPanel createCenterFrame(){
        JPanel pnl = new JPanel();
        Border raisedBevel = BorderFactory.createRaisedBevelBorder();
        Border loweredBevel = BorderFactory.createLoweredBevelBorder();
        Border compound = BorderFactory.createCompoundBorder(raisedBevel, loweredBevel);

        pnl.setBorder(compound);

        charCombo.addActionListener(
                new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        String charName = ((JComboBox)(e.getSource())).getSelectedItem().toString();
                        if (charName.equals("charOne")){
                            weaponsCombo.removeAllItems();
                            weaponsCombo.addItem(allWeapons[1]); // <---- Help Please!!!
                        }
                    }
                }
        );
        pnl.add(charCombo, setGbc(0,0, 0, 0, "NORTHWEST", 0, 0, setInsets(0, 10, 0, 0)));
        pnl.add(weaponsCombo, setGbc(0,0, 0, 0, "NORTHWEST", 0, 0, setInsets(0, 10, 0, 0)));

        return pnl;
    }

    TestGui(){
        add(centerFrame, BorderLayout.CENTER);

        setSize(800,600);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    //**************************************************************************************

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

Line 95 (// <---- Help Please!!!) is where I'm wondering if there is an easy way to do this. The problem is when you select "charOne" in the character combobox on the gui, you see a weird language string instead of a list of the 4 weapons in the weapon combobox.

第95行(// <----请求!!!)是我想知道是否有一个简单的方法来做到这一点。问题是当你在gui上的角色组合框中选择“charOne”时,你会看到一个奇怪的语言字符串而不是武器组合框中的4个武器列表。

1 个解决方案

#1


2  

The simple solution is simply to replace the ComboBoxModel...

简单的解决方案就是替换ComboBoxModel ......

if (charName.equals("charOne")) {
    DefaultComboBoxModel<String> model = new DefaultComboBoxModel<>(allWeapons[1]);
    weaponsCombo.setModel(model);
}

See How to Use Combo Boxes for more details

有关更多详细信息,请参见如何使用组合框

#1


2  

The simple solution is simply to replace the ComboBoxModel...

简单的解决方案就是替换ComboBoxModel ......

if (charName.equals("charOne")) {
    DefaultComboBoxModel<String> model = new DefaultComboBoxModel<>(allWeapons[1]);
    weaponsCombo.setModel(model);
}

See How to Use Combo Boxes for more details

有关更多详细信息,请参见如何使用组合框