Petit soucis de placement sur gridbaglayout

Résolu
morgain63 -  
KX Messages postés 16761 Date d'inscription   Statut Modérateur Dernière intervention   -
Bonjour (ou bonsoir) à tous,
comme l'indique, j'ai un petit soucis de placement avec gridbaglayout
le but est de faire une calculatrice (une toute simple pour commencer), comme je l'ai pas trouvé de tutos ici j'en ai cherché ailleurs mais même en codant comme il est recommandé je n'ai qu'une ligne.....
donc voici mon code:
public class FP_Calculatrice extends JFrame
{
	/**
	bc_ ,bouton chiffre
	bo_ ,bouton oppération
	bv_ ,bouton virgule
	be_ ,bouton égal
	*/
	private JButton bc_0=new JButton("0");
	private JButton bc_1=new JButton("1");
	private JButton bc_2=new JButton("2");
	private JButton bc_3=new JButton("3");
	private JButton bc_4=new JButton("4");
	private JButton bc_5=new JButton("5");
	private JButton bc_6=new JButton("6");
	private JButton bc_7=new JButton("7");
	private JButton bc_8=new JButton("8");
	private JButton bc_9=new JButton("9");
	private JButton be_egale=new JButton("=");
	private JButton bv_virgule=new JButton(".");
	private JButton bo_plus=new JButton("+");
	private JButton bo_moins=new JButton("-");
	private JButton bo_multiplier=new JButton("*");
	private JButton bo_diviser=new JButton("/");
	private JButton bo_pourCent=new JButton("%");
	private JButton bo_inverse=new JButton("1/x");
	private JTextField es_ecran=new JTextField();
	private JPanel sc_clavier=new JPanel();
	private JPanel sc_calculatrice=new JPanel();
	
	private void miseEnPlace()
	{
		this.sc_calculatrice.add(es_ecran,BorderLayout.NORTH);
		this.sc_clavier.setLayout(new GridBagLayout());
		GridBagConstraints cont= new GridBagConstraints();
		cont.fill = GridBagConstraints.HORIZONTAL;
		cont.weightx=1.0;
		cont.gridx=0;
		cont.gridy=0;
		this.sc_clavier.add(bc_7);
		cont.gridx=1;
		cont.gridy=0;
		this.sc_clavier.add(bc_8);
		cont.gridx=2;
		cont.gridy=0;
		this.sc_clavier.add(bc_9);
		cont.gridx=3;
		cont.gridy=0;
		cont.gridwidth=GridBagConstraints.RELATIVE;
		this.sc_clavier.add(bo_diviser);
		cont.gridx=4;
		cont.gridy=0;
		cont.gridwidth=GridBagConstraints.REMAINDER;
		this.sc_clavier.add(bo_pourCent);
		cont.gridx=0;
		cont.gridy=1;
		this.sc_clavier.add(bc_4);
		cont.gridx=1;
		cont.gridy=1;
		this.sc_clavier.add(bc_5);
		cont.gridx=2;
		cont.gridy=1;
		this.sc_clavier.add(bc_6);
		cont.gridx=3;
		cont.gridy=1;
		cont.gridwidth=GridBagConstraints.RELATIVE;
		this.sc_clavier.add(bo_multiplier);
		cont.gridx=4;
		cont.gridy=1;
		cont.gridwidth=GridBagConstraints.REMAINDER;
		this.sc_clavier.add(bo_inverse);
		cont.gridx=0;
		cont.gridy=2;
		this.sc_clavier.add(bc_1);
		cont.gridx=1;
		cont.gridy=2;
		this.sc_clavier.add(bc_2);
		cont.gridx=2;
		cont.gridy=2;
		this.sc_clavier.add(bc_3);
		cont.gridx=3;
		cont.gridy=2;
		cont.gridwidth=GridBagConstraints.RELATIVE;
		this.sc_clavier.add(bo_moins);
		cont.gridx=4;
		cont.gridy=2;
		cont.gridwidth=GridBagConstraints.REMAINDER;
		cont.gridheight=2;
		this.sc_clavier.add(be_egale);
		cont.gridx=0;
		cont.gridy=3;
		cont.gridheight=1;
		cont.gridwidth=2;
		this.sc_clavier.add(bc_0);
		cont.gridx=2;
		cont.gridy=3;
		cont.gridwidth=1;
		this.sc_clavier.add(bv_virgule);
		cont.gridx=3;
		cont.gridy=3;
		cont.gridwidth=GridBagConstraints.RELATIVE;
		this.sc_clavier.add(bo_plus);
		this.sc_calculatrice.add(sc_clavier,BorderLayout.CENTER);
		this.setContentPane(sc_calculatrice);
	}
	public FP_Calculatrice(String name)
	{
		super(name);
		miseEnPlace();
	}
}

