Applets

I need to have an applet that has two JLabels with two JTextField and a button wtih an event handler that that prints the message "I got clicked"

import javax.swing.*;

import java.awt.*;

import java.io.*;

import java.awt.event.*;

publicclass SimpleApplet2extends JApplet{

public SimpleApplet2(){

}

privateclass MyButtonClick inplements ActionListener{

publicvoid actionPerformed(ActionEvent myClick)

{

System.out.println("I got clicked!");

}// end method action performed

}// end class mybuttonclick

publicvoid start(){

SwingUtilities.invokeLater(new Runnable(){

publicvoid run(){// run in the event thread . . .

JPanel p =new JPanel();

JTextField field =new JTextField();

p.setLayout(new GridLayout(4, 4, 4, 4));

p.add(new JLabel("Field 1"));

p.add(new JTextField());

p.add(new JLabel("Field 2"));

p.add(new JTextField());

p.add(new JButton("ok") );

Container content = getContentPane();

content.setLayout(new GridBagLayout());// used to center the panel

content.add(p);

content.add(field);

validate();

}

});

}

}

Error Messages:

F:\csc148>javac SimpleApplet2.java

SimpleApplet2.java:19: '{' expected

private class MyButtonClick inplements ActionListener {

^

SimpleApplet2.java:49: '}' expected

^

2 errors

[2940 byte] By [Dwooda] at [2007-10-2 16:19:58]
# 1

Hi,

you were quite close:

first change your private class definition so that the misspelling of "implements" is out of the way:

private class MyButtonClick implements ActionListener {

Then all you need to do is to use the ActionListener for your Button:

public void run() { // run in the event thread . . .

JPanel p = new JPanel();

JTextField field = new JTextField();

p.setLayout(new GridLayout(4, 4, 4, 4));

p.add(new JLabel("Field 1"));

p.add(new JTextField());

p.add(new JLabel("Field 2"));

p.add(new JTextField());

JButton jb = new JButton("ok");

jb.addActionListener(new MyButtonClick());

p.add(jb);

Container content = getContentPane();

content.setLayout(new GridBagLayout()); // used to center the panel

content.add(p);

content.add(field);

validate();

}

I think this works,

Rene

trhtrhrthta at 2007-7-13 17:14:10 > top of Java-index,Java Essentials,New To Java...