0
我只是測試了我的代碼,我試圖做到這一點,當我點擊「推」按鈕,第一個textArea打印出「你好」。卡住訪問非最終變量?
,但我不斷收到此錯誤:無法指非最終變量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);
}
}
它的工作!謝謝! – Rashesh 2013-02-24 21:28:02
在這一行上:'JTextArea textArea = new JTextArea();'在它之前添加最終的單詞。 – 2013-02-24 21:28:43
我修改了其中一行...尋找'JTextArea textArea' – vikingsteve 2013-02-24 21:28:47