Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从JComboBox获取值_Java_Swing_Jbutton_Jcombobox_Listcellrenderer - Fatal编程技术网

Java 从JComboBox获取值

Java 从JComboBox获取值,java,swing,jbutton,jcombobox,listcellrenderer,Java,Swing,Jbutton,Jcombobox,Listcellrenderer,我有两列JComboBox,还有JButton。当我单击JButton时,我需要分别从第一列和第二列获取JComboBox所选值的结果 我该怎么做 另外:如何设置JComboBox的标题 守则: public class Combo extends JFrame implements ActionListener{ private JComboBox combo = new JComboBox(); private JButton button = new JButton();

我有两列
JComboBox
,还有
JButton
。当我单击
JButton
时,我需要分别从第一列和第二列获取
JComboBox
所选值的结果

我该怎么做

另外:如何设置JComboBox的标题

守则:

 public class Combo extends JFrame implements ActionListener{
    private JComboBox combo = new JComboBox();
    private JButton button = new JButton();
    public Combo() {

        setLayout(new FlowLayout());
        combo.setRenderer(new render());

        add(combo);

        combo.addItem(new String[] {"1","bbb"});
        combo.addItem(new String[] {"2","ff"});
        combo.addItem(new String[] {"3","gg"});
        combo.addItem(new String[] {"4","ee"});

        add(button);
        button.addActionListener(this);
        pack();
    }


    public static void main(String[]args){
        new Combo().setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==button){
            System.out.println(combo.getSelectedItem());
        }
    }
}
class render extends JPanel implements ListCellRenderer{

    private JLabel label1 = new JLabel();
    private JLabel label2 = new JLabel();
    private JLabel label3 = new JLabel();
    private JLabel label4 = new JLabel();
    private JLabel label5 = new JLabel();

    public render() {
        setLayout(new GridLayout(2,5));
        add(label1);
        add(label2);   
    }

    @Override
    public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
        String[] values = (String[]) value;
        label1.setText(values[0]);
        label2.setText(values[1]);
        if(index ==0){
            label1.setForeground(Color.red);
            label2.setForeground(Color.red);
        }else{
            label1.setForeground(Color.white);
            label2.setForeground(Color.white);
        }

        return this;
    }

}

谢谢。

您的项目是字符串数组,因此您可以按如下方式打印所选项目:

System.out.println(Arrays.toString((String[])combo.getSelectedItem()));
编辑:

String[]selectedItem=(String[])组合。getSelectedItem();
for(int i=0;i

或者,如果您只需要第一项-
(String[])combo.getSelectedItem())[0]
,则可以很快使用

要显示选定元素的第一个值,可以使用:

System.out.println(((String[])combo.getSelectedItem())[0]);

真的非常感谢。。它起作用了。。但如果我只需要第一个值怎么办?我需要单独的值。。非常感谢。。但是如何设置它的标题呢?为许多请求道歉。@JasonAmavisca什么标题?:)(我不知道怎么称呼它)从jcombobox呈现的jlist/list的头。。。或者至少使索引值不为0selectable@JasonAmavisca我不太明白。标题是指
JComboBox
中显示的值吗?你想展示什么?你有一个2,5网格面板是一个渲染器。非常感谢它真的很有帮助。。但我可以设置它的标题吗?问题是两个问题,标题不是一个适用于JComboBox的术语。您必须给出一个示例或上下文,说明该术语有意义(但不能作为其他问题的一部分)。可以理解的是,这些答案没有显示出理解问题第二部分的能力。您应该编辑该问题以删除第二个问题,并从评论中删除该附加问题的重复部分。试着分别寻找第二个问题,如果这个问题不存在,可以作为单独的原始帖子而不是评论来提问。
System.out.println(((String[])combo.getSelectedItem())[0]);