Foro sobre Java SE > Centrar Ventana Swing
public enum UtilidadesVentana {
INSTANCE;
public static GraphicsConfiguration getDefaultGraphicsConfig() {
return getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
}
public static Rectangle getUsableScreenBounds(final GraphicsConfiguration gc) {
final Insets insets = getDefaultToolkit().getScreenInsets(gc);
final Rectangle bounds = gc.getBounds();
bounds.x += insets.left;
bounds.y += insets.top;
bounds.width -= insets.left + insets.right;
bounds.height -= insets.top + insets.bottom;
return bounds;
}
public static Rectangle getUsableScreenBounds() {
return getUsableScreenBounds(getDefaultGraphicsConfig());
}
public static Point getPointForCentering(final Window window) {
final Rectangle usableBounds = getUsableScreenBounds();
return new Point((usableBounds.width - window.getWidth()) / 2, (usableBounds.height - window.getHeight()) / 2);
}
}
public class NewJFrame extends javax.swing.JFrame {
private static final long serialVersionUID = 2_711_955_152_747_636_374L;
/** Creates new form NewJFrame */
public NewJFrame() {
initComponents();
}
/** This method is called from within the constructor to
initialize the form.
WARNING: Do NOT modify this code. The content of this method is
always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Ventana centrada");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
/**
@param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
NewJFrame newJFrame = new NewJFrame();
newJFrame.setLocation(UtilidadesVentana.getPointForCentering(newJFrame));
newJFrame.setVisible(true);
}
});
}
// Variables declaration - do not modify
// End of variables declaration
}
Hola, estoy intentando centrar una ventana creada en JFrame Form, pues a lo que inicio la aplicación aparece en el extremo izquierdo del monitor, alguien puede ayudarme, si es posible explicarme donde puedo configurar esto en la pestaña "Design" del JFrame, ?? espero su respuesta, gracias