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 36 37 38
| public static void main(String[] args) {
final JButton boutonMiseEnMarche = new JButton("Démarrer");
final JButton boutonMiseATable = new JButton("Arrêter");
final JLabel labelEtat = new JLabel("Je suis en train de dormir");
final Timer timer = new Timer(3000, new ActionListener() {
public void actionPerformed(ActionEvent evt) {
labelEtat.setText("Je n'ai plus faim");
}
});
timer.setRepeats(false);
ActionListener a = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
Object source = evt.getSource();
if (source == boutonMiseEnMarche) {
labelEtat.setText("Je vais bien");
timer.start();
} else if (source == boutonMiseATable) {
if (timer.isRunning()) {
timer.stop();
labelEtat.setText("J'avais encore faim !!!");
}
}
}
};
boutonMiseEnMarche.addActionListener(a);
boutonMiseATable.addActionListener(a);
JFrame f = new JFrame("test");
f.getContentPane().add(boutonMiseEnMarche, BorderLayout.WEST);
f.getContentPane().add(boutonMiseATable, BorderLayout.EAST);
f.getContentPane().add(labelEtat);
f.pack();
f.setVisible(true);
} |