galdranorn
galdranorn

Reputation: 83

Very basic JFrame not showing up

I know that this question was asked many times and I've tried solutions mentioned there for last hour, but none of them worked for me. I have very basic frame and it just doesn't show up. No errors, after compilation just "Build succesful".

The whole code is:

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

public class Frame extends JFrame
{    
    public Frame()
    {
        super("Hello");
        this.setBounds(100, 500, 100, 100);
        this.setDefaultCloseOperation(3);
    }

    public static void main(String[] args) 
    {
        new Frame().setVisible(true);
    }
}

Thanks in advance for your help.

Upvotes: 0

Views: 1472

Answers (3)

camickr
camickr

Reputation: 324088

Start by reading the Swing tutorial on How to Make Frames.

Download and test the FrameDemo example. It show you the proper way to create Swing components. One key is that Swing components should be created on modified on the Event Dispatch Thread (EDT).

If this code doesn't work then you probably have something wrong with your JDK. Try reinstalling.

Upvotes: 1

S B
S B

Reputation: 394

I tried your program and the frame shows up fine for me. Couple of steps which you can try -

  1. After running the program, you should check in task manager whether a new Java process is visible and if so, expand it and you should see "Hello", try bringing it to front (right click on process -> Bring to front).
  2. From a code point of view, try adding this.pack(); after this.setDefaultCloseOperation and re-run the program.

Upvotes: 1

Apophis
Apophis

Reputation: 273

I think your Frame instance in main is from package java.awt.* which you have imported. Check it out.

Upvotes: 1

Related Questions