2013-02-24 4 views
0

저는 코드를 테스트하고 있는데, "푸시"버튼을 클릭하면 첫 번째 textArea가 "Hello"를 출력합니다.비 최종 변수에 액세스하지 못하셨습니까?

하지만이 오류가 계속은 :

import java.awt.BorderLayout; 

공용 클래스 WindowBuilderTest이 JFrame의 확장 : 다른 방법 여기

에 정의 된 내부 클래스 내부가 아닌 최종 변수 textArea의 참조 할 수 없음 내 코드입니다 {

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); 
} 

}

답변

6

다음 회 변경 이다 : 이것에

JTextArea textArea = new JTextArea(); 

:

final JTextArea textArea = new JTextArea(); 

이것은 당신이 anonymous inner classes를 사용할 때해야 할 뭔가입니다 ...) 그것은 일

+0

! 고맙습니다! – Rashesh

+0

이 줄에 :'JTextArea textArea = new JTextArea();'앞에 final이라는 단어를 추가하십시오. –

+0

줄을 수정했습니다 ... 'JTextArea textArea'를 찾으십시오 – vikingsteve

관련 문제