2 Java 类中的公共私有变量连接问题

问题描述

我有两个文件,Display.java(第一个)和 Lancher.java(第二个)。它们都在同一个项目下,但属于两个不同的类。我想要一个显示图像,但 Launcher 文件中的宽度和高度出现错误。我不知道为什么,因为 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);
        
        
    }

}

启动器.java

public class Launcher {
    
    public static void main(String[] args) {
        Display display = new Display( width: 800,height: 600);
    }

}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)