Don't you get a compile error from that?
frame.add(new TextDemo());
There are a few things wrong with this, but ultimately it just needs to be removed. (
TextDemo is called in the
NewClass2 constructor.)
For that matter, you don't need the
TextDemo method at all, because you never reuse the logic it contains. So put its contents into the
NewClass2 constructor, like so:
public NewClass2() {
//...
//Add Components to this container, using the default FlowLayout.
textField = new JTextField(20);
textField.addActionListener(this);
add(b1);
}
Also, I don't see that the text field is ever added to the panel, so pass it to
add -
before add(b1) so the components are arranged in that order.
public NewClass2() {
//...
//Add Components to this container, using the default FlowLayout.
textField = new JTextField(20);
textField.addActionListener(this);
add(textField);
add(b1);
}
I believe that will do what you want.
EDIT: Woops, just looked and saw the post conflict.