Sie sind auf Seite 1von 24

VENTANA DE

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

RESULTADO 01 : CON PURO COD. FUENTE

RESULTADO 02 : CON LA INTERFAZ DE


NETBEANS 7.2

COD. FUENTE DEL RESULTADO 01


?
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

/**
*
* 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

COMO FUNCIONA EL MENU

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

public JMenu MENU_ARCHIVO,MENU_JAVA;


public JMenuItem ITEM_ABRIR,ITEM_GUARDAR,ITEM_CERRAR,ITEM_JAVADHC;
public JMenuBar MENU_BAR;
public EJERCICIO_01()
{
super("MI MENU CON JFRAME...");
this.setSize(600,400);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.ITEM_ABRIR = new JMenuItem("ABRIR");
this.ITEM_GUARDAR = new JMenuItem("GUARDAR");
this.ITEM_CERRAR = new JMenuItem("CERRAR");
this.ITEM_JAVADHC = new JMenuItem("JAVADHC");
this.MENU_ARCHIVO = new JMenu("ARCHIVO");
this.MENU_ARCHIVO.add(this.ITEM_ABRIR);
this.MENU_ARCHIVO.add(this.ITEM_GUARDAR);
this.MENU_ARCHIVO.addSeparator();
this.MENU_ARCHIVO.add(this.ITEM_CERRAR);
this.MENU_JAVA = new JMenu("JAVA");
this.MENU_JAVA.add(this.ITEM_JAVADHC);
this.MENU_BAR = new JMenuBar();
this.MENU_BAR.add(this.MENU_ARCHIVO);
this.MENU_BAR.add(this.MENU_JAVA);
this.setJMenuBar(this.MENU_BAR);
}
public static void main(String[] ARGS)
{
//-- HACEMOS QUE SE INICIALIZE NUESTRA VENTANA JFRAME
EJERCICIO_01 MI_INTERFAZ = new EJERCICIO_01();
//-- HACEMOS QUE NUESTRA VENTANA SE VISIBLE

}
}

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

this.IMG_MUSICA = new ImageIcon("IMAGENES/MUSICA.PNG");


this.IMG_INTERNET = new ImageIcon("IMAGENES/INTERNET.PNG");
this.IMG_JAVADHC = new ImageIcon("IMAGENES/JAVADHC.PNG");
this.IMG_INFO = new ImageIcon("IMAGENES/INFO.PNG");

this.BTN_VIDEO = new JButton("VIDEO",this.IMG_VIDEO);


this.BTN_MUSICA = new JButton("MUSICA",this.IMG_MUSICA);
this.BTN_INTERNET = new JButton("INTERNET",this.IMG_INTERNET);
this.BTN_JAVADHC = new JButton("JAVADHC",this.IMG_JAVADHC);
this.BTN_INFO = new JButton("http://javadhc.blogspot.com/",this.IMG_INFO);
this.CAJA_HERRAMIENTA_01 = new JToolBar();
this.CAJA_HERRAMIENTA_01.add(this.BTN_VIDEO);
this.CAJA_HERRAMIENTA_01.add(this.BTN_MUSICA);
this.CAJA_HERRAMIENTA_01.add(this.BTN_INTERNET);
this.CAJA_HERRAMIENTA_02 = new JToolBar();
this.CAJA_HERRAMIENTA_02.add(this.BTN_JAVADHC);
this.CAJA_HERRAMIENTA_02.add(this.BTN_INFO);
BorderLayout DISTRIBUIDOR = new BorderLayout();
this.setLayout(DISTRIBUIDOR);
this.add(this.CAJA_HERRAMIENTA_01,BorderLayout.NORTH);
this.add(this.CAJA_HERRAMIENTA_02,BorderLayout.SOUTH);
}
public static void main(String[] ARGUMENTOS)
{
EJERCICIO_02 MI_INTERFAZ = new EJERCICIO_02();
MI_INTERFAZ.setVisible(true);
}
}

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

String NOMBRE_CAJA_01, NOMBRE_CAJA_02;


do
{

JOptionPane.showMessageDialog(null,"SE VOLVERA A PREGUNTAR SI LOS NOMBRES


NOMBRE_CAJA_01 = JOptionPane.showInputDialog(null,"INDIQUE EL NOMBRE DE LA
NOMBRE_CAJA_02 = JOptionPane.showInputDialog(null,"INDIQUE EL NOMBRE DE LA

}
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);

}
}

this.DESLIZADOR = new JScrollPane(this.COMENTARIOS);


this.add(this.DESLIZADOR);

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 JTextField NUMERO_01,NUMERO_02,RESULTADO;


public JButton SUMAR;
public EJERCICIO_04()
{
super("SUMA DE NUMEROS... JAVADHC");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(210,160);
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.RESULTADO.setEditable(false);
this.SUMAR = new JButton("PULSE PARA SUMAR");
this.SUMAR.addActionListener(this);
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.LETRA_RESULTADO);
this.add(this.RESULTADO);
}
public void actionPerformed(ActionEvent EVENTO)
{
float AUX_NUMERO_01,AUX_NUMERO_02,AUX_RESULTADO;
try
{
AUX_NUMERO_01 = Float.parseFloat(this.NUMERO_01.getText());
AUX_NUMERO_02 = Float.parseFloat(this.NUMERO_02.getText());
AUX_RESULTADO = AUX_NUMERO_01 + AUX_NUMERO_02;
this.RESULTADO.setText(String.valueOf(AUX_RESULTADO));
}
catch(Exception E)
{
this.RESULTADO.setText("ERROR AL SUMAR");
}

}
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);

public static void main(String[] ARGUMENTOS)


{
EJERCICIO_06 MI_INTERFAZ = new EJERCICIO_06();
MI_INTERFAZ.setResizable(false);
MI_INTERFAZ.setVisible(true);
}

Ejemplos creando JFrame y


JOptionPane
30 marzo 2009

Es muy til que nos vayamos introduciendo al mundo del


ambiente grfico en Java. Para ello hoy en da se utiliza el popular paquete dejavax.swing. Las libreras
swing nos aportan una serie de componentes y contenedores para realizar aplicaciones de escritorio.
Como ya en varios ejercicios se ha estado utilizando la famosa clase esttica JOptionPane, ahora veremos
las diferencias y cmo instanciar un nuevo formulario, que en el Swing es llamado JFrame. As podremos ver
las diferencias y podemos aprender a crear nuestros primeros formularios en Java.
Lo haremos utilizando el ejercicio que se us en un parcial. Que se calcularan los factoriales de los nmeros
impares menores que 2 y se mostraran al usuario. Se har de dos formas: con un JOptionPane y con un
JFrame.
Antes que nada creamos la clase factorial que nos devolver el factorial del nmero que se le ingrese:

package factoriales21;
/** * * @author Hugol */
public class factorial {

private int numero;


private double resultado;
//Constructor
public factorial(int num){
this.numero=num;
this.resultado=1;
}
//Devuelve el factorial
public double devolverFactorial(){

for (int i=1; i<=this.numero;i++){


this.resultado*=i;
}
return this.resultado;
}
}

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:

Das könnte Ihnen auch gefallen