Java - No veo por qué no cambia entre paneles JPanel

 
Vista:

No veo por qué no cambia entre paneles JPanel

Publicado por MecMec (1 intervención) el 06/04/2018 12:30:45
A ver si alguien me saca de dudas ;)

Gracias de antemano.

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
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
 
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
 
public class Farolas extends JFrame implements ActionListener, ChangeListener {
 
	private JPanel jpP, jp1, jp2, jp3;
	private JCheckBox[][][] jcb=new JCheckBox[5][2][7];
	private JLabel jlP1, jlP2, jlP3;
	private JLabel[] calles=new JLabel[5];
	private JComboBox jcbP3;
	private JTextField jtfP1;
	private JTextArea jtaP3;
	private JButton jbPPb1, jbPPb2, jbPPb3, jbP1;
	private JSeparator[] separador=new JSeparator[4];
 
	public Farolas() {
		setLayout(new BorderLayout());
 
	/* PANEL PRINCIPAL */
		jpP=new JPanel();
		jpP.setLayout(new GridLayout(3, 1));
 
		jbPPb1=new JButton("Farolas por Calle");
		jbPPb1.addActionListener(this);
		jpP.add(jbPPb1);
		jbPPb2=new JButton("Control de Farolas");
		jbPPb2.addActionListener(this);
		jpP.add(jbPPb2);
		jbPPb3=new JButton("Información");
		jbPPb3.addActionListener(this);
		jpP.add(jbPPb3);
 
		add(jpP, BorderLayout.WEST);
 
	/* PANEL 1 */
		jp1=new JPanel();
		jp1.setLayout(null);
 
		jlP1=new JLabel("Cantidad de farolas encendidas por calle:");
		jlP1.setBounds(20, 40, 300, 20);
		jp1.add(jlP1);
 
		jtfP1=new JTextField();
		jtfP1.setBounds(340, 40, 50, 50);
		jp1.add(jtfP1);
 
		jbP1=new JButton("Confirmar");
		jbP1.setBounds(40, 100, 100, 50);
		jbP1.addActionListener(this);
		jp1.add(jbP1);
 
	//	jp1.setVisible(false);
	//	add(jp1);
 
	/* PANEL 2 */
		jp2=new JPanel();
		jp2.setLayout(null);
 
		jlP2=new JLabel("Selecciona las farolas que quieras encender:");
		jlP2.setBounds(20, 20, 300, 20);
		jp2.add(jlP2);
 
		int y=90, x=0;
		int count=0;
 
		for(int c=0;c<5;c++) {
			x+=30;
			for(int a=0;a<2;a++) {
				for(int f=0;f<7;f++) {
					jcb[c][a][f]=new JCheckBox();
					jcb[c][a][f].setBounds(x, y, 20, 20);
					jp2.add(jcb[c][a][f]);
					y+=30;
				}
				x+=30;
				y=90;
			}
			calles[c]=new JLabel(""+(c+1));
			calles[c].setBounds(x-38, y-25, 15, 15);
			jp2.add(calles[c]);
			if(count<4) {
				separador[count]=new JSeparator();
				separador[count].setOrientation(SwingConstants.VERTICAL);
				separador[count].setBounds(x+15, y, 2, 200);
				jp2.add(separador[count]);
				count++;
			}
		}
 
	//	jp2.setVisible(false);
	//	add(jp2);
 
	/* PANEL 3 */
		jp3=new JPanel();
		jp3.setLayout(null);
 
		jlP3=new JLabel("Calle:");
		jlP3.setBounds(20, 20, 50, 20);
		jp3.add(jlP3);
 
		jcbP3=new JComboBox<>();
		jcbP3.setBounds(80, 20, 70, 20);
		for(int i=0;i<5;i++) {
			jcbP3.addItem(i+1);
		}
		jp3.add(jcbP3);
 
		jtaP3=new JTextArea();
		jtaP3.setBounds(10, 55, 460, 300);
		jp3.add(jtaP3);
 
	//	jp3.setVisible(false);
	//	add(jp3);
	}
 
	public void actionPerformed(ActionEvent a) {
		if(a.getSource()==jbPPb1) {
	//		jp1.setVisible(true);
	//		jp2.setVisible(false);
	//		jp3.setVisible(false);
			add(jp1);
		}
		if(a.getSource()==jbPPb2) {
	//		jp1.setVisible(false);
	//		jp2.setVisible(true);
	//		jp3.setVisible(false);
			add(jp2);
		}
		if(a.getSource()==jbPPb3) {
	//		jp1.setVisible(false);
	//		jp2.setVisible(false);
	//		jp3.setVisible(true);
			add(jp3);
		}
	}
	public void stateChanged(ChangeEvent c) {}
 
	public static void main(String[] args) {
		Farolas obj=new Farolas();
		obj.setVisible(true);
		obj.setResizable(false);
		obj.setBounds(10, 10, 620, 400);
		obj.setDefaultCloseOperation(EXIT_ON_CLOSE);
	}
}
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
Imágen de perfil de kingk
Val: 247
Ha mantenido su posición en Java (en relación al último mes)
Gráfica de Java

No veo por qué no cambia entre paneles JPanel

Publicado por kingk (108 intervenciones) el 08/04/2018 01:06:25
Hola, puedes modificar el metodo actionPerformed de esta forma:

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
public void actionPerformed(ActionEvent a) {
	if(a.getSource()==jbPPb1) {
		//	jp1.setVisible(true);
		//	jp2.setVisible(false);
		//	jp3.setVisible(false);
		remove(jp2);
		remove(jp3);
		add(jp1,BorderLayout.CENTER);
	}
	if(a.getSource()==jbPPb2) {
		//	    jp1.setVisible(false);
		//		jp2.setVisible(true);
		//		jp3.setVisible(false);
		remove(jp1);
		remove(jp3);
		add(jp2,BorderLayout.CENTER);
	}
	if(a.getSource()==jbPPb3) {
		//		jp1.setVisible(false);
		//		jp2.setVisible(false);
		//		jp3.setVisible(true);
		remove(jp1);
		remove(jp2);
		add(jp3,BorderLayout.CENTER);
	}
	repaint();
	revalidate();
}
Espero haberte ayudado.
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