Newbie: Whats wrong with this little program?

From: Piet (pit.grinja_at_gmx.de)
Date: 03/27/04


Date: 26 Mar 2004 23:41:10 -0800

Hi there!
I´ve started to learn Java. Right at the beginning, I am stuck. To get
an idea of the Java class possibilities, I tried to write a program
that would write some text in a textfield when a buton is clicked.
After some experimentation, the program could be compiled
successfully, but when the button is clicked, I get a ver long list of
errors. I use JCreator LE v 2.8 build 8 and the SUN Java SDK 1.42.
Here is the source:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class JavaTest extends JFrame {
private JTextArea myTextField;
        class myButtonEvent implements ActionListener {
                public void actionPerformed(ActionEvent evt) {
                        java.awt.Toolkit.getDefaultToolkit().beep();
                        myTextField.setText("Hi there!");
                }
        }
        JavaTest(String title) {
        super(title);
                JLabel myLabel = new JLabel("Label");
                JButton myButton = new JButton("Test");
        myButton.addActionListener(new myButtonEvent());
                getContentPane().setLayout(new FlowLayout());
                getContentPane().add(myLabel);
                getContentPane().add(myButton);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JTextArea myTextField = new JTextArea(5,20);
        getContentPane().add(myTextField);
        }
        public static void main(String[] args) {
                JavaTest rahmen = new JavaTest("Test");
                rahmen.pack();
                rahmen.show();
        }
}

I think (or hope) that this error should be easily corrected. Any help
and explanations will be greatly appreciated! Thanx in advance

Peter


Quantcast