Java - Abrir una ventana desde ventana principal

 
Vista:
sin imagen de perfil

Abrir una ventana desde ventana principal

Publicado por obrian1180 (4 intervenciones) el 05/03/2017 20:42:19
Buenas tardes, hace poco me decidí aprender a programar en Java y me ha ido bien hasta que me topé con un problema que no me deja avanzar más... o bueno si pero quiero seguir desde allí.

Estoy programando una Gui en Eclipse de forma manual, o sea escribir todo el código de botones, label, etc.

El asunto es el siguiente, me creé una ventana principal con cuatro botones, todo ok.. a continuación quise que cuando diera click en el primer botón me creara otra ventana con otras Label y otros Botones. El me crea la Ventana con las propiedades que quiero sin embargo "aquí el problema" no se ve la Label que quiero mostrar y ningún otro componente que le desee agregar... he intentando de varias formas hasta donde tengo conocimiento y he buscando un poco por google pero no doy con el asuntillo... hasta creo que creaba ciclos infinitos porque se me colgaba la aplicación, a continuación el código.
Ah, otra cosa, son dos Clases, en la clase principal que llamo Inicio.java tengo los botones principales y en la segunda clase llamada Operaciones.java es donde quiero hacer todas las operaciones correspondientes a los botones principales...

CLASE 1
Inicio.java
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
package BANCA;
import javax.swing.*;
import java.awt.event.*;
public class Inicio extends JFrame implements ActionListener{
	private JLabel titulo_Menu, descripcion;
	private JButton cliente_N, deposito, retiro, consulta;
	public Inicio(){
		super("INICIO");
		setLayout(null);
		//TITULO Y DESCRIPCION
		titulo_Menu = new JLabel(".::BANCA::.");
		titulo_Menu.setBounds(180, 20, 300, 15);
		add(titulo_Menu);
		descripcion = new JLabel("Menú de Opciones");
		descripcion.setBounds(180, 40, 300, 15);
		add(descripcion);
		//BOTONES
		cliente_N = new JButton("Cliente Nuevo");
		cliente_N.setBounds(90, 90, 120, 30);
		add(cliente_N);
		cliente_N.addActionListener(this);
		deposito = new JButton("Deposito");
		deposito.setBounds(260, 90, 120, 30);
		add(deposito);
		deposito.addActionListener(this);
		retiro = new JButton("Retiro");
		retiro.setBounds(90, 170, 120, 30);
		add(retiro);
		retiro.addActionListener(this);
		consulta = new JButton("Consultar");
		consulta.setBounds(260, 170, 120, 30);
		add(consulta);
		consulta.addActionListener(this);
		//FIN INTERFAZ MENU PRINCIPAL
	}
	//CAPTURADOR DE EVENTO DE LOS BOTONES
	public void actionPerformed(ActionEvent e){
		if (e.getSource() == cliente_N){
			Operaciones operacion = new Operaciones();
			operacion.crearCliente();
		}
		if (e.getSource() == deposito){}
		if (e.getSource() == retiro){}
		if (e.getSource() == consulta){}
	}
	public static void main(String[] ar){
		Inicio inicio = new Inicio();
		inicio.setBounds(10, 20, 500, 400);
		inicio.setVisible(true);
	}
}

CLASE 2
Operaciones.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package BANCA;
import javax.swing.*;
import java.awt.event.*;
public class Operaciones extends JFrame{
	private JLabel titulo_ventana;
	private JButton aceptar, cancelar;
 
	public void ventana(){
		Operaciones operacion = new Operaciones();
		operacion.setBounds(10,20,700,500);
		operacion.setVisible(true);
	}
	public void crearCliente(){
		ventana();
		titulo_ventana = new JLabel("CLIENTE NUEVO");
		titulo_ventana.setBounds(10,20,100,200);
		add(titulo_ventana);
	}
 
}

Gracias de ante mano...
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
sin imagen de perfil

Abrir una ventana desde ventana principal

Publicado por Alejandro (6 intervenciones) el 06/03/2017 08:23:46
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
73
package BANCA;
 
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
 
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
 
public class Inicio extends JFrame implements ActionListener {
	private enum Actions {
		CLIENTE_NUEVO, DEPOSITO, RETIRO, CONSULTA
	}
 
