Skip to content

Instantly share code, notes, and snippets.

@kurotori
Created July 17, 2025 22:14
Show Gist options
  • Save kurotori/1394a5fd20851a98b20a447d1fbedacc to your computer and use it in GitHub Desktop.
Save kurotori/1394a5fd20851a98b20a447d1fbedacc to your computer and use it in GitHub Desktop.
Java: (Swing) Ventana con un botón y método de centrado
import interfaz.Inicio;
public class App {
public static void main(String[] args) throws Exception {
Inicio v1 = new Inicio();
}
}
package interfaz;
import javax.swing.JButton;
import javax.swing.JFrame;
import java.awt.Dimension;
import java.awt.Toolkit;
public class Inicio extends JFrame{
Toolkit herramientas;
JButton boton1;
private void centrar(){
herramientas = Toolkit.getDefaultToolkit();
Dimension d = herramientas.getScreenSize();
double xVentana = (d.getWidth() / 2) - (getWidth() /2);
double yVentana = (d.getHeight() / 2) - (getHeight() /2);
setLocation( (int) xVentana , (int) yVentana);
}
public Inicio(){
configurar();
}
private void configurar(){
setLayout(null);
setSize(640, 480);
setTitle("Ventanita");
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
centrar();
boton1 = new JButton("Hacé algo");
boton1.setBounds(10,10,100,30);
getContentPane().add(boton1);
repaint();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment