I was creating a swing applet if i make a seperate class of action listener i was unable to access extended class variables.Can someone explain my mistake and modify the code without merging two classes.
Code:
import javax.swing.*;
import java.awt.*;
public class Clock extends JApplet{
JFrame frame;
JPanel panel;
JButton button;
public void init(){
frame = new JFrame();
frame.setSize(700,600);
panel = new JPanel();
button = new JButton();
button.addActionListener(new listener()); //if i use seperate class listener instead of writing code here itself and try to access the variables of this class null pointer exception occurs
button.setText("aaaaaaa");
panel.add(button);
frame.add(panel);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Code:
import java.awt.event.*;
class listener extends Clock implements ActionListener {
public void actionPerformed(ActionEvent e){
button.setText("bbbbbbbbbbb");
}
}
Comment