ISilchev
ISilchev

Reputation: 13

JFrame does not appear

I'm using the Intelij idea platform.

I have the following code:

package GUI.test;

import javax.swing.*;

public class Frame extends JFrame{

         Frame(){}

public void main (String[] args){

    new Frame();
}

}

I expected to see a JFrame after compiling this code, but was nothing appeared. What kind of problem can it be?

Upvotes: 1

Views: 3070

Answers (4)

Malwaregeek
Malwaregeek

Reputation: 2274

You might want to add this

Frame()
  {
    setVisible(true);
    setSize(100,100);
  }

Upvotes: 2

ISilchev
ISilchev

Reputation: 13

Thanks a lot for answering. I wrote an example without details. I have added: setVisible(true); setSize(100,100);

to a class constructor. But i havent seen form jet.

Have to say about one feature. I'm not running method main in class Frame, i'm compiling this class Frame. In my previous exersises with java i allways run main method, but when i tried to work with GUI form and used swing library, ability to run method main have dissapiared.

If you haven't cought what i mean, i'd send a screenshot.

Upvotes: 0

Brittany Alkire
Brittany Alkire

Reputation: 430

package GUI.test;

import javax.swing.*;

public class Frame extends JFrame{
private myFrame;

public Frame()
{
 myFrame = new JFrame("put a title here");   //title not necessary but it's there if you want it
 myFrame.setSize(400,400); // sets the window size
 myFrame.setVisible(true); // toggles the frame to be visible inside the window
 myFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); // this will terminate the VM once the *last* JFrame is closed, so you can have multiple frames open and just close one
}

public void main (String[] args){

    new Frame();
}

}

Upvotes: 1

Matt H
Matt H

Reputation: 93

Frames are not visible by default--use the

setVisible(true);

method in order to display frames. You also might want to take a look at other options such as

setSize(int width, int height);

method to resize a frame,

setLocation(int xLoc, int yLoc);

to move the frame, and

setTitle(String title);

to set the title of the component.

Aside, it is good practice to use a variable to hold components so they can be manipulated when needed.

Upvotes: 2

Related Questions