简体   繁体   English

Repl.it Java SWING,控制台一直以找不到主类而结束,如何添加一个?

[英]Repl.it Java SWING, the console keeps on ending with it not finding main class, how do I add one?

So this is the ending error I get: 所以这是我得到的结束错误:

could not find or load main class main caused by java.lang.classnotfoundexception main 找不到或加载由java.lang.classnotfoundexception main引起的主类main

This is the code I am using: Main.java 这是我正在使用的代码: Main.java

package com.test.main;

import java.awt.Canvas;

public class Main extends Canvas implements Runnable {

    private static final long serialVersionUID = -235234634745643747L;

    public static final int WIDTH = 640, HEIGHT = WIDTH /12 * 9;

    public Game() {
        new Window(WIDTH, HEIGHT, "Test Window", this);
    }

    public synchronized void start() {
    }

    public void run() {
    }

    public static void main(String args[]){
            new Game();
    }
}

Window.java Window.java

package com.test.main;

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

public class Window extends Canvas{

    private static final long serialVersionUID = -235234634745643747L;

    public Window(int width, int height, String title, Game game) {
        JFrame frame = new JFrame(title);

        frame.setPreferredSize(new Dimension(width, height));
        frame.setMaximumSize(new Dimension(width, height));
        frame.setMinimumSize(new Dimension(width, height));

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
        frame.setLocationRelativeTo(null);
        frame.add(game);
        frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
        frame.setUndecorated(true);
        frame.setVisible(true);
        game.start();
    }
}

So the question is, what am I doing wrong? 所以问题是,我做错了什么? I don't think that the error is coming from my code, I believe it has to do something with the files (PS: I am using Repl.it on java swing). 我认为错误不是来自我的代码,我认为它与文件有关(PS:我在Java swing上使用Repl.it)。

As the error says, the method is not defined properly because it's missing the return type. 如错误所述,该方法未正确定义,因为它缺少返回类型。 From the existing code it appears you intended to define a constructor because of the call 从现有代码看,您似乎打算因调用而定义构造函数

new Game();

in the main method. 在主要方法。 However a constructor must have the same name as the class where it is defined, so you should change its name, like this 但是,构造函数必须与定义它的类具有相同的名称,因此您应该更改其名称,如下所示

public Main() {

and also update the call in main() to 并将main()中的调用更新为

new Main();

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

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