petite précision: le "=" doit être sur deux lignes et le "0" sur deux colonnes

merci d'avance à tous
A voir également:

1 réponse

KX Messages postés 16761 Date d'inscription   Statut Modérateur Dernière intervention   3 020
 
Il te manque une étape avant de faire tes this.sc_clavier.add(b_truc)
Il faut que tu fasses gridbag.setConstraints(b_truc, cont);
En définissant GridBagLayout gridbag = new GridBagLayout(); et this.sc_clavier.setLayout(gridbag);

Remarque1 : il y a d'autres bugs mais tu devrais pouvoir corriger ça.
Remarque2 : GroupLayout est peut-être un peu plus compliqué à comprendre au départ, mais si t'arrives à voir comment ça marche je pense que le résultat sera mieux ;-)
1
morgain63
 
merci ça m'aide beaucoup!
0
KX Messages postés 16761 Date d'inscription   Statut Modérateur Dernière intervention   3 020
 
Je parlais de GroupLayout, voici ce que ça pourrait donner (avec encore quelques bugs ^^)

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class FP_Calculatrice extends JFrame
{
    private static final long serialVersionUID = 1;
        
    private final Map<String, JButton> touches;
    
    private Map<String, JButton> setBoutons(String...noms)
    {
        HashMap<String,JButton> map = new HashMap<String,JButton>();
        if (noms!=null)
            for (String s : noms)
                map.put(s, new JButton(s));
        return Collections.unmodifiableMap(map);
    }
    
    public FP_Calculatrice(String name)
    {
        super(name);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(300,200);
        
        touches = setBoutons("0","1","2","3","4","5","6","7","8","9","+","-","x","/","%","=","1/x",".");        
        miseEnPlace();
        
        setVisible(true);
    }
    
    private void miseEnPlace()
    {
        JPanel sc_clavier=new JPanel();
        GroupLayout layout = new GroupLayout(sc_clavier);
        sc_clavier.setLayout(layout);        
        
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);
        
        layout.setHorizontalGroup(
            layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup()
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup()
                            .addComponent(touches.get("7"))
                            .addComponent(touches.get("4"))
                            .addComponent(touches.get("1"))
                            )
                        .addGroup(layout.createParallelGroup()
                            .addComponent(touches.get("8"))
                            .addComponent(touches.get("5"))
                            .addComponent(touches.get("2"))
                            )
                        .addGroup(layout.createParallelGroup()
                            .addComponent(touches.get("9"))
                            .addComponent(touches.get("6"))
                            .addComponent(touches.get("3"))
                            )
                        )
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(touches.get("0"))
                        .addComponent(touches.get("."))
                        )
                    )
                .addGroup(layout.createParallelGroup()
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup()
                            .addComponent(touches.get("1/x"))
                            .addComponent(touches.get("x"))
                            .addComponent(touches.get("+"))
                            )
                        .addGroup(layout.createParallelGroup()
                            .addComponent(touches.get("%"))
                            .addComponent(touches.get("/"))
                            .addComponent(touches.get("-"))
                            )
                        )
                    .addComponent(touches.get("="))
                    )
                );
        
        layout.setVerticalGroup(
            layout.createParallelGroup()
                .addGroup(layout.createSequentialGroup()
                    .addGroup(layout.createParallelGroup()
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(touches.get("7"))
                            .addComponent(touches.get("4"))
                            .addComponent(touches.get("1"))
                            )
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(touches.get("8"))
                            .addComponent(touches.get("5"))
                            .addComponent(touches.get("2"))
                            )
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(touches.get("9"))
                            .addComponent(touches.get("6"))
                            .addComponent(touches.get("3"))
                            )
                        )
                    .addGroup(layout.createParallelGroup()
                        .addComponent(touches.get("0"))
                        .addComponent(touches.get("."))
                        )
                    )
                .addGroup(layout.createSequentialGroup()
                    .addGroup(layout.createParallelGroup()
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(touches.get("1/x"))
                            .addComponent(touches.get("x"))
                            .addComponent(touches.get("+"))
                            )
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(touches.get("%"))
                            .addComponent(touches.get("/"))
                            .addComponent(touches.get("-"))
                            )
                        )
                    .addComponent(touches.get("="))
                    )
                );
        
        add(sc_clavier);
    }
        
    public static void main(String...args)
    {
        new FP_Calculatrice("Calculatrice");
    }
}
0