Home > OS >  JLabel added to JPanel not displayed
JLabel added to JPanel not displayed

Time:10-28

i have been trying to display a JLabel added to a JPanel with no sucess for some time now.
Main program :

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

public class Main{
    public static void main(String[] args){
        int screenWidth = 5000;
        //int screenWidth = (int)Toolkit.getDefaultToolkit().getScreenSize().getWidth();
        MyFrame frame = new MyFrame(screenWidth);

        JLabel header = new JLabel("Choisissez un nombre");
        header.setBounds(100,100,screenWidth-100,40);
        header.setFont(new Font("Arial",Font.BOLD,40));
        
        JPanel panel1 = new JPanel();
        panel1.setBounds(100,140,screenWidth-100,100);
        
        JLabel desc = new JLabel("entrez un nombre entre 1 et 100 : ");
        desc.setFont(new Font("Arial",Font.BOLD,40));
        
        panel1.add(desc);
        frame.add(header);      
        frame.add(panel1);

        frame.setVisible(true);
    }
}

MyFrame class :

import javax.swing.JFrame;
public class MyFrame extends JFrame{
    MyFrame(int screenWidth){
        this.setSize(screenWidth/5,screenWidth/10);
        this.setTitle("Le juste nombre");
        this.setLayout(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

CodePudding user response:

So your problem basically comes down to a misunderstanding over how layout management works in Swing.

To start with, take a look at Laying Out Components Within a Container and How to Use BorderLayout in particular, as this is the default layout manager user by JFrame

A simple fix would be to remove setLayout(null) and add your components as follows...

frame.add(header, BorderLayout.NORTH);      
frame.add(panel1);

But, your next question is going to be about why the size and position properties are ignored and that leads you straight back to the first link.

Layout managers are very powerful, but they can take some time to get use to. You need to let go of the concept of "pixel perfect" layouts, as they tend to break really fast, as just about every environment your application runs on will have some level differences which will just haunt you.

CodePudding user response:

remove the line

this.setLayout(null);

But the header is displayed below the panel.

My suggestion is to add a BorderLayout so you have control over where the components are displayed in the frame:

this.layout(new java.awt.BorderLayout());

// when you add components to the frame, specify where you want them:
frame.add(header,BorderLayout.NORTH);      
frame.add(panel1,BorderLayout.CENTER);

For testing you can set a backgroundcolor to the panel to understand more how it is displayed:

panel1.setBackground(Color.red);

CodePudding user response:

problem wasn't related to my use of this.setLayout(null), it was because of panel1.setBounds(100,140,screenWidth-100,100); the panel was overflowing outside the frame and because desc was being rendered in the center of panel1 it was being rendered outside the frame.
the solution :
replace : panel1.setBounds(100,140,screenWidth-100,100);
by : panel1.setBounds(100,140,frame.getWidth()-100,100);

  • Related