Java 如何从第二个子控制台获取值?

Java 如何从第二个子控制台获取值?,java,jcombobox,Java,Jcombobox,我是新来的,我需要关于Java代码的帮助。在这里问问题是我最后的选择,然后你就可以了。我已经把脑子里的每一点都挤进了。我没有得到我想要的输出。此代码发布在stackoverflow中。这段代码和我想创建的代码是一样的 我只想得到第二个JComboBox的值 例如: 我点击了第一个组合框中的“形状”。然后我选择“三角形”。最后一部分是我会放一些,或者它会显示关于“三角形”的描述。比如“三角形有三条边”之类的 另一个例子 我在第一个组合框中选择了“颜色”。然后我点击了“红色”。然后我想在输出的最后一

我是新来的,我需要关于Java代码的帮助。在这里问问题是我最后的选择,然后你就可以了。我已经把脑子里的每一点都挤进了。我没有得到我想要的输出。此代码发布在stackoverflow中。这段代码和我想创建的代码是一样的

我只想得到第二个JComboBox的值

例如:

我点击了第一个组合框中的“形状”。然后我选择“三角形”。最后一部分是我会放一些,或者它会显示关于“三角形”的描述。比如“三角形有三条边”之类的

另一个例子

我在第一个组合框中选择了“颜色”。然后我点击了“红色”。然后我想在输出的最后一部分显示颜色“红色”的描述

在JComboBox中选择后的输出示例如下:

形状:三角形

三角形有三条边。”

我希望这是有道理的

下面是示例代码

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

  public class ComboBoxTwo extends JFrame implements ActionListener,   ItemListener {

private static final long serialVersionUID = 1L;
private JComboBox mainComboBox;
private JComboBox subComboBox;
private Hashtable<Object, Object> subItems = new Hashtable<Object, Object> ();

public ComboBoxTwo() {
    String[] items = {"Select Item", "Color", "Shape", "Fruit"};
    mainComboBox = new JComboBox(items);
    mainComboBox.addActionListener(this);
    mainComboBox.addItemListener(this);

    getContentPane().add(mainComboBox, BorderLayout.WEST);
    subComboBox = new JComboBox();

    subComboBox.setPrototypeDisplayValue("XXXXXXXXXX"); // JDK1.4
    subComboBox.addItemListener(this);
    getContentPane().add(subComboBox, BorderLayout.EAST);
    String[] subItems1 = {"Select Color", "Red", "Blue", "Green"};
    subItems.put(items[1], subItems1);
    String[] subItems2 = {"Select Shape", "Circle", "Square", "Triangle"};
    subItems.put(items[2], subItems2);
    String[] subItems3 = {"Select Fruit", "Apple", "Orange", "Banana"};
    subItems.put(items[3], subItems3);

}

@Override
public void actionPerformed(ActionEvent e) {
    String item = (String) mainComboBox.getSelectedItem();
    Object o = subItems.get(item);
    if (o == null) {
        subComboBox.setModel(new DefaultComboBoxModel());
    } else {
        subComboBox.setModel(new DefaultComboBoxModel((String[]) o));
    }
}

@Override
public void itemStateChanged(ItemEvent e) {
    if (e.getStateChange() == ItemEvent.SELECTED) {
        if (e.getSource() == mainComboBox) {
            if (mainComboBox.getSelectedIndex() != 0) {
                FirstDialog firstDialog = new FirstDialog(ComboBoxTwo.this,
                        mainComboBox.getSelectedItem().toString(), "Please wait,  Searching for ..... ");
            }
        } 
    }
}

private class FirstDialog extends JDialog {

    private static final long serialVersionUID = 1L;

    FirstDialog(final Frame parent, String winTitle, String msgString) {
        super(parent, winTitle);
        setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
        JLabel myLabel = new JLabel(msgString);
        JButton bNext = new JButton("Stop Processes");
        add(myLabel, BorderLayout.CENTER);
        add(bNext, BorderLayout.SOUTH);
        bNext.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent evt) {
                setVisible(false);
            }
        });
        javax.swing.Timer t = new javax.swing.Timer(1000, new      
ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                setVisible(false);
            }
        });
        t.setRepeats(false);
        t.start();
        setLocationRelativeTo(parent);
        setSize(new Dimension(400, 100));
        setVisible(true);
    }
}

public static void main(String[] args) {
    JFrame frame = new ComboBoxTwo();
    frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}
} ///////
import java.awt.*;
导入java.awt.event.*;
导入java.util.*;
导入javax.swing.*;
公共类ComboBoxTwo扩展JFrame实现ActionListener、ItemListener{
私有静态最终长serialVersionUID=1L;
专用JComboBox主组合框;
专用JComboBox子omboBox;
私有哈希表子项=新哈希表();
公营机构{
字符串[]项={“选择项”、“颜色”、“形状”、“水果”};
mainComboBox=新的JComboBox(项目);
mainComboBox.addActionListener(这个);
mainComboBox.addItemListener(此);
getContentPane().add(mainComboBox,BorderLayout.WEST);
subComboBox=新的JComboBox();
subComboBox.setPrototypeDisplayValue(“xxxxxxxxx”);//JDK1.4
subComboBox.addItemListener(此);
getContentPane().add(subComboBox,BorderLayout.EAST);
字符串[]子项1={“选择颜色”、“红色”、“蓝色”、“绿色”};
子项目。投入(项目[1],子项目1);
字符串[]子项2={“选择形状”、“圆”、“正方形”、“三角形”};
子项目。投入(项目[2],子项目2);
String[]subItems3={“选择水果”、“苹果”、“橘子”、“香蕉”};
子项目。投入(项目[3],子项目3);
}
@凌驾
已执行的公共无效操作(操作事件e){
String item=(String)mainComboBox.getSelectedItem();
对象o=子项。获取(项);
如果(o==null){
setModel(新的DefaultComboxModel());
}否则{
setModel(新的DefaultComboxModel((字符串[])o));
}
}
@凌驾
公共无效itemStateChanged(ItemEvent e){
如果(如getStateChange()==ItemEvent.SELECTED){
如果(例如getSource()==mainComboBox){
如果(mainComboBox.getSelectedIndex()!=0){
FirstDialog FirstDialog=新建FirstDialog(ComboBoxTwo.this,
mainComboBox.getSelectedItem().toString(),“请稍候,正在搜索…”;
}
} 
}
}
私有类FirstDialog扩展JDialog{
私有静态最终长serialVersionUID=1L;
第一个对话框(最终帧父级、字符串winTitle、字符串msgString){
超级(家长,winTitle);
setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
JLabel myLabel=新的JLabel(msgString);
JButton bNext=新JButton(“停止进程”);
添加(myLabel,BorderLayout.CENTER);
添加(bNext,BorderLayout.SOUTH);
bNext.addActionListener(新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件evt){
setVisible(假);
}
});
javax.swing.Timer t=newjavax.swing.Timer(1000,new
ActionListener(){
@凌驾
已执行的公共无效操作(操作事件e){
setVisible(假);
}
});
t、 设置重复(假);
t、 start();
setLocationRelativeTo(父级);
设置尺寸(新尺寸(400100));
setVisible(真);
}
}
公共静态void main(字符串[]args){
JFrame frame=new ComboBoxTwo();
frame.setDefaultCloseOperation(关闭时退出);
frame.pack();
frame.setLocationRelativeTo(空);
frame.setVisible(true);
}
} ///////
这很有效

package test1;

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

import javax.swing.*;

public class ComboBoxTwo extends JFrame implements ActionListener, ItemListener {

    private static final long serialVersionUID = 1L;
    private JComboBox<String> mainComboBox;
    private JComboBox<String> subComboBox;

    private JLabel label;
    private String[][] descriptions;

    private Hashtable<Object, Object> subItems = new Hashtable<Object, Object> ();

    public ComboBoxTwo() {
        String[] items = {"Select Item", "Color", "Shape", "Fruit"};
        this.getContentPane().setLayout(null);

        this.setSize(400, 400);

        mainComboBox = new JComboBox<String>(items);
        mainComboBox.addActionListener(this);
        mainComboBox.addItemListener(this);
        mainComboBox.setSize(100, 100);
        mainComboBox.setLocation(0, 100);


        subComboBox = new JComboBox<String>();  
        subComboBox.setPrototypeDisplayValue("XXXXXXXXXX"); // JDK1.4
        subComboBox.addItemListener(this);
        subComboBox.setSize(100, 100);
        subComboBox.setLocation(this.getSize().width - 120, 100);


        String[] subItems1 = {"Select Color", "Red", "Blue", "Green"};
        subItems.put(items[1], subItems1);

        String[] subItems2 = {"Select Shape", "Circle", "Square", "Triangle"};
        subItems.put(items[2], subItems2);

        String[] subItems3 = {"Select Fruit", "Apple", "Orange", "Banana"};
        subItems.put(items[3], subItems3);


        label = new JLabel();
        label.setSize(300, 25);
        label.setLocation(this.getSize().width /2 - label.getSize().width /2, 25);
        label.setText("This is a label: currently nothing selected");
        label.setVisible(true);


        getContentPane().add(subComboBox);
        getContentPane().add(mainComboBox);
        getContentPane().add(label);

        descriptions = new String[][]
        { 
            {"The red color", "The blue color", "A green color"}, 
            {"This shape is round", "Looks rectangular", "Has three sides"}, 
            {"Didn't fall far from the tree", "Seems quite orangy", "Look i've got a banana in my trousers"} 
        };    
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String item = (String) mainComboBox.getSelectedItem();
        Object o = subItems.get(item);
        if (o == null) {
            subComboBox.setModel(new DefaultComboBoxModel<String>());
        } else {
            subComboBox.setModel(new DefaultComboBoxModel<String>((String[]) o));
        }
    }

    @SuppressWarnings("unchecked")
    @Override
    public void itemStateChanged(ItemEvent e) {
        if (e.getStateChange() == ItemEvent.SELECTED) {

            if (mainComboBox.getSelectedIndex() == 0 && subComboBox.getSelectedIndex() == 0)
            {
                label.setText("This is a label: currently nothing selected");
            }
            else if (mainComboBox.getSelectedIndex() >= 0)
            {
                label.setText("The first combobox has been altered");
            }

            if (e.getSource() == mainComboBox) {
                if (mainComboBox.getSelectedIndex() != 0) {
                    FirstDialog firstDialog = new FirstDialog(ComboBoxTwo.this,
                            mainComboBox.getSelectedItem().toString(), "Please wait,  Searching for ..... ");
                }
            }
            else if (e.getSource() == subComboBox)
            {
                label.setText(descriptions[mainComboBox.getSelectedIndex() -1][((JComboBox<String>) e.getSource()).getSelectedIndex() -1]);
            }
        }
    }

    private class FirstDialog extends JDialog {

        private static final long serialVersionUID = 1L;

        FirstDialog(final Frame parent, String winTitle, String msgString) {
            super(parent, winTitle);
            setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
            JLabel myLabel = new JLabel(msgString);
            JButton bNext = new JButton("Stop Processes");
            add(myLabel, BorderLayout.CENTER);
            add(bNext, BorderLayout.SOUTH);
            bNext.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent evt) {
                    setVisible(false);
                }
            });
            javax.swing.Timer t = new javax.swing.Timer(1000, new  ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    setVisible(false);
                }
            });
            t.setRepeats(false);
            t.start();
            setLocationRelativeTo(parent);
            setSize(new Dimension(400, 100));
            setVisible(true);
        }
    }

    public static void main(String[] args) {
        JFrame frame = new ComboBoxTwo();
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}
包测试1;
导入java.awt.*;
导入java.awt.event.*;
导入java.util.*;
导入javax.swing.*;
公共类ComboBoxTwo扩展JFrame实现ActionListener、ItemListener{
私有静态最终长serialVersionUID=1L;
专用JComboBox主组合框;
专用JComboBox子omboBox;
私人标签;
私有字符串[][]描述;
私有哈希表子项=新哈希表();
公营机构{
字符串[]项={“选择项”、“颜色”、“形状”、“水果”};
this.getContentPane().setLayout(null);
这个。设置大小(400400);
mainComboBox=新的JComboBox(项目);
mainComboBox.addActionListener(这个);
mainComboBox.addItemListener(此);
mainComboBox.setSize(100100);
mainComboBox.setLocation(01100);
subComboBox=新的JComboBox();
subComboBox.setPrototypeDisplayValue(“xxxxxxxxx”);//JDK1.4
subComboBox.addItemListener(此);
子接线盒设置尺寸(100100);
setLocation(this.getSize().width-120100);
字符串[]子项1={“选择颜色”、“红色”、“蓝色”、“绿色”};
子项目。投入(项目[1],子项目1);
字符串[]子项2={“选择形状”、“圆”、“正方形”、“三角形”};
子项目。投入(项目[2],子项目2);
String[]subItems3={“选择水果”、“苹果”、“橘子”、“香蕉”};
子项目。投入(项目[3],子项目3);
label=新的JLabel();
标签。设置尺寸(300,25);
label.setLocation(this.getSize().width/2-label.getSize().width/2,25);
label.setText(“这是一个标签:当前未选择任何内容”);
label.setVisible(true);
getContentPane().add(子omboBox);
getContentPane().add(主组合框);
getContentPane().add(标签);