Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/8.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 无法访问非最终变量?_Java_Eclipse_Variables_User Interface_Final - Fatal编程技术网

Java 无法访问非最终变量?

Java 无法访问非最终变量?,java,eclipse,variables,user-interface,final,Java,Eclipse,Variables,User Interface,Final,我只是在测试我的代码,我正在尝试这样做,当我点击“按下”按钮时,第一个文本区域会打印出“你好” 但我经常遇到这样的错误:无法在不同方法中定义的内部类中引用非最终变量textArea 这是我的密码: import java.awt.BorderLayout; 公共类WindowBuilderTest扩展了JFrame{ private JPanel contentPane; private JTextField textField; /** * Launch the application.

我只是在测试我的代码,我正在尝试这样做,当我点击“按下”按钮时,第一个文本区域会打印出“你好”

但我经常遇到这样的错误:无法在不同方法中定义的内部类中引用非最终变量textArea

这是我的密码:

import java.awt.BorderLayout;
公共类WindowBuilderTest扩展了JFrame{

private JPanel contentPane;
private JTextField textField;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                WindowBuilderTest frame = new WindowBuilderTest();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public WindowBuilderTest() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 400, 400);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    GridBagLayout gbl_contentPane = new GridBagLayout();
    gbl_contentPane.columnWidths = new int[]{90, 92, 95, 95, 0};
    gbl_contentPane.rowHeights = new int[]{64, 74, 83, 74, 12, 0};
    gbl_contentPane.columnWeights = new double[]{1.0, 1.0, 1.0, 1.0, Double.MIN_VALUE};
    gbl_contentPane.rowWeights = new double[]{1.0, 0.0, 0.0, 1.0, 1.0, Double.MIN_VALUE};
    contentPane.setLayout(gbl_contentPane);

    JTextArea textArea = new JTextArea();
    textArea.setEditable(false);
    GridBagConstraints gbc_textArea = new GridBagConstraints();
    gbc_textArea.gridheight = 2;
    gbc_textArea.insets = new Insets(0, 0, 5, 5);
    gbc_textArea.fill = GridBagConstraints.BOTH;
    gbc_textArea.gridx = 0;
    gbc_textArea.gridy = 0;
    contentPane.add(textArea, gbc_textArea);

    JLabel lblstack = new JLabel("<-- Stack");
    GridBagConstraints gbc_lblstack = new GridBagConstraints();
    gbc_lblstack.gridheight = 2;
    gbc_lblstack.anchor = GridBagConstraints.WEST;
    gbc_lblstack.insets = new Insets(0, 0, 5, 5);
    gbc_lblstack.gridx = 1;
    gbc_lblstack.gridy = 0;
    contentPane.add(lblstack, gbc_lblstack);

    JLabel lblNewLabel = new JLabel("Result -->");
    GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
    gbc_lblNewLabel.anchor = GridBagConstraints.EAST;
    gbc_lblNewLabel.gridheight = 2;
    gbc_lblNewLabel.insets = new Insets(0, 0, 5, 5);
    gbc_lblNewLabel.gridx = 2;
    gbc_lblNewLabel.gridy = 0;
    contentPane.add(lblNewLabel, gbc_lblNewLabel);

    JTextArea textArea_2 = new JTextArea();
    textArea_2.setEditable(false);
    GridBagConstraints gbc_textArea_2 = new GridBagConstraints();
    gbc_textArea_2.gridheight = 2;
    gbc_textArea_2.insets = new Insets(0, 0, 5, 0);
    gbc_textArea_2.fill = GridBagConstraints.BOTH;
    gbc_textArea_2.gridx = 3;
    gbc_textArea_2.gridy = 0;
    contentPane.add(textArea_2, gbc_textArea_2);

    textField = new JTextField();
    GridBagConstraints gbc_textField = new GridBagConstraints();
    gbc_textField.anchor = GridBagConstraints.SOUTH;
    gbc_textField.gridwidth = 2;
    gbc_textField.insets = new Insets(0, 0, 5, 5);
    gbc_textField.fill = GridBagConstraints.HORIZONTAL;
    gbc_textField.gridx = 1;
    gbc_textField.gridy = 2;
    contentPane.add(textField, gbc_textField);
    textField.setColumns(10);

    JLabel lblenterInteger = new JLabel("<-- Enter Integer");
    GridBagConstraints gbc_lblenterInteger = new GridBagConstraints();
    gbc_lblenterInteger.anchor = GridBagConstraints.SOUTHWEST;
    gbc_lblenterInteger.insets = new Insets(0, 0, 5, 0);
    gbc_lblenterInteger.gridx = 3;
    gbc_lblenterInteger.gridy = 2;
    contentPane.add(lblenterInteger, gbc_lblenterInteger);

    JButton btnNewButton = new JButton("Push");
    btnNewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {

            String TEST = "Hello";

            textArea.setText(TEST);

        }
    });
    GridBagConstraints gbc_btnNewButton = new GridBagConstraints();
    gbc_btnNewButton.fill = GridBagConstraints.BOTH;
    gbc_btnNewButton.insets = new Insets(0, 0, 5, 5);
    gbc_btnNewButton.gridx = 0;
    gbc_btnNewButton.gridy = 3;
    contentPane.add(btnNewButton, gbc_btnNewButton);

    JButton btnNewButton_1 = new JButton("Pop");
    GridBagConstraints gbc_btnNewButton_1 = new GridBagConstraints();
    gbc_btnNewButton_1.fill = GridBagConstraints.BOTH;
    gbc_btnNewButton_1.insets = new Insets(0, 0, 5, 5);
    gbc_btnNewButton_1.gridx = 1;
    gbc_btnNewButton_1.gridy = 3;
    contentPane.add(btnNewButton_1, gbc_btnNewButton_1);

    JButton btnNewButton_2 = new JButton("Peek");
    GridBagConstraints gbc_btnNewButton_2 = new GridBagConstraints();
    gbc_btnNewButton_2.fill = GridBagConstraints.BOTH;
    gbc_btnNewButton_2.insets = new Insets(0, 0, 5, 5);
    gbc_btnNewButton_2.gridx = 2;
    gbc_btnNewButton_2.gridy = 3;
    contentPane.add(btnNewButton_2, gbc_btnNewButton_2);

    JButton btnNewButton_3 = new JButton("Exit");
    GridBagConstraints gbc_btnNewButton_3 = new GridBagConstraints();
    gbc_btnNewButton_3.fill = GridBagConstraints.BOTH;
    gbc_btnNewButton_3.insets = new Insets(0, 0, 5, 0);
    gbc_btnNewButton_3.gridx = 3;
    gbc_btnNewButton_3.gridy = 3;
    contentPane.add(btnNewButton_3, gbc_btnNewButton_3);

    JTextArea textArea_1 = new JTextArea();
    textArea_1.setEditable(false);
    GridBagConstraints gbc_textArea_1 = new GridBagConstraints();
    gbc_textArea_1.gridwidth = 4;
    gbc_textArea_1.fill = GridBagConstraints.BOTH;
    gbc_textArea_1.gridx = 0;
    gbc_textArea_1.gridy = 4;
    contentPane.add(textArea_1, gbc_textArea_1);
}
私有JPanel内容窗格;
私有JTextField textField;
/**
*启动应用程序。
*/
公共静态void main(字符串[]args){
invokeLater(新的Runnable(){
公开募捐{
试一试{
WindowBuilderTest框架=新建WindowBuilderTest();
frame.setVisible(true);
}捕获(例外e){
e、 printStackTrace();
}
}
});
}
/**
*创建框架。
*/
公共WindowBuilderTest(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
立根(100100400400);
contentPane=newjpanel();
setboorder(新的EmptyBorder(5,5,5,5));
setContentPane(contentPane);
GridBagLayout gbl_contentPane=新的GridBagLayout();
gbl_contentPane.columnWidths=newint[]{90,92,95,95,0};
gbl_contentPane.rowHeights=newint[]{64,74,83,74,12,0};
gbl_contentPane.columnWeights=newdouble[]{1.0,1.0,1.0,double.MIN_VALUE};
gbl_contentPane.rowWeights=newdouble[]{1.0,0.0,1.0,1.0,double.MIN_VALUE};
setLayout(gbl_contentPane);
JTextArea textArea=新的JTextArea();
textArea.setEditable(false);
GridBagConstraints gbc_textArea=新的GridBagConstraints();
gbc_textArea.gridheight=2;
gbc_textArea.insets=新的insets(0,0,5,5);
gbc_textArea.fill=GridBagConstraints.BOTH;
gbc_textArea.gridx=0;
gbc_textArea.gridy=0;
contentPane.add(textArea,gbc_textArea);
JLabel lblstack=新的JLabel(“”);
GridBagConstraints GBCLBLNEWLABEL=新的GridBagConstraints();
gbc_lblNewLabel.anchor=gridbagstraints.EAST;
gbc_lblNewLabel.gridheight=2;
gbc_lblNewLabel.insets=新的插入(0,0,5,5);
gbc_lblNewLabel.gridx=2;
gbc_lblNewLabel.gridy=0;
contentPane.add(lblNewLabel,gbc_lblNewLabel);
JTextArea textArea_2=新的JTextArea();
text区域_2.设置可编辑(假);
GridBagConstraints gbc_textArea_2=新的GridBagConstraints();
gbc_textArea_2.gridheight=2;
gbc_textArea_2.insets=新的insets(0,0,5,0);
gbc_textArea_2.fill=GridBagConstraints.BOTH;
gbc_textArea_2.gridx=3;
gbc_textArea_2.gridy=0;
contentPane.add(textArea_2,gbc_textArea_2);
textField=新的JTextField();
GridBagConstraints gbc_textField=新的GridBagConstraints();
gbc_textField.anchor=GridBagConstraints.SOUTH;
gbc_textField.gridwidth=2;
gbc_textField.insets=新的insets(0,0,5,5);
gbc_textField.fill=GridBagConstraints.HORIZONTAL;
gbc_textField.gridx=1;
gbc_textField.gridy=2;
contentPane.add(textField,gbc_textField);
textField.setColumns(10);
JLabel lblenterInteger=新的JLabel(“然后更改此项:

JTextArea textArea = new JTextArea();
为此:

final JTextArea textArea = new JTextArea();
这只是使用
匿名内部类时必须执行的操作
…;)

然后更改此操作:

JTextArea textArea = new JTextArea();
为此:

final JTextArea textArea = new JTextArea();

这只是在这一行中使用
匿名内部类时必须执行的操作:
JTextArea textArea=new JTextArea();
在它前面添加final一词。我修改了你的一行……查找
JTextArea textArea
我也在尝试这样做,当我按下“推”键时“按钮,它将文本字段中的任何内容添加到文本区域,但作为列表。基本上,如果我键入3,然后键入4,然后键入5,我希望文本区域显示:3 4 5,但将它们堆叠在一起。在这一行:
JTextArea textArea=new JTextArea()
在它前面加上final一词。我修改了你的一行……查找
JTextArea textArea
当我按下“推”键时,我也在尝试这样做按钮,它将文本字段中的任何内容添加到文本区域,但作为列表。基本上,如果我键入3,然后键入4,然后键入5,我希望文本区域显示:3 4 5,但将它们堆叠在一起。