简体   繁体   English

从JComboBox中选择一个项目后,如何让JLabel从数组中显示值?

[英]How do you let a JLabel display a value from an array when an item is selected from a JComboBox?

I am trying to create a fortune teller that lets you pick a color from one JCombobox then lets you pick a number from another JCombobox in accordance to what color you have chosen. 我正在尝试创建一个算命先生,让您从一个JCombobox中选择一种颜色,然后根据您选择的颜色从另一个JCombobox中选择一个数字。 For example if I pick "Red" or "Yellow", then this set of #'s are shown - {1,3,4,7}. 例如,如果我选择“红色”或“黄色”,则会显示这组#-{1,3,4,7}。 However if I pick "Blue" or "Green", this set of #'s are shown - {2,6,8,5}. 但是,如果我选择“蓝色”或“绿色”,则会显示这组#-{2,6,8,5}。 Once a number is selected then I would like to display a fortune from the "fortune array" that is tied to that number on a statusbar(JLabel) at the bottom of the screen. 选择一个数字后,我想在屏幕底部的状态栏(JLabel)上显示与该数字相关的“财富数组”中的财富。 This is my code so far:- 到目前为止,这是我的代码:

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

public class Example2 extends JFrame implements ItemListener {


    private JComboBox maincombobox;
    private JComboBox subcombobox;
    private JLabel labels;

    public Example2(){
        String[] items = {"Select a Color","Red","Blue","Yellow","Green"};
        maincombobox =  new JComboBox(items);
        maincombobox.addItemListener(this);

        getContentPane().add(maincombobox);

        subcombobox = new JComboBox();
        subcombobox.addItemListener(this);      
        subcombobox.setEnabled(true);
        subcombobox.setPrototypeDisplayValue("XXXXXXXXXXXXX");
        getContentPane().add(subcombobox,BorderLayout.EAST);

        labels =  new JLabel("Default");
        getContentPane().add(labels, BorderLayout.SOUTH);


             }
    public void itemStateChanged(ItemEvent e) {
        String[] subitems1 = {"Choose a number","1","3","4","7"};
        String[] subitems2= {"Choose a number","2","6","8","5"};

        String[] fortune = {"Today is you lucky day", "You will get strange looks from people", "Don't touch your left foot today", 
                "You will forget a crucial thing today", "You will meet a mysterious person", "Will win a million dollars", 
                "Good day in the financial market", "Get a life","Think hard, you will find the answer"};


        if (e.getSource() == maincombobox) {

            if (maincombobox.getSelectedItem().equals("Select a Color")) {
               subcombobox.setEnabled(false);
            }    
            else if (maincombobox.getSelectedItem().equals("Red") ||
                maincombobox.getSelectedItem().equals("Yellow") ){

                subcombobox.setEnabled(true);
                subcombobox.removeAllItems();
                for (int i = 0; i < subitems1.length; i++) {
                    subcombobox.addItem(subitems1[i]);
                }

            }

            else if (maincombobox.getSelectedItem().equals("Blue") ||
                maincombobox.getSelectedItem().equals("Green") ){


                subcombobox.setEnabled(true);
                subcombobox.removeAllItems();
                for (int i = 0; i < subitems2.length; i++) {
                    subcombobox.addItem(subitems2[i]);
                }

            }
        }

    }

    public static void main(String[] args){
        JFrame frame = new Example2();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(300,200);
        frame.setVisible(true);
        frame.setLocationRelativeTo(null); 
    }
}

add the following code: 添加以下代码:

if(e.getSource()==subcombobox){
    int choice=Integer.parseInt(subcombobox.getSelectedItem());
    //here the selected number gets stored into the int variable choice
    labels.setText(fortune[choice]);
    //here the text for "labels" is set from your fortune string array
}

Change the code according to your liking but this itself is self-explanatory 根据您的喜好更改代码,但这本身是不言自明的

i wrote the logic perfectly check this 我写的逻辑完美地检查了这一点

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

public class Example2 extends JFrame implements ItemListener {

private JComboBox maincombobox;
private JComboBox subcombobox;
private JLabel labels;

public Example2(){
    String[] items = {"Select a Color","Red","Blue","Yellow","Green"};
    maincombobox =  new JComboBox(items);
    maincombobox.addItemListener(this);

    getContentPane().add(maincombobox);

    subcombobox = new JComboBox();
    subcombobox.addItemListener(this);      
    subcombobox.setEnabled(true);
    subcombobox.setPrototypeDisplayValue("XXXXXXXXXXXXX");
    getContentPane().add(subcombobox,BorderLayout.EAST);

    labels =  new JLabel("Default");
    getContentPane().add(labels, BorderLayout.SOUTH);


         }
public void itemStateChanged(ItemEvent e) {
    String[] subitems1 = {"Choose a number","1","3","4","7"};
    String[] subitems2= {"Choose a number","2","6","8","5"};

    String[] fortune = {"Today is you lucky day", "You will get strange looks from people", "Don't touch your left foot today", 
            "You will forget a crucial thing today", "You will meet a mysterious person", "Will win a million dollars", 
            "Good day in the financial market", "Get a life","Think hard, you will find the answer"};

    if (e.getSource() == maincombobox)
    {

        if (maincombobox.getSelectedItem().equals("Select a Color"))
        {
            subcombobox.setEnabled(false);
        }
        else if (maincombobox.getSelectedItem().equals("Red") || maincombobox.getSelectedItem().equals("Yellow"))
        {

            subcombobox.setEnabled(true);
            subcombobox.removeAllItems();
            for (int i = 0; i < subitems1.length; i++)
            {
                subcombobox.addItem(subitems1[i]);

            }

        }

        else if (maincombobox.getSelectedItem().equals("Blue") || maincombobox.getSelectedItem().equals("Green"))
        {

            subcombobox.setEnabled(true);
            subcombobox.removeAllItems();
            for (int i = 0; i < subitems2.length; i++)
            {
                subcombobox.addItem(subitems2[i]);

            }
        }

    }


    else if (subcombobox.getItemCount()>0)
    {
        for(int i=0;i<8;i++)
        {
                if (subcombobox.getSelectedItem().equals(Integer.toString(i)))
                {
                    labels.setText(fortune[i]);
                }

        }
    }

}

public static void main(String[] args){
    JFrame frame = new Example2();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(300,200);
    frame.setVisible(true);
    frame.setLocationRelativeTo(null); 
}

} }

I just added the code at the bottom is 我只是在底部添加了代码

else if (subcombobox.getItemCount()>0)
{
    for(int i=0;i<8;i++)
    {
            if (subcombobox.getSelectedItem().equals(Integer.toString(i)))
            {
                labels.setText(fortune[i]);
            }

    }
}

if it works tick it as correct answer 如果可行,请勾选正确答案

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

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