Created
July 17, 2025 22:14
-
-
Save kurotori/1394a5fd20851a98b20a447d1fbedacc to your computer and use it in GitHub Desktop.
Java: (Swing) Ventana con un botón y método de centrado
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import interfaz.Inicio; | |
public class App { | |
public static void main(String[] args) throws Exception { | |
Inicio v1 = new Inicio(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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