有关软件包理解的问题

问题描述 投票:-1回答:1

我正在用Java开发游戏,因此需要一些帮助。我想将代码组织在软件包中,但是我不知道属于哪个.java文件。 My file organization

App.java

package main;
import engine.io.Window;
import org.lwjgl.glfw.GLFW;
public class App implements Runnable {
    public Thread game;
    public static Window window;
    public static final int WIDTH = 1280, HEIGHT = 760;
    public void start() {
        game = new Thread(this, "game");
        game.start();
    }
    public static void init() {
        window = new Window(WIDTH, HEIGHT, "Game");
        window.create();
    }
    public void run() {
        init();
        while (!window.shouldClose()) {
            update();
            render();
        } 
    }
    private void update() {
        window.update();
    }
    private void render() {
        window.swapBuffers();
    }
    public static void main(String[] args) {
        new App().start();
    }
}

和Window.java

package engine.io;

import org.lwjgl.glfw.GLFW;
import org.lwjgl.glfw.GLFWVidMode;

public class Window {
    private int width, height;
    private String title;
    private long window;
    public int frames;
    public static long time;

    public Window(int width, int height, String title) {
        this.width = width;
        this.height = height;
        this.title = title;
    }

    public void create() {
        if (!GLFW.glfwInit()) {
            System.err.println("ERROR: GLFW wasn't initializied");
            return;
        }

        window = GLFW.glfwCreateWindow(width, height, title, 0, 0);

        if (window == 0) {
            System.err.println("ERROR: Window wasn't created");
            return;
        }

        GLFWVidMode videoMode = GLFW.glfwGetVideoMode(GLFW.glfwGetPrimaryMonitor());
        GLFW.glfwSetWindowPos(window, (videoMode.width() - width) / 2, (videoMode.height() - height) / 2);
        GLFW.glfwMakeContextCurrent(window);


        GLFW.glfwShowWindow(window);

        GLFW.glfwSwapInterval(1);

        time = System.currentTimeMillis();
    }

    public void update() {
        GLFW.glfwPollEvents();
        frames++;
        if (System.currentTimeMillis() > time + 1000) {
            GLFW.glfwSetWindowTitle(window, title + " | FPS: " + frames);
            time = System.currentTimeMillis();
            frames = 0;
        }
    }

    public void swapBuffers() {
        GLFW.glfwSwapBuffers(window);
    }

    public boolean shouldClose() {
        return GLFW.glfwWindowShouldClose(window);
    }
}

我收到以下错误:声明的软件包“ main”与预期的软件包“ ourRTS”不匹配和导入引擎无法解析。

java package naming
1个回答
0
投票

将一个目录视为“默认”或“主”目录;不管它是什么,它的名称都不必与项目相关。它可以位于磁盘的根目录与否,也可以位于磁盘的根目录下,它的位置也可以无关紧要。

在您的应用程序中,每个程序包中的名称(在第一个句点之前)是“默认”目录下的目录名称。每个软件包(每个周期之后)中的连续名称是上一个软件包名称段下的目录级别。

您具有'engine.io',因此在默认目录下将具有目录“ engine”,在该目录下将具有一个名为“ io”的目录。软件包“ engine.io”中的所有Java文件都必须位于io目录中。

这些是目录和包名称,因为它们必须存在于Java文件和项目中。在eclipse中,默认目录通常称为“ src”,有时顶层软件包名称目录位于该目录下。我无法确定您的组织方式是否如此,但这是一种常用的方法。

© www.soinside.com 2019 - 2024. All rights reserved.