Here is the code;
public class Outer extends JApplet
{
private Inner top;
private JPanel face = new JPanel();
public void init()
{
getContentPane().setLayout(new BorderLayout());
top = new Outer().new Inner();
face.setBackground(Color.blue);
getContentPane().add(top, BorderLayout.NORTH);
getContentPane().add(face);
}
class Inner extends JPanel() implements ActionListener
{
JButton btn = new JButton("Button");
btn.addActionListener(this); //line one
add(btn);
public void actionPerformed(ActionEvent e)
{
face.setBackground(Color.red); //line two
}
}
}
This code can be compiled. With appletviewer, the
applet can be showd. But when click the button, there is some error.
If I command out line two, the problem still there. It looks like there is something wrong with line one. If I implemented the action listener as inner class, there is no problem without line two. The button can be clicked without error. If the line two comes back, the same error is still there.