0

ある JCombobox から色を選択し、選択した色に応じて別の JCombobox から数字を選択できる占い師を作成しようとしています。たとえば、「赤」または「黄」を選択すると、この一連の # が表示されます - {1,3,4,7}。ただし、「青」または「緑」を選択すると、この一連の # が表示されます - {2,6,8,5}。数字が選択されたら、画面下部のステータスバー (JLabel) にその数字に関連付けられている「フォーチュン配列」からフォーチュンを表示したいと思います。これはこれまでの私のコードです:-

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); 
    }
}
4

2 に答える 2

0

次のコードを追加します。

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
}

好みに応じてコードを変更しますが、これ自体は一目瞭然です

于 2012-06-29T08:18:53.897 に答える
0

私はこれを完全にチェックするロジックを書きました

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); 
}

}

一番下にコードを追加しました

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

    }
}

それが機能する場合は、正解としてチェックしてください

于 2012-06-29T10:12:20.627 に答える