// // exemple d'une application graphique interactive avec un JFrame // on utilise ici les "listener" anonymes // 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 FrameAttentif2 extends JPanel{ 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"); FrameAttentif2(){ setLayout(new BorderLayout()); add(BorderLayout.NORTH,bPlus); add(BorderLayout.CENTER,l); add(BorderLayout.SOUTH,bRAZ); // listeners anonymes.... bPlus.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ val++; miseAJour(); }}); bRAZ.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ val=0; miseAJour(); }}); } private void miseAJour(){ l.setText("Nombre de clic"+(val>1?"s":"")+"="+val); l.repaint(); } } public class CompteurClic2App { public static void main(String[] args){ JFrame f = new JFrame(); f.getContentPane().add(new FrameAttentif2()); f.setSize(300,150); f.setLocation(100,100); f.setVisible(true); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }