gpt4 book ai didi

java - 将 JComboBox 中的项目替换为数组

转载 作者:行者123 更新时间:2023-12-02 02:53:26 31 4
gpt4 key购买 nike

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

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

第 95 行( // <---- Help Please!!! )是我想知道是否有一种简单的方法可以做到这一点的地方。问题是,当您在 GUI 的字符组合框中选择“charOne”时,您会看到一个奇怪的语言字符串,而不是武器组合框中的 4 种武器列表。

最佳答案

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

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

参见How to Use Combo Boxes了解更多详情

关于java - 将 JComboBox 中的项目替换为数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43446171/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com