Skip to content

Instantly share code, notes, and snippets.

@alvareztech
Created February 15, 2014 18:01
Show Gist options
  • Save alvareztech/9022849 to your computer and use it in GitHub Desktop.
Save alvareztech/9022849 to your computer and use it in GitHub Desktop.
Java: Ejemplo de la creación de una Ventana con Swing.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
/**
* Clase Ventana
* Muestra la estructuta que deberia tener una Ventana en Java con la libreria
* Swing, contiene una etiqueta, un caja de texto y un boton, que tiene la
* accion de mostrar el texto en la caja por una ventana de mensaje.
* @author Daniel Alvarez (a3dany)
*/
public class Ventana extends JFrame implements ActionListener {
private JLabel texto; // etiqueta o texto no editable
private JTextField caja; // caja de texto, para insertar datos
private JButton boton; // boton con una determinada accion
public Ventana() {
super(); // usamos el contructor de la clase padre JFrame
configurarVentana(); // configuramos la ventana
inicializarComponentes(); // inicializamos los atributos o componentes
}
private void configurarVentana() {
this.setTitle("Esta Es Una Ventana"); // colocamos titulo a la ventana
this.setSize(310, 210); // colocamos tamanio a la ventana (ancho, alto)
this.setLocationRelativeTo(null); // centramos la ventana en la pantalla
this.setLayout(null); // no usamos ningun layout, solo asi podremos dar posiciones a los componentes
this.setResizable(false); // hacemos que la ventana no sea redimiensionable
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // hacemos que cuando se cierre la ventana termina todo proceso
}
private void inicializarComponentes() {
// creamos los componentes
texto = new JLabel();
caja = new JTextField();
boton = new JButton();
// configuramos los componentes
texto.setText("Inserte Nombre"); // colocamos un texto a la etiqueta
texto.setBounds(50, 50, 100, 25); // colocamos posicion y tamanio al texto (x, y, ancho, alto)
caja.setBounds(150, 50, 100, 25); // colocamos posicion y tamanio a la caja (x, y, ancho, alto)
boton.setText("Mostrar Mensaje"); // colocamos un texto al boton
boton.setBounds(50, 100, 200, 30); // colocamos posicion y tamanio al boton (x, y, ancho, alto)
boton.addActionListener(this); // hacemos que el boton tenga una accion y esa accion estara en esta clase
// adicionamos los componentes a la ventana
this.add(texto);
this.add(caja);
this.add(boton);
}
@Override
public void actionPerformed(ActionEvent e) {
String nombre = caja.getText(); // obtenemos el contenido de la caja de texto
JOptionPane.showMessageDialog(this, "Hola " + nombre + "."); // mostramos un mensaje (frame, mensaje)
}
public static void main(String[] args) {
Ventana V = new Ventana(); // creamos una ventana
V.setVisible(true); // hacemos visible la ventana creada
}
}
@JSiapo
Copy link

JSiapo commented May 12, 2018

Gracias está geniai

@DavidCampos15012017
Copy link

Gracias !

Como sería si quiero agregar más botones con más funciones ?

@rvsweb
Copy link

rvsweb commented Aug 27, 2018

Gracias por compartir tus conocimientos.
Muy bien explicado

@leonardo0martinez
Copy link

Me iluminaste loco... ¡Gracias!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment