Centrer des boutons dans une fenêtre

Bonjour,

Je souhaite centrer 2 boutons dans une fenêtre et je n’y arrive pas. Je me gratte la tête, je suis sûr que c’est super hypra c*n mais ça ne vient pas :slight_smile:
J’explique :
J’ai une fenêtre JFrame ayant pour dimensions 950x800.
Elle s’affiche correctement.
Ensuite, je fais un this.setContentPane(monPanel);

et voilà comment est contruit « monPanel » :


mainPanel = new JPanel();
		
		JButton buttonFeuillesDeTemps = new JButton("Feuilles de temps");
		//JButton buttonFeuillesDeTemps = new JButton(createImageIcon("images/timeclock2.gif"));
		buttonFeuillesDeTemps.addActionListener(this.actionListener);
		buttonFeuillesDeTemps.setName(Constants.BUTTON_MENU_FEUILLES_DE_TEMPS);
		
		JButton buttonConfiguration = new JButton("Configuration");
		//JButton buttonConfiguration = new JButton(createImageIcon("images/config2.gif"));
		buttonConfiguration.addActionListener(this.actionListener);
		buttonConfiguration.setName(Constants.BUTTON_MENU_CONFIGURATION);
		
		buttonFeuillesDeTemps.setPreferredSize(new Dimension(50,50));
		buttonConfiguration.setPreferredSize(new Dimension(50,50));
		
		JButton buttonQuitter = new JButton("Quitter");
		buttonQuitter.addActionListener(this.actionListener);
		buttonQuitter.setName(Constants.BUTTON_MENU_QUITTER);
		
		JPanel containerPanel = new JPanel();
		containerPanel.add(buttonFeuillesDeTemps);
		containerPanel.add(buttonConfiguration);
		containerPanel.setBackground(Color.CYAN);
		containerPanel.setMinimumSize(new Dimension(150,150));
		containerPanel.setPreferredSize(new Dimension(150,150));
		containerPanel.setMaximumSize(new Dimension(150,150));
		containerPanel.setSize(new Dimension(150,150));
		
		mainPanel.setLayout(new BorderLayout());
		Component f = Box.createGlue();
		f.setPreferredSize(new Dimension(50,50));
		f.setSize(new Dimension(50,50));
		mainPanel.add(f, BorderLayout.LINE_START);		
		mainPanel.add(containerPanel, BorderLayout.CENTER);
		//mainPanel.add(Box.createVerticalGlue(),BorderLayout.PAGE_END);
		Component f2 = Box.createHorizontalGlue();
		mainPanel.add(f2, BorderLayout.LINE_END);
		mainPanel.setBackground(Color.YELLOW);
		mainPanel.setPreferredSize(new Dimension(200,200));
		mainPanel.setMaximumSize(new Dimension(200,200));
		mainPanel.setSize(new Dimension(200,200));
		mainPanel.add(Box.createVerticalGlue(),BorderLayout.PAGE_START);
		mainPanel.add(new JButton("hhhh"), BorderLayout.PAGE_END);

Là, vous voyez un peu tous mes essais pour que ça marche mais sans succès. En fait, le panel « containerPanel » prend tout l’espace disponible. Ce que je souhaite est de pouvoir ajouter 2 boutons à un panel et que ce panel s’affiche de manière centrée (horizontalement et verticalement) dans la fenêtre. Au lieu de ça, les boutons sont affichés en haut du panel « containerPanel » qui, prenant toute la place, se retrouve également en haut de la JFrame et donc non centrée.

Quelqu’un a une idée ?

Merci, Termos

Le BorderLayout, c’est pas celui qui utilise 5 zones (Sud, Est, Nord, Ouest, Centre) ? Tu es sûr que tu n’ajoute pas les boutons l’un sur l’autre?

tout a fait d’acord a ve Sans-Nom
tu nu’utilise pas les bonnes constantes pour le borderlayout
java.sun.com…

Euh… Je ne vois où est l’erreur (How to Use BorderLayout)

Suite à vos posts, j’ai essayé avec les autres constantes (NORTH, EAST, WEST…) et ça donne la même chose. J’ai les deux jeux de constantes à disposition.

Ce je je fais, c’est ajouter 2 boutons dans un JPanel simple (donc le layout est FlowLayout apr défaut)
Ensuite, j’essaye d’ajouter ce panel dans le panel principal a qui je donne un layout BorderLayout.
Edité le 12/10/2007 à 13:03

un exemple
p_fond est un panel avec comme layout un borderLayout, p_bouton un flowlayout
b1 et b2 sont 2 boutons ajouts dans p_bouton qui est lui meme ajouté dans p_fond en position centrale
p_fond est ensuite désigné comme contentpane principale de la jframe.

class test extend JFrame
{
//…
JPanel p_fond = new JPanel(new BorderLayout());
JPanel p_bouton = new JPanel(new FlowLayout(FlowLayout.CENTER));

JButton b1,b2;
b1 = new JButton(« B1 »);
b2 = new JButton(« B2 »);
p_bouton.add(b1);
p_bouton.ad(b2);

p_fonf.add(p_bouton,BorderLayout.CENTER);

setcontentPane(p_fond);

}

J’ai fait tout « qu’est-ce qu’on m’a dit » :wink: mais ça marche pô :
http://img299.imageshack.us/my.php?image=screenvd0.jpg

Voilà le code :

package gestime.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;

public class MenuPanelContentManager implements PanelContentManager {

