I want to have a button that when pressed, a method will be invoked. Here is some code
public class Tester extends JPanel {
public void paintComponent(Graphics g){
super.paintComponent(g);
g.fillRect(10, 10, 10, 10);
}
//The method which I wand invoke but can't, I don't know why.
void setWidth(){
//width -= 10 ;
repaint();
}
public static void main(String[] args) {
//Here some JFrame...
Tester paint = new Tester();
frame.add(paint);
JButton click = new JButton("Click");
click.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
paint.setWidth();//Here I Want invoke that method
}
});
}
}
I want that ActionListener
will invoke the method setWidth()
, but it doesn't. Don't know why.