Java - JPanel en JFrame error

 
Vista:

JPanel en JFrame error

Publicado por Santiago (1 intervención) el 08/04/2020 16:39:17
Bueno,mi problema es el siguiente.Tengo una clase con el JFrame y le puse una clase interna JPanel.Esta puesto asi porque voy a necesitar paneles y no quiero que ninguna otra clase acceda a la del jpanel.mi problema es el siguiente,yo añado los componentes al panel y no me los muestra.Para que me los muestre tengo que añadirlos al frame y despues al panel,queria ver si me podian corregir algo del codigo que este mal


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
package Colegio;
 
	import java.awt.Color;
	import java.awt.Font;
	import java.awt.event.ActionEvent;
	import java.awt.event.ActionListener;
 
	import javax.swing.JButton;
	import javax.swing.JComboBox;
	import javax.swing.JFrame;
	import javax.swing.JLabel;
	import javax.swing.JPanel;
	import javax.swing.JTextField;
	public class PantallaA extends JFrame implements ActionListener {
	private JComboBox combobox;
	private JLabel label1,label2,label3,label4,label5,label6,label7;
	private JTextField text1,text2,text3,text4,text5,text6;
	private JButton boton1,boton2,boton3;
 
		public PantallaA(){
			setLayout(null);
			this.getContentPane().setBackground(Color.yellow);
			setTitle("Calculador de Promedios");
			setBounds(0,0,600,600);
			setLocationRelativeTo(null);
			setResizable(false);
			setDefaultCloseOperation(PantallaA.EXIT_ON_CLOSE);
	        setVisible(true);
 
 
 
			Panel panel=new Panel();
			panel.setBackground(new Color(3,187,133));
			panel.setBounds(10,10,570, 100);
		    this.getContentPane().add(panel);
 
		    Panel panel2=new Panel();
		    panel2.setBackground(new Color(3,187,133));
		    panel2.setBounds(10,120 , 440, 440);
                    //añado panel al frame
		    this.getContentPane().add(panel2);
 
		    Panel panel3=new Panel();
			panel3.setBackground(new Color(3,187,133));
			panel3.setBounds(460, 120, 120, 440);
			this.getContentPane().add(panel3);
 
			boton1=new JButton("Calcular");
			boton1.setBackground(Color.orange);
			boton1.setBounds(475,200,90,25);
		    boton1.addActionListener(this);
//añado boton al panel
			panel3.add(boton1);
//añado panel con boton al jframe
			this.getContentPane().add(panel3.add(boton1));
 
			boton2=new JButton("Limpiar");
			boton2.setBackground(Color.orange);
		    boton2.setBounds(475,240,90,25);
			panel3.add(boton2);
			this.getContentPane().add(panel3.add(boton2));
			 boton2.addActionListener(this);
 
 
 
 
 
 
 
 
		}
 
		public void actionPerformed(ActionEvent evento) {
			if(evento.getSource()==boton1) {
				if(text1.equals("")) {
 
				}
				else {
					boton1.setEnabled(true);
				}
 
			}
 
		}
//clase interna
		private class Panel extends JPanel {
			Panel(){
 
				this.setVisible(true);
 
			}
 
		}
 
}
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
Val: 129
Ha disminuido 1 puesto en Java (en relación al último mes)
Gráfica de Java

JPanel en JFrame error

Publicado por Juan Manuel (53 intervenciones) el 08/04/2020 20:09:54
amigo nose si esto sea tu problema, pero entendi que no muestra la clase interna panel, para eso debes instanciarla en el constructor de tu clase PantallaA, te dejo el codigo a ver si es lo que buscas, en la clase interna cree un botonnuevo para que veas como lo agregue
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
Val: 129
Ha disminuido 1 puesto en Java (en relación al último mes)
Gráfica de Java

JPanel en JFrame error

Publicado por Juan Manuel (53 intervenciones) el 08/04/2020 20:10: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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package Colegio;
 
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
 
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
 
public class PantallaA extends JFrame implements ActionListener {
	private JComboBox combobox;
	private JLabel label1, label2, label3, label4, label5, label6, label7;
	private JTextField text1, text2, text3, text4, text5, text6;
	private JButton boton1, boton2, boton3;
 
	public PantallaA() {
		setLayout(null);
		this.getContentPane().setBackground(Color.yellow);
		setTitle("Calculador de Promedios");
		setBounds(0, 0, 600, 600);
		setLocationRelativeTo(null);
		setResizable(false);
		setDefaultCloseOperation(PantallaA.EXIT_ON_CLOSE);
		setVisible(true);
 
		Panel panel = new Panel();
		panel.setBackground(new Color(3, 187, 133));
		panel.setBounds(10, 10, 570, 100);
		this.getContentPane().add(panel);
 
		Panel panel2 = new Panel();
		panel2.setBackground(new Color(3, 187, 133));
		panel2.setBounds(10, 120, 440, 440);
		// añado panel al frame
		this.getContentPane().add(panel2);
 
		Panel panel3 = new Panel();
		panel3.setBackground(new Color(3, 187, 133));
		panel3.setBounds(460, 120, 120, 440);
		this.getContentPane().add(panel3);
 
		boton1 = new JButton("Calcular");
		boton1.setBackground(Color.orange);
		boton1.setBounds(475, 200, 90, 25);
		boton1.addActionListener(this);
//añado boton al panel
		panel3.add(boton1);
//añado panel con boton al jframe
		this.getContentPane().add(panel3.add(boton1));
 
		boton2 = new JButton("Limpiar");
		boton2.setBackground(Color.orange);
		boton2.setBounds(475, 240, 90, 25);
		panel3.add(boton2);
		this.getContentPane().add(panel3.add(boton2));
		boton2.addActionListener(this);
 
		Panel panelclaseinterna = new Panel();//instanciamos el panel.
 
	}
 
	public void actionPerformed(ActionEvent evento) {
		if (evento.getSource() == boton1) {
			if (text1.equals("")) {
 
			} else {
				boton1.setEnabled(true);
			}
 
		}
 
	}
 
//clase interna
	private class Panel extends JPanel {
		Panel() {
			JButton botonnuevo = new JButton("BOTON NUEVO");//creas boton
			setLayout(null);//layout libre
			botonnuevo.setBounds(10, 10, 150, 50);//posicision del boton
			add(botonnuevo);//agregamos a la lamnia
			this.setVisible(true);
 
		}
 
	}
 
}
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