user1433479
user1433479

Reputation: 135

Running a java application in a windows frame

I'm trying to learn Java by creating a simple text-based "game". It's going pretty good so far, but I want to run the game in a window instead of the console (Netbeans 8.0 in my case).

I've looked online on how to do this and I see a lot of results with JFrame, but I'm not seeing the JFrame appear at all.

My code is as follows:

Main

package helloworld;

public class HelloWorld {

    public static void main(String[] args) {
        // TODO code application logic here
        StartGame startGame = new StartGame();
        Play play = new Play();

        startGame.main();
        play.main();
    }
}

Window

package helloworld;

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

public class Window {
    private static void createWindow() {
        JFrame frame = new JFrame("HelloWorld");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

The classes startGame and play are just doing some calculations with numbers so they don't matter.

I'm not getting any error massages, it's just not showing a window.

Upvotes: 0

Views: 105

Answers (3)

mKorbel
mKorbel

Reputation: 109813

  • JFrame and valid fo all Top-Level Contianers aren't visible by default, in compare with JComponents

  • you have to call JFrame.setVisible(true) after all JComponents are added to JFrame, and sizing is done

  • also read Initial Thread

Upvotes: 3

Revive
Revive

Reputation: 2248

The problem is you have'nt set the frame to be visible or given it a size. Use

frame.setVisible(true);
frame.setSize(300,400;) //whatever size you want

Upvotes: 1

Dan Grahn
Dan Grahn

Reputation: 9414

You need to add frame.setVisible(true);. This will make the window visible.

JFrame#setVisible JavaDoc

Upvotes: 0

Related Questions