0

The title bar only shows after I click run, the frame is not working.... Here is my code I have no ideas what is going on! Can someone help me?

import java.awt.Canvas;
import javax.swing.JFrame;


public class Display extends Canvas implements Runnable {

    private static final long serialVersionUID = 1L;
    public static final int WIDTH = 800;
    public static final int HEIGH = 600;
    public static String TITLE = "3D Game";

    private Thread thread;
    private boolean running = false;
    private void start(){
        if(running) return;
        running = true;
        thread = new Thread();
        thread.start();

        System.out.println("Working");
    }

    private void stop(){
        if(!running)
            return;
        running = false;
        try{
        thread.join();
        }catch (Exception e){
            e.printStackTrace();
            System.exit(0);
    }   
    }
    public void run(){
        while(running){

        }
    }



    public static void main(String[] args){

        Display game = new Display();
        JFrame frame = new JFrame();
        frame.add(game);
        //frame.pack();
        frame.setTitle(TITLE);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(WIDTH,HEIGHT);
        frame.setLocationRelativeTo(null);
        frame.setResizable(false);
        frame.setVisible(true);

        game.start();

    }


}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Xiaolong
  • 79
  • 2
  • 10
  • You `while-loop` is running free, meaning that it will consume all the CPU cycles and bring your UI to a standstill (as the UI thread is not getting a chance to run) – MadProgrammer Apr 15 '17 at 22:09

2 Answers2

2

Take a look at the difference between

public static final int HEIGH = 600;

and

frame.setSize(WIDTH, HEIGHT);

Canvas indirectly defines two public static fields called WIDTH and HEIGHT set to 1 and 2 respectively

A better solution would be to override Canvass getPreferredSize method and return the preferred size you want...

@Override
public Dimension getPreferredSize() {
    return new Dimension(800, 600);
}

The you can just use pack

Display game = new Display();
JFrame frame = new JFrame();
frame.add(game);
frame.pack();
frame.setTitle(TITLE);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setVisible(true);

A word or warning against using setSize on a top level container. A window normally contains frame decorations, these are included WITHIN the windows bounds. If you use setSize, you are reducing the content area by the amount of window decorations, which will result in a smaller then expected size. Instead, you should override the getPreferredSize method of your main container and call pack. This allows the window to "pack" itself around the content, but leave space for the frame decorations, preserving the space you have requested.

FrameSetup

You can also have a look at:

For more details

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
0

You have made a typo when declaring the static height variable, You have declared it as "HEIGH" but in the JFrame you have used the word "HEIGHT"

public static final int HEIGH = 600;

frame.setSize(WIDTH,HEIGHT);

So change the variable to HEIGHT and it should work