简体   繁体   English

JFrame Java。Window 未显示

[英]JFrame Java. Window not showing

Heey, I tried to copy a game code from a tutorial for a simple game and was just creating the environment for it.嘿,我试图从一个简单游戏的教程中复制游戏代码,只是为它创建环境。 The problem now is I copied the code and also checked it several times and it just does not show.现在的问题是我复制了代码,也检查了几次,就是不显示。 The code itself does not seem to have any errors and the console only shows some "terminated Game [Java Application] C:/Users...." which I don´t really understand.代码本身似乎没有任何错误,控制台只显示一些“终止的游戏 [Java 应用程序] C:/Users....”,我不太明白。


package com.tutorial.main;

import java.awt.Canvas;

public class Game extends Canvas implements Runnable{
    
    private static final long serialVersionUID = 1550691097823471818L;
    
    public static final int WIDTH = 640, HEIGTH = WIDTH / 12 * 9;
    
    public Game() {
        new Window(WIDTH, HEIGTH, "Let´s build a Game!", this);
    }
    
    public synchronized void start() {
        
    }
    public void run() {
        
    }
    
    public static void main(String []args ) {

        }
    }

and the class和 class


package com.tutorial.main;

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

public class Window extends Canvas{

    enter code here
    private static final long serialVersionUID = -240840600533728354L;
    
    public Window(int width, int height, String title, Game game) {
        JFrame frame = new JFrame();
        
        frame.setPreferredSize(new Dimension(width, height));
        frame.setMinimumSize(new Dimension(width, height));
        frame.setMaximumSize(new Dimension(width, height));

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
        frame.setLocation(null);
        frame.add(game);
        frame.setVisible(true);
        game.start();
    }

}

Just create an object for the class Game.只需为 class 游戏创建一个 object。

new Game();

Also the frame.setLocation(null);还有frame.setLocation(null); throws an error.抛出错误。

That's why it is not a good idea to copy paste code blindly.这就是为什么盲目复制粘贴代码不是一个好主意。 You have two classes.你有两个班级。 A Game class which I assume will handle the game logic and a Window function which will handle the GUI related.我假设游戏 class 将处理游戏逻辑,而游戏 Window function 将处理 GUI 相关。 In your Game class, you have the main function, which is the function java will look for when you start the program.在你的游戏 class 中,你有主要的 function,这是 function java 将在你启动程序时寻找。 But it's empty.但它是空的。 That's why your program terminates.这就是你的程序终止的原因。 You should define an instance of the Game class, which in turn will trigger the Game constructor and your window will be initiated and then call whatever method you need.您应该定义 Game class 的一个实例,这将触发 Game 构造函数,您的 window 将被启动,然后调用您需要的任何方法。

Add a new Game();添加一个new Game(); in the main method.在主要方法中。 Instead of frame.setLocation(null);而不是frame.setLocation(null); do `frame.setRelativeLocation(null);做`frame.setRelativeLocation(null);

It works now, thanks guys;现在可以用了,谢谢大家; The problem was not the setLocationRelativetoNull(null).问题不在于 setLocationRelativetoNull(null)。 but that I forgot to create an object of the game class in the main method;但是我忘了在主要方法中创建游戏 class 的 object; And the Title was not being used because I created the Jframe frame = new Jframe();标题没有被使用,因为我创建了 Jframe frame = new Jframe(); instead of Jframe frame = new Jframe(title);而不是 Jframe frame = new Jframe(title);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM