如何从JComboBox读取String数组索引到JLabel?

时间:2023-01-27 18:32:52

I have a ComboBox holding a String[]'s values. I have several other String[]'s holding numbers. I want the user to be able to select a item from the ComboBox(holding my String[] names) and according to which one they pick, I want that index associated with one of my other arrays to be printed out in a JLabel for display. Here's what I have so far:

我有一个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

Any help would be very much appreciated...Please don't just post a solution, I want to understand the code not copy it.

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

2 个解决方案

#1


First, create a "wrapper" class which can bind the String values to a particular name (before anyone rushes at me says you can use a Map of some kind, you could, but this "carries" the associated information within a neat package)

首先,创建一个“包装器”类,它可以将String值绑定到一个特定的名称(在任何人冲我之前说你可以使用某种类型的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;
    }

}

Build an array of these ClassTypes and give this to the JComboBox

构建这些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);

And when the ItemListener is notified, extract the values from the selected item...

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

}

#2


You can get the index and item as:

您可以将索引和项目作为:

public void itemStateChanged(ItemEvent e)
    {
        if(e.getStateChange() == ItemEvent.SELECTED)
        {
           int index = cStartingClasses.getSelectedIndex();
           String item = String.valueOf(cStartingClasses.getSelectedItem());
        }
    }

Here cStartingClasses is JcomboBox.

这里的cStartingClasses是JcomboBox。

#1


First, create a "wrapper" class which can bind the String values to a particular name (before anyone rushes at me says you can use a Map of some kind, you could, but this "carries" the associated information within a neat package)

首先,创建一个“包装器”类,它可以将String值绑定到一个特定的名称(在任何人冲我之前说你可以使用某种类型的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;
    }

}

Build an array of these ClassTypes and give this to the JComboBox

构建这些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);

And when the ItemListener is notified, extract the values from the selected item...

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

}

#2


You can get the index and item as:

您可以将索引和项目作为:

public void itemStateChanged(ItemEvent e)
    {
        if(e.getStateChange() == ItemEvent.SELECTED)
        {
           int index = cStartingClasses.getSelectedIndex();
           String item = String.valueOf(cStartingClasses.getSelectedItem());
        }
    }

Here cStartingClasses is JcomboBox.

这里的cStartingClasses是JcomboBox。