坚持访问非最终变量?

时间:2013-02-24 21:26:17

标签: java eclipse variables user-interface final

我只是在测试我的代码而我正试图这样做当我点击“推送”按钮时,第一个textArea打印出“Hello”。

但是我一直收到这个错误:不能在不同方法中定义的内部类中引用非final变量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);
}

}

1 个答案:

答案 0 :(得分:6)

然后改变这个:

JTextArea textArea = new JTextArea();

到此:

final JTextArea textArea = new JTextArea();

这是使用anonymous inner classes ...;)

时必须执行的操作