Rhevi
Rhevi

Reputation: 25

How do I fix my JLabel not appearing in the intended JPanel? (Code utilizes JLayeredPane)

I'm currently doing a GUI project as you can see below. While designing the UI I encountered a problem with my JLabel. It wouldn't appear as soon as I run the program.

public class Main extends JFrame {

private JFrame mainFrame;
private JPanel parentPanel;
private JPanel sidePanel;

private JLayeredPane layer;

private JPanel info, tri, sqr, rect, circ;
private JLabel infoTF, triLabel, sqrTF, circTF;

public static void main(String[] args) {
    Main show = new Main();
}

public Main() {
    mainFrame = new JFrame("Geometric Shapes Computation");


    layer = new JLayeredPane();
    layer.setLayout(null);
    layer.setBounds(0,0,700,600);

    Color standard = new Color (76 ,41, 211);
    Color fg = new Color (204, 204, 204);
    Font items = new Font("Century Gothic", Font.BOLD, 12);


    triLabel = new JLabel("Triangle");
    triLabel.setFont(items);
    triLabel.setBackground(standard);
    triLabel.setForeground(fg);
    triLabel.setVisible(true);

    tri = new JPanel();
    tri.add(triLabel);
    tri.setBackground(Color.ORANGE);
    tri.setLayout(null);
    tri.setBounds(0, 215, 200, 70);

    sqr = new JPanel();
    sqr.setBackground(standard);
    sqr.setLayout(null);
    sqr.setBounds(0, 315, 200, 70);

    parentPanel = new JPanel();
    parentPanel.setLayout(null);
    parentPanel.setSize(700, 600);


    sidePanel = new JPanel();
    sidePanel.setLayout(null);
    sidePanel.setBounds(0,0,200, 600);
    sidePanel.setBackground(standard);

    sidePanel.add(tri);

    layer.add(parentPanel, JLayeredPane.DEFAULT_LAYER);
    layer.add(sidePanel, JLayeredPane.PALETTE_LAYER);
    layer.add(tri, JLayeredPane.MODAL_LAYER);

    mainFrame.add(layer);
    mainFrame.setSize(700, 600);
    mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    mainFrame.setResizable(false);
    mainFrame.setVisible(true);

}
}

Upvotes: 1

Views: 37

Answers (1)

Powersjd
Powersjd

Reputation: 60

If I am understanding your question correctly you are just having issues displaying the label, I was able to show the label by commenting out the tri.setLayout(null);

tri = new JPanel();
tri.add(triLabel);
tri.setBackground(Color.ORANGE);
//tri.setLayout(null);
tri.setBounds(0, 215, 200, 70);

This was able to display the text for me. Hopefully, this helps.

Upvotes: 1

Related Questions