-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathJavaSwingVentana.java
More file actions
72 lines (63 loc) · 3.4 KB
/
Copy pathJavaSwingVentana.java
File metadata and controls
72 lines (63 loc) · 3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package ventana;
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);
}
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
}
}