Java - Ocultar un JPanel y visualizar otro en JApplet

 
Vista:

Ocultar un JPanel y visualizar otro en JApplet

Publicado por eduardo (1 intervención) el 23/12/2011 02:13:59
Buenas.
Tengo el siguiente código:
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
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
 
public class Heladeria extends JApplet
{
	// Declaración de paneles
	private PanelLogin pnlLogin;
	private PanelPrincipal pnlPrincipal;
	private Container contenedor;
 
	private Dimension dimensionMaxima;
 
	// Cumple la función de constructor
	public void init()
	{
		// Configurando el applet
		dimensionMaxima = Toolkit.getDefaultToolkit().getScreenSize();
		this.setSize(dimensionMaxima);
		initComponents();
	}
 
	private void initComponents()
	{
		contenedor = getContentPane();
 
		pnlPrincipal = new PanelPrincipal();
		pnlLogin = new PanelLogin();
 
		contenedor.add(pnlPrincipal, BorderLayout.CENTER);
		contenedor.add(pnlLogin, BorderLayout.CENTER);
	}
}
 
class PanelLogin extends JPanel
{
	private ImageIcon iconoImagen;
	private JLabel lblUser;
	private JLabel lblPassword;
	private JTextField txtuser;
	private JPasswordField txtPassword;
	private JButton btnAceptar;
	private JLabel lblMensaje;
 
	private String userName = "user01";
	private String userPassword = "password";
	private int intentos = 3;
 
	public PanelLogin()
	{
		initComponents();
	}
 
	private void initComponents()
	{
		iconoImagen = new ImageIcon("fondoLogin.jpeg");
		lblUser = new JLabel("User");
		lblPassword = new JLabel("Password");
		txtuser = new JTextField();
		txtPassword = new JPasswordField();
		btnAceptar = new JButton("Aceptar");
		lblMensaje = new JLabel();
 
		// Configurando componentes
		this.setLayout(null);
		lblUser.setFont(new Font("Arial", Font.BOLD, 16));
		lblPassword.setFont(new Font("Arial", Font.BOLD, 16));
 
		btnAceptar.setMnemonic('A');
 
		lblMensaje.setText("<html>Ingrese usuario y password, luego presione<br>el botón Aceptar o pulse Enter<html>");
		lblMensaje.setHorizontalAlignment(SwingConstants.CENTER);
		lblMensaje.setFont(new Font("sans serif", Font.PLAIN, 12));
 
		// Ubicando componentes en el panel
		lblUser.setBounds(300, 100, 100, 25);
		txtuser.setBounds(400, 100, 150, 25);
		lblPassword.setBounds(300, 135, 100, 25);
		txtPassword.setBounds(400, 135, 150, 25);
		btnAceptar.setBounds(400, 170, 150, 25);
		lblMensaje.setBounds(300, 215, 300, 45);
 
		// Agregando componentes al panel
		this.add(lblUser);
		this.add(txtuser);
		this.add(lblPassword);
		this.add(txtPassword);
		this.add(btnAceptar);
		this.add(lblMensaje);
 
		// Manejando eventos
		ActionListener al = new ActionListener()
		{
			public void actionPerformed(ActionEvent evt)
			{
				btnActionPerformed(evt);
			}
		};
		btnAceptar.addActionListener(al);
		txtuser.addActionListener(al);
		txtPassword.addActionListener(al);
	}
 
	private void btnActionPerformed(ActionEvent evt)
	{
		String user = new String(txtuser.getText());
		String password = new String(txtPassword.getPassword());
 
		if (intentos >  0 && user.equals(userName) && password.equals(userPassword)) {
			this.setVisible(false);
		}
		else {
			lblMensaje.setText("El nombre de usuario o password son incorrectos");
			lblMensaje.setFont(new Font("arial", Font.PLAIN, 12));
			lblMensaje.setForeground(new Color(200, 0, 0));
			intentos--;
 
			if (intentos == 0) {
				txtuser.setText("");
				txtPassword.setText("");
				txtuser.setEnabled(false);
				txtPassword.setEnabled(false);
				btnAceptar.setEnabled(false);
				lblMensaje.setText("No ha podido acceder al aplicativo, inténtelo otra vez");
				JOptionPane.showMessageDialog(this, "Ha excedido el número de intentos, cierre el programa e inténtelo de nuevo");
			}
		}
	}
 
	public void paintComponent(Graphics g)
	{
		super.paintComponent(g);
		iconoImagen.paintIcon(this, g, 0, 0);
	}
 
	public Dimension getPreferredSize()
	{
		return new Dimension(iconoImagen.getIconWidth(), iconoImagen.getIconHeight());
	}
}
 
class PanelPrincipal extends JPanel
{
	private JTabbedPane panelConPestanas;
 
	private JPanel panelMantenimiento;
 
	private JPanel panelVentas;
 
	private JPanel panelConfigurarDesc;
	private JPanel panelReportes;
 
	public PanelPrincipal()
	{
		this.setLayout(new BorderLayout());
		initComponents();
	}
 
	private void initComponents()
	{
		panelConPestanas = new JTabbedPane();
 
		panelMantenimiento = crearPanelMantenimiento();
		panelConPestanas.addTab("Mantenimiento", new ImageIcon("mantenimiento.png"), panelMantenimiento);
 
		panelVentas = crearPanelVentas();
		panelConPestanas.addTab("Ventas", new ImageIcon("ventas.png"), panelVentas);
 
		panelConfigurarDesc = crearPanelConfigurarDesc();
		panelConPestanas.addTab("Configurar Descuentos", new ImageIcon("descuento.png"), panelConfigurarDesc);
 
		panelReportes = crearPanelReportes();
		panelConPestanas.addTab("Reportes", new ImageIcon("reportes.png"), panelReportes);
 
		this.add(panelConPestanas, BorderLayout.CENTER);
	}
 
	private JPanel crearPanelMantenimiento()
	{
		JPanel pnlMantenimiento = new JPanel();
 
		return pnlMantenimiento;
	}
 
	private JPanel crearPanelVentas()
	{
		JPanel pnlVentas = new JPanel();
 
		return pnlVentas;
	}
 
	private JPanel crearPanelConfigurarDesc()
	{
		JPanel pnlConfigurarDesc = new JPanel();
 
		return pnlConfigurarDesc;
	}
 
	private JPanel crearPanelReportes()
	{
		JPanel pnlReportes = new JPanel();
 
		return pnlReportes;
	}
}

Si panelPrincipal está solapado por panelLogin, imagino que una vez que se han igresado los datos correctos y pulsar el botón en el panel de logeo, se hace dicho panel invisible y por tanto debería mostrarse el panel posterior (panelPrincipal) pero no ocurre.
Favor podrían orientarme qué hago mal o sugerirme otro modo de hacerlo?

Un saludo y gracias desde ya.
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