A demo:
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
class Test extends JFrame {
private int i;
private JButton button;
private JButton button2;
public Test() {
initComponents();
}
private void initComponents() {
i = 0;
button = new JButton("Increment I");
button2 = new JButton("Show Value");
button.setSize(100, 20);
button2.setSize(100, 20);
button.setLocation(5, 5);
button2.setLocation(5, 30);
this.getContentPane().setLayout(null);
this.getContentPane().add(button);
this.getContentPane().add(button2);
this.pack();
this.setSize(300, 300);
this.setLocationRelativeTo(null);
this.setTitle("Demo");
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
i++;
}
});
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
System.out.println("i = " + i);
}
});
}
public static void main(String[] argv) {
new Test().setVisible(true);
}
}