	private JPanel container = null;
	private JPanel mainPanel = null;
	private ActionListener actionListener = null;
	
	public MenuPanelContentManager(ActionListener actionListener) {
		this.container = null;
		this.actionListener = actionListener;
	}

	public JPanel getContent() {
		if (this.mainPanel==null) {
			initMenuPanel();
		}
		
		return this.mainPanel;
	}
	
	private void initMenuPanel() {
		mainPanel = new JPanel(new BorderLayout());
		
		JButton buttonFeuillesDeTemps = new JButton("Feuilles de temps");
		//JButton buttonFeuillesDeTemps = new JButton(createImageIcon("images/timeclock2.gif"));
		buttonFeuillesDeTemps.addActionListener(this.actionListener);
		buttonFeuillesDeTemps.setName(Constants.BUTTON_MENU_FEUILLES_DE_TEMPS);
		
		JButton buttonConfiguration = new JButton("Configuration");
		//JButton buttonConfiguration = new JButton(createImageIcon("images/config2.gif"));
		buttonConfiguration.addActionListener(this.actionListener);
		buttonConfiguration.setName(Constants.BUTTON_MENU_CONFIGURATION);
		
		JButton buttonQuitter = new JButton("Quitter");
		buttonQuitter.addActionListener(this.actionListener);
		buttonQuitter.setName(Constants.BUTTON_MENU_QUITTER);
		
		JPanel containerPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
		containerPanel.add(buttonFeuillesDeTemps);
		containerPanel.add(buttonConfiguration);
		containerPanel.setBackground(Color.CYAN);
		
		mainPanel.add(containerPanel, BorderLayout.CENTER);
	}

	private void _initMenuPanel() {
		mainPanel = new JPanel();
		
		JButton buttonFeuillesDeTemps = new JButton("Feuilles de temps");
		//JButton buttonFeuillesDeTemps = new JButton(createImageIcon("images/timeclock2.gif"));
		buttonFeuillesDeTemps.addActionListener(this.actionListener);
		buttonFeuillesDeTemps.setName(Constants.BUTTON_MENU_FEUILLES_DE_TEMPS);
		
		JButton buttonConfiguration = new JButton("Configuration");
		//JButton buttonConfiguration = new JButton(createImageIcon("images/config2.gif"));
		buttonConfiguration.addActionListener(this.actionListener);
		buttonConfiguration.setName(Constants.BUTTON_MENU_CONFIGURATION);
		
		buttonFeuillesDeTemps.setPreferredSize(new Dimension(50,50));
		buttonConfiguration.setPreferredSize(new Dimension(50,50));
		
		JButton buttonQuitter = new JButton("Quitter");
		buttonQuitter.addActionListener(this.actionListener);
		buttonQuitter.setName(Constants.BUTTON_MENU_QUITTER);
		
		JPanel containerPanel = new JPanel();
		containerPanel.add(buttonFeuillesDeTemps);
		containerPanel.add(buttonConfiguration);
		containerPanel.setBackground(Color.CYAN);
		containerPanel.setMinimumSize(new Dimension(150,150));
		containerPanel.setPreferredSize(new Dimension(150,150));
		containerPanel.setMaximumSize(new Dimension(150,150));
		containerPanel.setSize(new Dimension(150,150));
		containerPanel.setAlignmentY(Component.CENTER_ALIGNMENT);
		
		mainPanel.setLayout(new BorderLayout());
		Component f = Box.createGlue();
		f.setPreferredSize(new Dimension(50,50));
		f.setSize(new Dimension(50,50));
		mainPanel.add(f, BorderLayout.WEST);		
		mainPanel.add(containerPanel, BorderLayout.CENTER);
		//mainPanel.add(Box.createVerticalGlue(),BorderLayout.PAGE_END);
		Component f2 = Box.createHorizontalGlue();
		mainPanel.add(f2, BorderLayout.EAST);
		mainPanel.setBackground(Color.YELLOW);
		mainPanel.setPreferredSize(new Dimension(200,200));
		mainPanel.setMaximumSize(new Dimension(200,200));
		mainPanel.setSize(new Dimension(200,200));
		mainPanel.add(Box.createVerticalGlue(),BorderLayout.NORTH);
		mainPanel.add(new JButton("hhhh"), BorderLayout.SOUTH);
		mainPanel.setAlignmentY(Component.CENTER_ALIGNMENT);
	}
	
	private ImageIcon createImageIcon(String path) {
		java.net.URL imgURL = MenuPanelContentManager.class.getResource(path);
	    return new ImageIcon(imgURL);
	}

}

Les properties de la JFrame :

JFrame.setDefaultLookAndFeelDecorated(true);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setSize(950, 800);
		this.setPreferredSize(this.getSize());
		this.setLocationRelativeTo(null);	
		UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
		//UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		
		Font mainFont = new Font("Tahoma",Font.PLAIN, 9);
		Font mainFont2 = new Font("Tahoma",Font.PLAIN, 9);
		
		UIManager.put("Label.font", mainFont);
		UIManager.put("FormattedTextField.font", mainFont);
		UIManager.put("Button.font", mainFont);
		UIManager.put("Button.textIconGap", new Integer(0));
		UIManager.put("TextField.font", mainFont);
		UIManager.put("TextPane.font", mainFont);
		UIManager.put("ComboBox.font", mainFont2);
		
		//getContentPane().setLayout(new GridBagLayout());
		
		centerFrame();	

Edité le 12/10/2007 à 16:23