1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| public class Demo extends JPanel implements ActionListener {
private JLabel label = new JLabel();
private int compteur;
public Demo() {
super(new GridLayout(0,1));
JLabel label = new JLabel(String.valueOf(compteur), JLabel.CENTER);
JButton button = new JButton("+1");
button.addActionListener(this);
add(label);
add(button);
}
@Override
public void actionPerformed(ActionEvent e) {
compteur++;
System.out.println("Nouveau compteur: " + compteur);
label.setText(String.valueOf(compteur));
}
public static void main(String[] args) {
JFrame frame = new JFrame("Démo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new Demo());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
} |