Image sur des boutons

cs_GREENDAYZER Messages postés 5 Date d'inscription mardi 15 juillet 2003 Statut Membre Dernière intervention 18 août 2003 - 18 août 2003 à 14:43
benohite Messages postés 210 Date d'inscription vendredi 11 avril 2003 Statut Membre Dernière intervention 18 décembre 2003 - 18 août 2003 à 15:48
Comment faire pour inserer une image sur des boutons d'un GridBagLayout en utilisant makeButton?
J'ai utilisé ça mais ça ne marche pas qlq connait la reponse Merci!

public class Max
{
static void makeBoboLayout() {
Frame f = new Frame("GridBagLayout Example 1");
f.setFont(new Font("Helvetica", Font.PLAIN, 12));
f.setForeground(Color.black);
f.setBackground(Color.yellow);
//resize(1600,1200);
//f.setSize(f.getPreferredSize());
f.setLayout(new GridBagLayout());

makeButton (new ImageIcon("bingo.gif"),f, "xxxx", 0, 0, 32,1, 1.0, 1.0, Color.white );
/*makeButton(f, "xxxx", 0, 1, 1, 2, 1.0, 1.0,Color.red); makeButton(f, "xxxxx", 0, 3, 1, 2, 1.0, 1.0,Color.red);
makeButton(f, "11", 1, 1, 1, 1, 1.0, 1.0,Color.red); makeButton(f, "C1", 1, 3, 1, 1, 0.0, 1.0,Color.yellow);
makeButton(f, "12", 1, 2, 1, 1, 1.0, 1.0,Color.red); makeButton(f, "D1", 1, 4, 1, 1, 0.0, 1.0,Color.black);
makeButton(f, "WF Int", 2, 1, 1, 2, 1.0, 1.0,Color.red); makeButton(f, "Axxxx", 2, 3, 1, 2, 1.0, 1.0,Color.white);
makeButton(f, "13", 3, 1, 1, 1, 1.0, 1.0,Color.red); makeButton(f, "F1", 3, 3, 1, 1, 0.0, 1.0,Color.red);*/



f.resize(500, 1000);
//f.setBackground(Color.blue);
f.pack();
f.show();
f.setLocation (10, 10);
// f.setSize(1000,1200);


}



static void makeButton (Icon icon ,Container cont, String name ,int x, int y, int w, int h, double weightx, double weighty, Color scolor )
{
GridBagLayout gbl = (GridBagLayout)cont.getLayout();
GridBagConstraints c = new GridBagConstraints();
Component comp;
// icon = Toolkit.getDefaultToolkit().getImage ( "C:\\bingo.gif" );
icon = new ImageIcon();
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = x;
c.gridy = y;
c.gridwidth = w;
c.gridheight = h;
c.weightx = weightx=1.0 ; c.weighty weighty1.0;

if (arg instanceof String) {
comp = new Button((String)arg);
comp.setBackground(scolor);

//comp.setBounds(50,50,50,50);


} else {

comp = (Component)arg;
}
cont.add(comp);

gbl.setConstraints(comp, c);







}

static public void main(String[] args) {
makeBoboLayout();

}
}

3 réponses

cs_GREENDAYZER Messages postés 5 Date d'inscription mardi 15 juillet 2003 Statut Membre Dernière intervention 18 août 2003
18 août 2003 à 14:43
-------------------------------
Réponse au message :
-------------------------------

> Comment faire pour inserer une image sur des boutons d'un GridBagLayout en utilisant makeButton?
> J'ai utilisé ça mais ça ne marche pas qlq connait la reponse Merci!
>
>
> public class Max
> {
> static void makeBoboLayout() {
> Frame f = new Frame("GridBagLayout Example 1");
> f.setFont(new Font("Helvetica", Font.PLAIN, 12));
> f.setForeground(Color.black);
> f.setBackground(Color.yellow);
> //resize(1600,1200);
> //f.setSize(f.getPreferredSize());
> f.setLayout(new GridBagLayout());
>
> makeButton (new ImageIcon("bingo.gif"),f, "xxxx", 0, 0, 32,1, 1.0, 1.0, Color.white );
> /*makeButton(f, "xxxx", 0, 1, 1, 2, 1.0, 1.0,Color.red); makeButton(f, "xxxxx", 0, 3, 1, 2, 1.0, 1.0,Color.red);
> makeButton(f, "11", 1, 1, 1, 1, 1.0, 1.0,Color.red); makeButton(f, "C1", 1, 3, 1, 1, 0.0, 1.0,Color.yellow);
> makeButton(f, "12", 1, 2, 1, 1, 1.0, 1.0,Color.red); makeButton(f, "D1", 1, 4, 1, 1, 0.0, 1.0,Color.black);
> makeButton(f, "WF Int", 2, 1, 1, 2, 1.0, 1.0,Color.red); makeButton(f, "Axxxx", 2, 3, 1, 2, 1.0, 1.0,Color.white);
> makeButton(f, "13", 3, 1, 1, 1, 1.0, 1.0,Color.red); makeButton(f, "F1", 3, 3, 1, 1, 0.0, 1.0,Color.red);*/
>
>
>
> f.resize(500, 1000);
> //f.setBackground(Color.blue);
> f.pack();
> f.show();
> f.setLocation (10, 10);
> // f.setSize(1000,1200);
>
>
> }
>
>
>
> static void makeButton (Icon icon ,Container cont, String name ,int x, int y, int w, int h, double weightx, double weighty, Color scolor )
> {
> GridBagLayout gbl = (GridBagLayout)cont.getLayout();
> GridBagConstraints c = new GridBagConstraints();
> Component comp;
> // icon = Toolkit.getDefaultToolkit().getImage ( "C:\\bingo.gif" );
> icon = new ImageIcon();
> c.fill = GridBagConstraints.HORIZONTAL;
> c.gridx = x;
> c.gridy = y;
> c.gridwidth = w;
> c.gridheight = h;
> c.weightx = weightx=1.0 ;> c.weighty weighty1.0;
>
> if (arg instanceof String) {
> comp = new Button((String)arg);
> comp.setBackground(scolor);
>
> //comp.setBounds(50,50,50,50);
>
>
> } else {
>
> comp = (Component)arg;
> }
> cont.add(comp);
>
> gbl.setConstraints(comp, c);
>
>
>
>
>
>
>
> }
>
> static public void main(String[] args) {
> makeBoboLayout();
>
> }
> }
0
cs_GREENDAYZER Messages postés 5 Date d'inscription mardi 15 juillet 2003 Statut Membre Dernière intervention 18 août 2003
18 août 2003 à 14:43
kkkk
0
benohite Messages postés 210 Date d'inscription vendredi 11 avril 2003 Statut Membre Dernière intervention 18 décembre 2003 7
18 août 2003 à 15:48
bonjour,

cela me parait normal car dans ta methode makeButton il n y a aucune ligne qui specifie que ton objet icon doit etre mis sur le boutton
de plus je ne sais pas si avec awt tu peux mettre des icones sur les boutton
pour te simplifier la vie, utilise plutot les boutton de swing:
soit en passant l icone aau constructeur soit via la methode setIcon();

benohite
:big)
0
Rejoignez-nous