Search code examples
javaswingwindowjbuttonjlabel

JLabel doesn't show up


I'm working on a program but my JLabel doesn't show up. My JButton works perfectly (it appears) but for some reason the JLabel does not appear. I have checked on internet but I Haven't found anything.

package com.hinx.client;

import java.awt.Color;    
import javax.swing.*;

public class Main {

    public static void main(String [] args) 
    {
        createWindow();
    }       

    static void createWindow()
    {           

        //Create panel
        JPanel content = new JPanel();
        content.setLayout(null);

        //Build the frame
        JFrame frame = new JFrame("Hinx - A marketplace for apps - Client ALPHA_0.0.1");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(700, 400);
        frame.setResizable(false);
        frame.setLocationRelativeTo(null);
        frame.add(content);
        frame.setVisible(true);

        //Add the login button
        JButton login = new JButton("Login");
        login.setBounds(0, 342, 150, 30);

        //Create login label
        JLabel loginlabel = new JLabel("Login Area");

        //Create login panel
        JPanel loginpanel = new JPanel();
        loginpanel.setLayout(null);
        loginpanel.setBounds(0, 0, 150, 400);
        loginpanel.setBackground(Color.gray);
        loginpanel.add(login);
        loginpanel.add(loginlabel);         

        content.add(loginpanel);
    }       
}

Solution

  • Set a layout for your panel. Per example :

    loginpanel.setLayout(new BorderLayout());
    

    You can learn more about layouts here.

    Here's what I get : enter image description here