简体   繁体   English

从JComboBox获取值时出错

[英]error to get the value from JComboBox

I created a JComboBox and I want to get the value from it. 我创建了一个JComboBox,我想从中获取价值。 Here is the code that I wrote: 这是我编写的代码:

        final JComboBox jc = new JComboBox();
        jc.addItem("ARR");
        jc.addItem("SRR");

        myPanel.add(jc, gbc);
        String pat_order;

        jc.addItemListener(new ItemListener() {
            public void itemStateChanged(ItemEvent ie) {
                String order = (String) jc.getSelectedItem();
                pat_order = order;
            }
        });

and the problem is that I got an error: "local variable pat_order is accessed from whitin inner class; needs to be declared final" and when I put final in pat_order declaration, get another error: "cannot assign a value to a final variable pat_order". 问题是我得到一个错误:“从whitin内部类访问局部变量pat_order;需要声明为final”,当我将final放在pat_order声明中时,出现另一个错误:“无法为最终变量pat_order赋值”。 Sorry I'm totally beginner in Java GUI. 抱歉,我完全是Java GUI的初学者。 Thanks in advance. 提前致谢。

Here is your program, it is working fine, I don know why are you getting the error, even after you made the pat_Order final(Private) , I am just using the static method for pat_Order. 这是您的程序,运行良好,我不知道为什么会收到错误,即使您使pat_Order final(Private)之后 ,我也只是将静态方法用于pat_Order。 To show that, you have selected things from the combo box, I added, dialogbox , which shows the thing, that you selected ,,. 为了显示这一点,您从组合框中选择了东西,我添加了dialogbox ,它显示了您选择的东西。 :) if you have any further question, about my program, feel free to ask me, import java.awt.event.ItemListener; :)如果您对我的程序还有其他疑问,请随时问我,导入java.awt.event.ItemListener;

import javax.swing.*;

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


public class comboBoxProblem  extends JFrame{

static JPanel myPanel  = new JPanel();
static String pat_order;
public static void main(String [] args)
    {
        new comboBoxProblem().show(); 

    }
public comboBoxProblem()
    {
        setTitle("Combo");
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        getContentPane().setLayout(new GridBagLayout());


        GridBagConstraints gbc = new GridBagConstraints();
        myPanel.setLayout(new GridBagLayout());
        myPanel.setBorder(BorderFactory.createTitledBorder("Button's"));

        final JComboBox jc = new JComboBox(); 
        jc.addItem("ARR");
        jc.addItem("SRR");

        gbc.gridx = 0;
        gbc.gridy = 1;
        jc.addItemListener(new ItemListener() 
            {
                public void itemStateChanged(ItemEvent ie)
                    {
                        String order = (String) jc.getSelectedItem();
                        pat_order = order;
                        JOptionPane.showConfirmDialog(null, pat_order, " Message Dialog Box", JOptionPane.DEFAULT_OPTION    );
                    }
            });

        myPanel.add(jc, gbc);


        gbc.gridx = 1;
        gbc.gridy = 0;

        getContentPane().add(myPanel, gbc);



pack();
 } 

 }

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

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