	public Inicio() {
		super("INICIO");
		setLayout(null);
		// TITULO Y DESCRIPCION
		JLabel titulo_Menu = new JLabel(".::BANCA::.");
		titulo_Menu.setBounds(180, 20, 300, 15);
		add(titulo_Menu);
		JLabel descripcion = new JLabel("Menú de Opciones");
		descripcion.setBounds(180, 40, 300, 15);
		add(descripcion);
		// BOTONES
		JButton cliente_N = new JButton("Cliente Nuevo");
		cliente_N.setBounds(90, 90, 120, 30);
		cliente_N.setActionCommand(Actions.CLIENTE_NUEVO.name());
		add(cliente_N);
		cliente_N.addActionListener(this);
 
		JButton deposito = new JButton("Deposito");
		deposito.setBounds(260, 90, 120, 30);
		deposito.setActionCommand(Actions.DEPOSITO.name());
		add(deposito);
		deposito.addActionListener(this);
 
		JButton retiro = new JButton("Retiro");
		retiro.setBounds(90, 170, 120, 30);
		retiro.setActionCommand(Actions.RETIRO.name());
		add(retiro);
		retiro.addActionListener(this);
 
		JButton consulta = new JButton("Consultar");
		consulta.setBounds(260, 170, 120, 30);
		consulta.setActionCommand(Actions.CONSULTA.name());
		add(consulta);
		consulta.addActionListener(this);
		// FIN INTERFAZ MENU PRINCIPAL
	}
 
	// CAPTURADOR DE EVENTO DE LOS BOTONES
	@Override
	public void actionPerformed(ActionEvent e) {
 
		if (e.getActionCommand() == Actions.CLIENTE_NUEVO.name()) {
			Operaciones operacion = new Operaciones();
		}
		if (e.getSource() == Actions.DEPOSITO.name()) {
		}
		if (e.getSource() == Actions.RETIRO.name()) {
		}
		if (e.getSource() == Actions.CONSULTA.name()) {
		}
	}
 
	public static void main(String[] ar) {
		Inicio inicio = new Inicio();
		inicio.setBounds(10, 20, 500, 400);
		inicio.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		inicio.setVisible(true);
	}
}


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
package BANCA;
 
import java.awt.HeadlessException;
 
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
 
public class Operaciones extends JFrame {
	private JLabel titulo_ventana;
	private JButton aceptar, cancelar;
 
	public Operaciones() throws HeadlessException {
		super();
		setBounds(10, 20, 700, 500);
		crearCliente();
		setVisible(true);
	}
 
	public void crearCliente() {
		titulo_ventana = new JLabel("CLIENTE NUEVO");
		titulo_ventana.setBounds(10, 20, 100, 200);
		add(titulo_ventana);
	}
 
}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
1
Comentar
sin imagen de perfil

Abrir una ventana desde ventana principal

Publicado por OBRIAN (4 intervenciones) el 07/03/2017 01:22:09
Muuchisimas gracias Alejandro, lo probé y funcionó a la perfección. Aunque lo que hiciste desconozco algunas cosas jeje pero iré aprendiendo. Yo había probando de este modo y también me sirvió pero antes tuve que hacerlo otra vez paso a paso para ver donde estaba el fallo o que me faltaba. Gracias de nuevo.

CLASE 1
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
package BANCA;
import javax.swing.*;
import java.awt.event.*;
public class Inicio extends JFrame implements ActionListener{
	private JLabel titulo_Menu, descripcion;
	private JButton cliente_N, deposito, retiro, consulta;
	public Inicio(){
		super("INICIO");
		setLayout(null);
		//TITULO Y DESCRIPCION
		titulo_Menu = new JLabel(".::BANCA::.");
		titulo_Menu.setBounds(180, 20, 300, 15);
		add(titulo_Menu);
		descripcion = new JLabel("Menú de Opciones");
		descripcion.setBounds(180, 40, 300, 15);
		add(descripcion);
		//BOTONES
		cliente_N = new JButton("Cliente Nuevo");
		cliente_N.setBounds(90, 90, 120, 30);
		add(cliente_N);
		cliente_N.addActionListener(this);
		deposito = new JButton("Deposito");
		deposito.setBounds(260, 90, 120, 30);
		add(deposito);
		deposito.addActionListener(this);
		retiro = new JButton("Retiro");
		retiro.setBounds(90, 170, 120, 30);
		add(retiro);
		retiro.addActionListener(this);
		consulta = new JButton("Consultar");
		consulta.setBounds(260, 170, 120, 30);
		add(consulta);
		consulta.addActionListener(this);
		//FIN INTERFAZ MENU PRINCIPAL
	}
	//CAPTURADOR DE EVENTO DE LOS BOTONES
	public void actionPerformed(ActionEvent e){
		if (e.getSource() == cliente_N){
			CuentaNueva operacion = new CuentaNueva();
			operacion.crearCliente();
		}
		if (e.getSource() == deposito){}
		if (e.getSource() == retiro){}
		if (e.getSource() == consulta){}
	}
	public static void main(String[] ar){
		Inicio inicio = new Inicio();
		inicio.setBounds(10, 20, 500, 400);
		inicio.setVisible(true);
	}
}


CLASE 2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package BANCA;
import javax.swing.*;
import java.awt.event.*;
public class Operaciones extends JFrame{
	public void crearCliente(){
		setLayout(null);
		//DESCRIPCION VENTANA
		titulo_ventana = new JLabel("CLIENTE NUEVO");
		titulo_ventana.setBounds(150,10,100,50);
		add(titulo_ventana);
		titulo_ventana.setVisible(true);
 
		//VENTANA
		setSize(500,400);
		setTitle("CLIENTE NUEVO");
		setVisible(true);
 
 
	}
	private JLabel titulo_ventana;
}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
sin imagen de perfil

Abrir una ventana desde ventana principal

Publicado por Alejandro (6 intervenciones) el 07/03/2017 07:45:19
Lo siento por mi español. (Traduzco Google-traductor)

En el primer ejemplo de código:
1) Es necesario llamar setVisible(true), sólo después de la adición de todos los elementos.
En la clase Operaciones se llama setVisible(true), antes de añadir el JLabel.
Por lo que es invisible.

2) es necesario agregar "inicio.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);".
de lo contrario, después de cerrar la ventana, el proceso permanece en la memoria.

3)
"Operaciones operacion = new Operaciones();
operacion.crearCliente();"
Esta degradación del rendimiento.
debido a que la clase Operaciones hereda de JFrame, después de llamar a la "new Operaciones ()", llamado JFrame() (Constructor).

en este código, el constructor de JFrame se llama dos veces:

en class Inicio, fila 39
y después en class Operaciones fila 9

en el segundo ejemplo de código:
fila "titulo_ventana.setVisible(true);" no es necesario
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
1
Comentar
sin imagen de perfil

Abrir una ventana desde ventana principal

Publicado por OBRIAN (4 intervenciones) el 08/03/2017 02:13:58
I understand, you speak English.
I am also using the translator for this answer.
Thank you first for taking the time to help me using a translator.
With respect to your comment, it helped me a lot, I noticed that there are small details that make the difference in the code.

1) I did not know how important (EXIT_ON_CLOSET) I understand because there were so many javaw.exe in the task manager.

2) I could see that he added the
1
2
3
private enum Actions{
		CUENTA_NUEVA(I changed the class), DEPOSITO, RETIRO, CONSULTA
	}

I did not fully understand what it is for, I know it affects
1
2
3
4
Public void actionPerformed (ActionEvent e) {
If (e.getActionCommand () == Actions.CUENTA_NUEVA.name ()) {
AccountNew operation = newNew Account ();
}
However, I do not quite have it clear, I will be looking to know more.

3) I could fix the constructor by raising the panel, also erase a SetVisible (true)

4) At
1
import java.awt.HeadlessException;
I researched a little and what I could understand is that it is like a generic exeption.
: P

5) It was clear to me that:
If you do
1
JLabel example new = JLabel ();
It is not necessary to declare the
1
Private JLabel example;
.

Thanks again for everything. If you have time and if you like, leave me an email address to show you how I carry the rest of the code.
Goodnight...
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
sin imagen de perfil

Abrir una ventana desde ventana principal

Publicado por Alejandro (6 intervenciones) el 08/03/2017 14:03:29
1.
1
2
3
private enum Actions{
		CUENTA_NUEVA , DEPOSITO, RETIRO, CONSULTA
	}

This is to get rid of the storage of JButton objects in the instance of the class.
Like this:
1
private JButton cliente_N, deposito, retiro, consulta;

Then I assign to each button an actionCommand. In fact, this is string constant
1
... consulta.setActionCommand(Actions.CONSULTA.name());...

and then in actionPerformed method (Sorry. I made a mistake there. Should be.):
1
... if (e.getActionCommand() == Actions.CONSULTA.name()) ....

Or it could be done so:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
....
private static final String CONSULTA = "CONSULTA";
....
....Inicio(){
       consulta.setActionCommand(CONSULTA);
.....
}
.....
....actionPerformed(ActionEvent e){
.....
        switch(e.getActionCommand()){
              case CONSULTA:
                  ........
                  break;
                  ....
        }
...
}
....


2.
1
public Operaciones() throws HeadlessException
This is because I called parent constructor
1
2
3
4
5
6
7
8
9
10
11
public class Operaciones extends JFrame {
	......
 
	public Operaciones() throws HeadlessException {
		super(); // <------ parent constructor call (JFrame )
                //  wich throws HeadlessException 
               // it's signature :
               //  public JFrame()  throws HeadlessException
 
		.....
	}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
sin imagen de perfil

Abrir una ventana desde ventana principal

Publicado por Alejandro (6 intervenciones) el 08/03/2017 14:58:05
correction:
1
2
3
4
5
public Operaciones() throws HeadlessException {
		//super(); 
// super()   -  it's not obligatory. Parent constructor will be called in any case.
.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
sin imagen de perfil

Abrir una ventana desde ventana principal

Publicado por OBRIAN (4 intervenciones) el 10/03/2017 01:18:23
It is understood...
I'd better stick with this mode for now.

1
... if (e.getActionCommand () == Actions.CONSULTA.name ())

1
Super();
I have seen that it serves to give title to the window that invokes it. In that case, I usually use
1
SetTitle();
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar