gpt4 book ai didi

java - 如何将 JComboBox 中的字符串数组索引读取到 JLabel 中?

转载 作者:行者123 更新时间:2023-12-01 11:34:03 24 4
gpt4 key购买 nike

我有一个 ComboBox,其中包含 String[] 的值。我还有其他几个 String[] 的持有号码。我希望用户能够从 ComboBox 中选择一项(保存我的 String[] 名称),并根据他们选择的一项,我希望与我的其他数组之一关联的索引在 JLabel 中打印出来以供显示。这是我到目前为止所拥有的:

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

public class NewBuild extends JFrame
{
private static final int WIDTH = 900;
private static final int HEIGHT = 350;

//Create array constants
private static final String[] WARRIOR = {"7","6","6","5","15","11","5","5","5"};
private static final String[] KNIGHT = {"12","6","7","4","11","8","9","3","6"};
private static final String[] SWORDSMAN = {"4","8","4","6","9","16","6","7","5"};
private static final String[] BANDIT = {"9","7","11","2","9","14","3","1","8"};
private static final String[] CLERIC = {"10","3","8","10","11","5","4","4","12"};
private static final String[] SORCERER = {"5","6","5","12","3","7","8","14","4"};
private static final String[] EXPLORER = {"7","6","9","7","6","6","12","5","5"};
private static final String[] DEPRIVED = {"6","6","6","6","6","6","6","6","6"};
private static final String[] CLASS_NAMES = {" ", "Warrior", "Knight", "SwordsMan", "Bandit", "Cleric", "Sorcerer", "Explorer", "Deprived"};



private int num;
private int count = 0;
private String classes;

Container newBuildWindow = getContentPane();

private JLabel lblBuildName, lblStartingClass, lblDisplayStartingStats;

private JTextField txtBuildName;

private JComboBox cStartingClasses;


public NewBuild()
{
GUI();

}//End of Constructor

public void GUI()
{
this.setSize(WIDTH, HEIGHT);
newBuildWindow.setBackground(Color.DARK_GRAY);
setTitle("New Build");
setLayout(new GridLayout(5,2));
setVisible(true);
// setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);

JPanel panel1 = new JPanel(new GridLayout());
JPanel panel2 = new JPanel(new GridLayout(2,3));

lblBuildName = new JLabel("Build Name");
lblBuildName.setForeground(Color.YELLOW);
panel1.setBackground(Color.DARK_GRAY);
panel1.add(lblBuildName);

txtBuildName = new JTextField(15);
txtBuildName.setBackground(Color.LIGHT_GRAY);
panel1.add(txtBuildName);

lblStartingClass = new JLabel("Pick a starting class:");
lblStartingClass.setForeground(Color.YELLOW);
lblDisplayStartingStats = new JLabel("Default");
lblDisplayStartingStats.setForeground(Color.YELLOW);
cStartingClasses = new JComboBox(CLASS_NAMES);

cStartingClasses.addItemListener(new itemChangeListener());



panel2.setBackground(Color.DARK_GRAY);
panel2.add(lblStartingClass);
panel2.add(cStartingClasses);
panel2.add(lblDisplayStartingStats);


//add panels to pane
newBuildWindow.add(panel1);
newBuildWindow.add(panel2);
// pack();

}//End of GUI method

class itemChangeListener implements ItemListener
{
@Override
public void itemStateChanged(ItemEvent e)
{
if(e.getStateChange() == ItemEvent.SELECTED)
{



}

}

}






}//End of class NewBuild

任何帮助将非常感激...请不要只是发布解决方案,我想理解代码而不是复制它。

最佳答案

首先,创建一个“包装”类,它可以将 String 值绑定(bind)到特定名称(在有人冲向我说你可以使用某种 Map 之前) ,你可以,但是这在一个整洁的包中“携带”了相关信息)

public class ClassType {

private String description;
private String[] values;

public ClassType(String description, String[] values) {
this.description = description;
this.values = values;
}

public String[] getValues() {
return values;
}

@Override
public String toString() {
return description;
}

}

构建这些 ClassType 的数组并将其提供给 JComboBox

    ClassType[] types = new ClassType[]{
new ClassType("Warrior", WARRIOR),
new ClassType("Knight", KNIGHT),
new ClassType("Swordsman", SWORDSMAN),
new ClassType("Bandit", BANDIT),
new ClassType("Cleric", CLERIC),
new ClassType("Socerer", SORCERER),
new ClassType("Explorer", EXPLORER),
new ClassType("Deprived", DEPRIVED)
};

cStartingClasses = new JComboBox(types);

ItemListener 收到通知时,从所选项目中提取值...

@Override
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
ClassType classType = (ClassType) ((JComboBox)e.getSource()).getSelectedItem();
if (classType != null) {
String values[] = classType.getValues();
}
}

}

关于java - 如何将 JComboBox 中的字符串数组索引读取到 JLabel 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30181181/

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