1

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);
    }       
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
James L.
  • 131
  • 2
  • 3
  • 7

4 Answers4

6

I have checked on internet but I Haven't found anything.

  • JFrame is visible before JComponents (frame.add(content);) are added / created

  • move code line frame.setVisible(true); (better everything about JFrame) to the end of constuctor

mKorbel
  • 109,525
  • 20
  • 134
  • 319
5

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

Alexis C.
  • 91,686
  • 21
  • 171
  • 177
  • I cant change the location of the JLabel even by using setBounds(); – James L. May 03 '13 at 13:53
  • 2
    *"I cant change the location of the JLabel even by using setBounds()"* Don't use `setBounds()`! – Andrew Thompson May 03 '13 at 13:53
  • Well I used setLocation() and it did not work. I really need to place the JLabel at a specific location. – James L. May 03 '13 at 13:55
  • @AndrewThompson People just don't want to accept fact that you have to use layout managers. This is not .NET! – Branislav Lazic May 03 '13 at 13:56
  • *"I really need to place the JLabel at a specific location."* If you really knew the size components need to be and how to place them, you could include that logic in a custom layout manager. But trust me, you don't. You really just need to understand how to work with layout managers (+ layout padding, borders etc.) & how to nest them inside one another. If the problem is the layout, your next question should focus on that. Provide ASCII art of how the GUI should look. – Andrew Thompson May 03 '13 at 14:07
  • I figured out how to place it to a specific location, but thanks for the help! – James L. May 03 '13 at 14:21
2
  1. Use layouts. FlowLayout should be fine in this case. Do not call setBounds() and do not set layout as a null.

  2. Add label and button on JPanel

  3. Then add JPanel on JFrame

  4. Call pack() instead of setSize()

  5. Call setVisible(true) in the end.

Good luck!

Branislav Lazic
  • 14,388
  • 8
  • 60
  • 85
2

You are making setLayout null.

    JPanel loginpanel = new JPanel();
    loginpanel.setLayout(null);

Use this,

    JPanel loginpanel = new JPanel();
    loginpanel.setLayout(new BorderLayout());        

Run the UI on the EDT instead of running on the main thread. Read this post.

Example:

public static void main(String [] args) 
    {
        Runnable r  = new Runnable() {

            @Override
            public void run() {
                createWindow();
            }
        };

        EventQueue.invokeLater(r);
    }   
Community
  • 1
  • 1
Amarnath
  • 8,736
  • 10
  • 54
  • 81