I'm using Swing and I am trying to add the same components to multiple panels. However, only the panel I add to the frame last has these buttons. I am adding the same set of labels/textfields to p2, p3, and p4. But since I added the labels/textfields to p4 last, it seems to show only p4 as having it. How can I re-use these components for multiple JPanels so I don't have to make a ton of them for each different panel?
The code I am using:
public class TriangleGUI extends JFrame implements ActionListener
{
static JPanel p1, p2, p3,p4,p5;
static JButton b1, b2, b3;
static JLabel label1, label2, label3;
static JTextField tf1, tf2, tf3;
private static int side1, side2, side3, angle1, angle2, angle3;
public TriangleMadnessGUI(){
setSize(800,400);
p1 = new JPanel();
p2 = new JPanel();
p3 = new JPanel();
p4 = new JPanel();
b1 = new JButton("1 Side, 2 Angles"); //buttons
b2 = new JButton("1 Angle, 2 Sides");
b3 = new JButton("3 Sides");
label1 = new JLabel("Angle 1"); //labels which i intend to reuse
label2 = new JLabel("Angle 2");
label3 = new JLabel("Side 1");
tf1 = new JTextField("",5); //textfields which i intend to reuse
tf2 = new JTextField("",5);
tf3 = new JTextField("",5);
p1.add(b1);
p1.add(b2);
p1.add(b3);
p2.add(label1); //panel 2, add set of components
p2.add(tf1);
p2.add(label2);
p2.add(tf2);
p2.add(label3);
p2.add(tf3);
p3.add(label1); //panel 3, adding set of same components
p3.add(tf1);
p3.add(label2);
p3.add(tf2);
p3.add(label3);
p3.add(tf3);
p4.add(label1); //panel 4, adding set of same components
p4.add(tf1);
p4.add(label2);
p4.add(tf2);
p4.add(label3);
p4.add(tf3);
add(p1, BorderLayout.NORTH);
add(p4, BorderLayout.CENTER); // <-----HERE, only p4 has visible buttons
// p2 and p3 are just blank if used in here.
setVisible(true);
}
}