我有两个文件,Display.java
(首先)然后 Lancher.java
(第二)。它们都在同一个项目下,但属于两个不同的类。我想要一个显示图像,但启动器文件的宽度和高度出现错误。我不知道为什么,因为宽度和高度都是 public
在 Display
文件。
import javax.swing.*;
import java.awt.*;
public class Display extends JFrame {
private Canvas canvas;
public Display(int width, int height) {
setTitle("Game");
setDefaultCloseOperation(EXIT_ON_CLOSE);
//Process will still be running but application closes when cross is pressed
setResizable(false);
canvas = new Canvas();
canvas.setPreferredSize(new Dimension(width, height));
//Set size to pass in with dimension
canvas.setFocusable((false));
add(canvas);
pack();
//Add canvas to display & calculate size of display frame
setLocationRelativeTo (null);
//To open frame in the center of screen as opposed to side
setVisible(true);
}
}
启动器.javapublic class Launcher {
public static void main(String[] args) {
Display display = new Display( width: 800, height: 600);
}
}
最佳答案
Display
的初始化您的 Launcher
中的对象类不是有效的语法。这将是初始化它的正确方法:
Display display = new Display(800, 600);
请注意,在将值传递到对象的构造中时,不使用参数名称/标识符。
关于java - 2 个 Java 类中的公共(public)私有(private)变量连接问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65646318/