Beruflich Dokumente
Kultur Dokumente
BIENVENIDA CON
JFRAME
Este es un EJERCICIO que me mando un usuario, Aqui el
EJERCICIO RESUELTO:
Lo hize de dos Formas:
Uno con puro COD. FUENTE
Y otro con ayuda de la INTERFAZ de Netbeans 7.2
EJERCICIO
Necesito una ventana que tenga por objetivo dar un
saludo simple en la misma ventana, con un espacio para
poner NOMBRE X y 3 botones uno OK para aceptar el
nombre el otro para LIMPIAR el nombre colocado y el
ultimo boton para SALIR
/**
*
* E-Mail : shamirdhc31@gmail.com
* Blog
: http://javadhc.blogspot.com
*
*/
public class MensajeBienvenida extends JFrame implements ActionListener
{
//-- CREAMOS LOS OBJETOS QUE UTLIZAREMOS
public JButton btnBotonOk = new JButton("OK");
public JButton btnBotonLimpiar = new JButton("LIMPIAR");
public JButton btnBotonSalir = new JButton("SALIR");
public JLabel lblNombre = new JLabel("NOMBRE : ");
public JLabel lblBienvenido = new JLabel();
public JTextField txtNombre = new JTextField(20);
public MensajeBienvenida()
{
//-- CONFIGURAMOS LA VENTANA JFRAME
super("SALUDO");
this.setSize(400,200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.btnBotonOk.addActionListener(this);
this.btnBotonLimpiar.addActionListener(this);
this.btnBotonSalir.addActionListener(this);
//-- LE AADIMOS UN DISTRIBUIDOR DE OBJETOS A LA VENTANA
FlowLayout DISTRIBUIDOR = new FlowLayout(FlowLayout.CENTER,30,30);
this.setLayout(DISTRIBUIDOR);
//-- LE AGREGAMOS LOS OBJETOS A LA VENTANA
this.add(this.lblNombre);
this.add(this.txtNombre);
this.add(this.btnBotonOk);
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
73
74
75
76
77
78
79
80
81
82
83
this.add(this.btnBotonLimpiar);
this.add(this.btnBotonSalir);
this.add(this.btnBotonLimpiar);
this.add(this.lblBienvenido);
}
@Override
public void actionPerformed(ActionEvent AE)
{
//-- CON "getSource()" VEMOS EN QUE BOTON SE HIZO CLICK
if(AE.getSource() == this.btnBotonOk)
{
if(this.txtNombre.getText().equals(""))
{
JOptionPane.showMessageDialog(null,"INGRESE EL NOMBRE PRIMERO");
}
else
{
this.lblBienvenido.setText("BIENVENIDO : " + this.txtNombre.getTex
}
}
else if(AE.getSource() == this.btnBotonLimpiar)
{
this.txtNombre.setText("");
this.lblBienvenido.setText("");
}
else if(AE.getSource() == this.btnBotonSalir)
{
System.exit(0);
}
}
public static void main(String[] ARGUMENTOS)
{
//-- CREAMOS E INICIALIZAMOS LA VENTANA
MensajeBienvenida MB = new MensajeBienvenida();
MB.setVisible(true);
}
84
EJERCICIOS RESUELTOS
CON INTERFAZ EN
SWING JAVA PACK 02
EJERCICIOS PACK 02
Este PACK 02 Contiene ejercicios un poco mas avanzado
que el PACK 01, los ejercicios interactuan un poco con el
USUARIO, pero el PACK 03 interactuara por completo con
el usuario.
NOTA : PRIMERO les muestro los RESULTADOS por que
quiero que se vayan haciendo a la idea de lo que se hara
en el COD. FUENTE.
EJERCICIO 01
RESULTADO
COD. FUENTE
?
1
2
3
4
5
6
/**
*
* DUDAS SOBRE ESTE CODIGO, shamirdhc31@gmail.com
*/
import javax.swing.*;
public class EJERCICIO_01 extends JFrame
{
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
}
}
MI_INTERFAZ.setVisible(true);
EJERCICIO 02
RESULTADO
COD. FUENTE
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/**
*
* DUDAS SOBRE ESTE CODIGO, shamirdhc31@gmail.com
*/
import javax.swing.*;
import java.awt.*;
public class EJERCICIO_02 extends JFrame
{
public JButton BTN_MUSICA,BTN_VIDEO,BTN_INTERNET,BTN_JAVADHC,BTN_INFO;
public JToolBar CAJA_HERRAMIENTA_01,CAJA_HERRAMIENTA_02;
public ImageIcon IMG_MUSICA,IMG_VIDEO,IMG_INTERNET,IMG_JAVADHC,IMG_INFO;
public EJERCICIO_02()
{
super("MI MENU CON JFRAME...");
this.setSize(600,400);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.IMG_VIDEO = new ImageIcon("IMAGENES/VIDEO.PNG");
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
EJERCICIO 03
RESULTADO
COD. FUENTE
?
1 /**
2 ** DUDAS SOBRE ESTE CODIGO, shamirdhc31@gmail.com
3 */
4 import javax.swing.*;
5 import java.awt.*;
6 public class EJERCICIO_03 extends JFrame
{
7
public MI_PANEL PANEL_01, PANEL_02;
8
public JScrollPane DESLIZADOR;
9
1
public EJERCICIO_03()
{
0
super("CAJAS DE TEXTO CON DESLIZADOR... JAVADHC");
11
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
1
2
1
3
1
4
1
5
1
6
1
7
1
8
1
9
2
0
2
1
2
2
2
3
2
4
2
5
2
6
2
7
2
8
2
9
3
0
3
1
3
2
3
3
3
4
3
}
while(NOMBRE_CAJA_01.isEmpty() || NOMBRE_CAJA_02.isEmpty());
FlowLayout DISTRIBUIDOR = new FlowLayout();
this.setLayout(DISTRIBUIDOR);
PANEL_01 = new MI_PANEL(NOMBRE_CAJA_01);
PANEL_02 = new MI_PANEL(NOMBRE_CAJA_02);
this.add(PANEL_01);
this.add(PANEL_02);
}
public static void main(String[] ARGUMENTOS)
{
EJERCICIO_03 MI_INTERFAZ = new EJERCICIO_03();
MI_INTERFAZ.pack();
MI_INTERFAZ.setVisible(true);
}
}
class MI_PANEL extends JPanel
{
public JScrollPane DESLIZADOR;
public JTextArea COMENTARIOS;
public JTextField NOMBRE_CAJA;
public MI_PANEL(String NOMBRE)
{
super();
this.NOMBRE_CAJA = new JTextField();
this.NOMBRE_CAJA.setText("CAJA " + NOMBRE);
this.NOMBRE_CAJA.setEditable(false);
this.add(this.NOMBRE_CAJA);
this.COMENTARIOS = new JTextArea(10,20);
this.COMENTARIOS.setText("ESCRIBA AQUI SUS COMENTARIOS DE SU CAJA " + NOMBRE);
}
}
5
3
6
3
7
3
8
3
9
4
0
4
1
4
2
4
3
4
4
4
5
4
6
4
7
4
8
4
9
5
0
5
1
5
2
5
3
5
4
5
5
5
6
5
7
5
8
5
9
6
0
6
1
6
2
6
3
6
4
6
5
EJERCICIO 04
RESULTADO
COD. FUENTE
?
1
2
3
4
5
6
7
8
/**
*
* DUDAS SOBRE ESTE CODIGO, shamirdhc31@gmail.com
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class EJERCICIO_04 extends JFrame implements ActionListener
{
public JLabel LETRA_NUMERO_01,LETRA_NUMERO_02,LETRA_RESULTADO;
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
}
public static void main(String[] ARGUMENTOS)
{
EJERCICIO_04 MI_INTERFAZ = new EJERCICIO_04();
MI_INTERFAZ.setResizable(false);
MI_INTERFAZ.setVisible(true);
56
57
58
59
60
61
62
63
64
65
66
67
EJERCICIO 05
RESULTADO
COD. FUENTE
?
1
2
3
4
/**
*
* DUDAS SOBRE ESTE CODIGO, shamirdhc31@gmail.com
*/
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
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class EJERCICIO_05 extends JFrame implements ActionListener
{
JRadioButton SUMAR,RESTAR,MULTIPLICAR,DIVIDIR;
ButtonGroup OPERACIONES;
JButton EJECUTAR_OPERACION;
JLabel LETRA_NUMERO_01,LETRA_NUMERO_02,LETRA_RESULTADO;
JTextField NUMERO_01,NUMERO_02,RESULTADO;
public EJERCICIO_05()
{
super("SUMA CON 4 OPERACIONES");
this.setSize(210,210);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.LETRA_NUMERO_01 = new JLabel("NUMERO 01");
this.LETRA_NUMERO_02 = new JLabel("NUMERO 02");
this.LETRA_RESULTADO = new JLabel("RESULTADO");
this.NUMERO_01 = new JTextField(10);
this.NUMERO_02 = new JTextField(10);
this.RESULTADO = new JTextField(10);
this.EJECUTAR_OPERACION = new JButton("EJECUTAR OPERACION");
this.EJECUTAR_OPERACION.addActionListener(this);
this.SUMAR = new JRadioButton("SUMAR",true);
this.RESTAR = new JRadioButton("RESTAR",false);
this.MULTIPLICAR = new JRadioButton("MULTIPLICAR",false);
this.DIVIDIR = new JRadioButton("DIVIDIR",false);
this.OPERACIONES = new ButtonGroup();
this.OPERACIONES.add(this.SUMAR);
this.OPERACIONES.add(this.RESTAR);
this.OPERACIONES.add(this.MULTIPLICAR);
this.OPERACIONES.add(this.DIVIDIR);
FlowLayout DISTRIBUIDOR = new FlowLayout();
this.setLayout(DISTRIBUIDOR);
this.add(this.LETRA_NUMERO_01);
this.add(this.NUMERO_01);
this.add(this.LETRA_NUMERO_02);
this.add(this.NUMERO_02);
this.add(this.SUMAR);
this.add(this.RESTAR);
this.add(this.MULTIPLICAR);
this.add(this.DIVIDIR);
this.add(this.EJECUTAR_OPERACION);
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
this.add(this.LETRA_RESULTADO);
this.add(this.RESULTADO);
}
public void actionPerformed(ActionEvent EVENTO)
{
float AUX_NUMERO_01,AUX_NUMERO_02,AUX_RESULTADO;
try
{
AUX_RESULTADO = 0;
AUX_NUMERO_01 = Float.parseFloat(this.NUMERO_01.getText());
AUX_NUMERO_02 = Float.parseFloat(this.NUMERO_02.getText());
if(this.SUMAR.isSelected())
{
AUX_RESULTADO = AUX_NUMERO_01 + AUX_NUMERO_02;
}
else if(this.RESTAR.isSelected())
{
AUX_RESULTADO = AUX_NUMERO_01 - AUX_NUMERO_02;
}
else if(this.MULTIPLICAR.isSelected())
{
AUX_RESULTADO = AUX_NUMERO_01 * AUX_NUMERO_02;
}
else if(this.DIVIDIR.isSelected())
{
AUX_RESULTADO = AUX_NUMERO_01 / AUX_NUMERO_02;
}
this.RESULTADO.setText(String.valueOf(AUX_RESULTADO));
}
catch(Exception E)
{
this.RESULTADO.setText("ERROR AL EJECUTAR");
}
}
public static void main(String[] ARGUMENTOS)
{
EJERCICIO_05 MI_INTERFAZ = new EJERCICIO_05();
MI_INTERFAZ.setResizable(false);
MI_INTERFAZ.setVisible(true);
}
99
100
101
102
103
EJERCICIO 06
RESULTADO
COD. FUENTE
?
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
/**
*
* DUDAS SOBRE ESTE CODIGO, shamirdhc31@gmail.com
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class EJERCICIO_06 extends JFrame implements ActionListener
{
public JButton VERDE,AMARILLO,ROJO;
public JPanel COLOR_PANEL;
public EJERCICIO_06()
{
super("COLORES... JAVADHC");
this.setSize(400,400);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.VERDE = new JButton("VERDE");
this.VERDE.setBackground(Color.GREEN);
this.VERDE.addActionListener(this);
this.AMARILLO = new JButton("AMARILLO");
this.AMARILLO.setBackground(Color.YELLOW);
this.AMARILLO.addActionListener(this);
this.ROJO = new JButton("ROJO");
this.ROJO.setBackground(Color.RED);
this.ROJO.addActionListener(this);
this.COLOR_PANEL = new JPanel();
this.COLOR_PANEL.setPreferredSize(new Dimension(300,300));
this.COLOR_PANEL.setBackground(Color.BLACK);
FlowLayout DISTRIBUIDOR_FRAME = new FlowLayout();
this.setLayout(DISTRIBUIDOR_FRAME);
this.add(this.VERDE);
this.add(this.AMARILLO);
this.add(this.ROJO);
this.add(this.COLOR_PANEL);
}
public void actionPerformed(ActionEvent EVENTO)
{
Object BOTON_SELECCIONADO = EVENTO.getSource();
if(BOTON_SELECCIONADO == this.VERDE)
{
this.COLOR_PANEL.setBackground(Color.GREEN);
}
else if(BOTON_SELECCIONADO == this.AMARILLO)
{
this.COLOR_PANEL.setBackground(Color.YELLOW);
}
else if(BOTON_SELECCIONADO == this.ROJO)
{
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
this.COLOR_PANEL.setBackground(Color.RED);
package factoriales21;
/** * * @author Hugol */
public class factorial {
Ahora, dentro de la clase Main instanciaremos nuestro JFrame y el JOptionPane, para que muestre los
factoriales debidos. Se hace de esta manera. Note que el cdigo est debidamente documentado:
package factoriales21;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
/** * * @author Hugol */
public class Main {
public static void main(String[] args) {
//Creamos un nuevo JTextArea
JTextArea area = new JTextArea();
//Instanciamos un nuvo JFrame, pasndole como argumento, el ttulo
JFrame form = new JFrame("Este es un JFrame");
//Variable para almacenar la salida en pantalla
String salida = "Los factoriales de los numeros impares menores que 21 son:\n";
for (int i=1;i<=21;i++){
if(i%2 != 0){
factorial obj=new factorial(i);
salida = salida + String.valueOf(obj.devolverFactorial()) +", ";
}
}
//La salida en pantalla se la asignamos como texto al JTextArea
area.setText(salida);
//Llamamos un nuevo JOptionPane
JOptionPane.showMessageDialog(null, area, "Este es un JOptionPane", JOptionPane.INFORMATION_MESSAGE);
//Con esta sentencia declaramos que cuando se cierre el JFrame la aplicacin se cierra
tambinform.setDefaultCloseOperation(javax.swing.WindowConstants.
EXIT_ON_CLOSE);
//Aadimos el componente JTextArea al JFrame
form.add(area);
//Le asignamos como posicin del JFrame, el centro de la pantalla
form.setLocationRelativeTo(null);
//Con esta sentencia, le asignamos el tamao de lo que contiene al JFrame
form.pack();
//Colocamos visible al usuario el formulario
form.setVisible(true);
}
}
Y ahi tenemos la aplicacin. Se tiene que ver muy parecido a esto: