// // exemple d'une application graphique interactive avec un JFrame // import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.BorderLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; class FrameAttentif extends JPanel implements ActionListener{ private int val=0; private JLabel l = new JLabel("Nombre de clic=0",JLabel.CENTER); private JButton bPlus = new JButton("++"); private JButton bRAZ = new JButton("RAZ"); FrameAttentif(){ setLayout(new BorderLayout()); add(BorderLayout.NORTH,bPlus); add(BorderLayout.CENTER,l); add(BorderLayout.SOUTH,bRAZ); // ajout des "listeners" bPlus.addActionListener(this); bRAZ.addActionListener(this); } public void actionPerformed(ActionEvent e){ if(e.getSource()==bRAZ) val=0; else val++; l.setText("Nombre de clic"+(val>1?"s":"")+"="+val); l.repaint(); } } public class CompteurClicApp { public static void main(String[] args){ JFrame f = new JFrame(); f.getContentPane().add(new FrameAttentif()); f.setSize(300,150); f.setLocation(100,100); f.setVisible(true); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }