簡體   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