Interaction bouton application
le code de la fenetre:
Code:
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 39 40 41 42 43 44 45 46 47 48
|
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class FenInter extends JFrame implements ActionListener
{
private BoutonInter BIr = new BoutonInter("mon bouton");
private JPanel ContInter = new JPanel();
private JLabel label = new JLabel(" 0 0 0 0 0 0 0 ");
private int compteur = 0; // on initialise le compteur de clics à 0 !
public FenInter()
{
this.setTitle("Interation_bouton");
this.setSize(900,300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
ContInter.setBackground(Color.black);
ContInter.setLayout(new BorderLayout());
ContInter.add(BIr, BorderLayout.SOUTH);
Font police = new Font("DigifaceWide", Font.BOLD, 40 );
label.setFont(police);
label.setForeground(Color.red);
label.setHorizontalAlignment(JLabel.CENTER);
ContInter.add(label, BorderLayout.NORTH);
this.setContentPane(ContInter);
this.setVisible(true);
}
public void actionPerformed(ActionEvent arg0)
{
this.compteur++;
label.setText(" Vous avez cliqué " + this.compteur + " fois");
}
} |
le code du bouton :
Code:
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 39 40 41 42 43 44
|
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class BoutonInter extends JButton implements ActionListener
{
private String name;
public BoutonInter(String strr)
{
super(strr);
this.name = strr;
this.addActionListener(this);
}
public void paintComponent(Graphics gr)
{
Graphics2D g2dr = (Graphics2D)gr;
GradientPaint gpr = new GradientPaint(0, 0, Color.gray, 0, 10, Color.gray, true);
g2dr.setPaint(gpr);
g2dr.fillRect(0, 0, this.getWidth(), this.getHeight());
g2dr.setColor(Color.green);
g2dr.drawString(this.name, this.getWidth() / 2 - (this.getWidth() / 2 /4), (this.getHeight() / 2) + 5);
}
} |
Bonjour,
j'ai un petit problème avec mon code je veux incrémenter le compteur à chaque clic sur le bouton mais y a quelque chose que ne va pas... quelqu'un peut me dire qu'est-ce qui manque? :roll:
Merci d'avance de m'avoir aider
A+