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

public class JFrameGUI extends JFrame 
{
    JLabel item1;
    public JFrameGUI(int l, int b , String Title)
    {
        setTitle(Title);
        setLayout(new FlowLayout());
        setSize(l, b);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        item1 = new JLabel("This is a Sentence.");
        item1.setToolTipText("This is gonna show up on hover.");
        add(item1);
    }

    public static void main(String[] args)
    {
        JFrameGUI g = new JFrameGUI(1280,720,"The End Of The Line");
        JPanel p = new JPanel();
        p.setBackground(Color.BLUE);
        g.add(p);
    }
}

When I execute this , all i get is a tiny Blue square nest to the "This is a sentence" string . I've tried everything !

Jens Piegsa
  • 7,399
  • 5
  • 58
  • 106
  • This is unrelated, but in the future you may want to learn about [the Event Dispatching Thread](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html), and why it is bad to manipulate Swing components outside of it. – afsantos Mar 08 '14 at 12:57

1 Answers1

2

You need to set the layout of the frame to a layout that doesn't respect the preferred sizes of its children. FlowLayout does, and your JPanel has no preferred size without any components added to it, or specifying a preferred size.

A simple fix, set the layout of the frame to BorderLayout, or not set a layout at all, since JFrame already has a default BorderLayout. Note though that you probably want to add the JLabel to the JPanel and not the JFrame. Unless you do want to add it the JFrame and not the background JPanel, you need to specify a BorderLayout position for the one you don't want in the center.

You can see this answer to see which layout managers respect preferred sizes and which don't

See more at Layout out Components Withing a Container

Also, setVisible(true) shoul be the last thing you do after adding all components.

Community
  • 1
  • 1
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720