Placement fenêtre JAVA 2
salim_du_54
-
Thomas -
Thomas -
Comment faire pour qu'un JFrame se place par défaut au centre de l'écran lorsqu'on l'affiche?
4 réponses
-
Salut !
Le code suivant marche très bien...
public class Fenetre extends javax.swing.JFrame {
public Fenetre() {
// Initialisation des composants...
//...
// Centrage de la fenetre
java.awt.GraphicsEnvironment graphicsEnvironment =
java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment();
int x = graphicsEnvironment.getCenterPoint().x - getWidth() / 2;
int y = graphicsEnvironment.getCenterPoint().y - getHeight() / 2;
setLocation(x,y);
show();
} // Fin constructeur
} // Fin classe
A+
Si à gauche j'ai l'amont, c'est bon...
Si à gauche j'ai l'aval, j'suis bancal... -
Juste un truc : il me semble que ce n'est possible de faire ça qu'à partir de la version 1.4 de java... A priori, si tu utilises la 1.4.1 ou la 1.4.2, y a pas de pb...
A+
Si à gauche j'ai l'amont, c'est bon...
Si à gauche j'ai l'aval, j'suis bancal... -
celui ci marche avec 1.3
Dimension dim = getToolkit().getScreenSize();
setLocation((dim.th- getWidth()) / 2, ((dim.height- getHeight()) / 2); j ai peut etre confondu width et height ajuste ... -
plus simple depuis 1.4 :
// null permet de centrer à l'écran
leFrame.setLocationRelativeTo(null);