import javax.swing.*;
import java.awt.event.*;
public class SimpleGUI3 implements ActionListener {
JButton button;
private int numClick;
public static void main(String[] args) {
SimpleGUI3 gui = new SimpleGUI3();
gui.go();
}
public void go() {
JFrame frame = new JFrame();
button = new JButton("Click me.");
button.addActionListener(this);
frame.getContentPane().add(button);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 300);
frame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
button.setLocation(100, 100); //This code do not change the button location if numClick++ (next row) used.
numClick++; //If comment numClick++ the button changes location on click. Why location doesn't changes if this row uncomment?
button.setText("Has been clicked " + numClick + " times.");
}
}
The question is: Why location changes on click without numClick++ in code and why the button ocation doesn't changes if numClick++ work in code?