1

I'm following through a book called "The JFC Swing Tutorial (Second Edition)" and I'm pretty much at the start I have followed this code and it should be displaying the button and the label in the content pane, but All im getting is a blank screen. any ideas? Thanks.

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

public class m extends JFrame
{
    void UserFrame()
    {
        //JFrame.setDefaultLookAndFeelDecorated(true);
        JFrame frame = new JFrame("Hellow You");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel jp = new JPanel(new GridLayout(0,1));

        //makes label
        JLabel label = new JLabel("Sup ");
        //adds to the frames content pane a label
        frame.getContentPane().add(label);

        JButton button = new JButton("Hai");
        frame.getContentPane().add(button);


        jp.add(button);
        jp.add(label);

        jp.setBorder(BorderFactory.createEmptyBorder(30,30,10,30));

        //pack set the window to what it needs AKA to display all components 
        frame.pack();
        //frame.setSize(250, 250);
        //shows window
        frame.setVisible(true);     
    }



    public static void main(String[] args)
    {
        final m window = new m();
        SwingUtilities.invokeLater(new Runnable() 
        {
            public void run()
            {
                window.UserFrame();
            }
        });
    }

}
Uli Köhler
  • 13,012
  • 16
  • 70
  • 120
Definity
  • 691
  • 2
  • 11
  • 31
  • is your blank screen at least the right size? – Mar Feb 27 '14 at 17:46
  • Nope, Its minimum size it can be – Definity Feb 27 '14 at 17:47
  • 1
    I think you missed to make the first window visible, like m.setVisible(true); – Gábor Bakos Feb 27 '14 at 17:47
  • You also need to set the size before you set it visible. That would be why it is at a minimum size. – gobernador Feb 27 '14 at 17:48
  • @GáborBakos No, he wrote `frame.setVisible(true);` – Uli Köhler Feb 27 '14 at 17:52
  • @gobernador Not neccessarily, although I think this would be a good practice. With the changes from my answer below alone, the window is expanded sufficiently. – Uli Köhler Feb 27 '14 at 17:53
  • @gobernador no, the size is calculated in this call: [frame.pack()](http://docs.oracle.com/javase/7/docs/api/java/awt/Window.html#pack%28%29). Definitely not a good practice. See kleopatra's answer in this topic: [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/questions/7229226/should-i-avoid-the-use-of-setpreferredmaximumminimumsize-methods-in-java-swi) – dic19 Feb 27 '14 at 17:54
  • Please use Java's naming conventions for classes! `m` ISN'T a good identifier for your class. Use something more suitable (eg: `MyFrame`). Use camelCase and capitalize the first letter. – u3l Feb 27 '14 at 18:19

1 Answers1

5

Simply add

frame.add(jp);

just before

frame.pack();

What's happening here? You correctly add all your widgets to a JPane, but you basically threw that JPane away and didn't use it anywhere.

This will be sufficient just to get it to work properly.

If you want to do it correctly, you should also remove frame.getContentPane().add(label); and frame.getContentPane().add(button); (Thank you @dic19 for noting that!). These will not work the way you used it.

Uli Köhler
  • 13,012
  • 16
  • 70
  • 120
  • 1
    +1 Also remove `frame.getContentPane().add(label)` and `frame.getContentPane().add(button)`. – dic19 Feb 27 '14 at 17:52
  • 1
    @dic19 I fully agree, thank you for noting that, I added this to my answer. These statements are quite useless in the way the OP used them. – Uli Köhler Feb 27 '14 at 17:56
  • 1
    I've already upvoted the answer so I can't do it again. Thanks! – dic19 Feb 27 '14 at 17:57