繁体   English   中英

如何从JComboBox读取字符串数组索引到JLabel?

[英]How can I read a String array index from a JComboBox into a JLabel?

我有一个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值绑定到特定名称(在有人冲我说可以使用某种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();
        }
    }

}

您可以通过以下方式获取索引和项目:

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

这里的cStartingClassesJcomboBox

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM