我有这个小一点的代码,但我不知道为什么它不能读取的值aa.width从aa类ab类.
我正在尝试创建一个基本游戏.该文件ab.java具有print语句.我知道我没有调用函数aa,但这就是java的工作方式.我以为我可以这样做aa.width,我会得到价值,因为它是一个公共变量...感谢您的帮助
aa.java:
package com.Game;
import javax.swing.JFrame;
public class aa {
public static ab f= new ab();
public static int width = 600;
public static int height = 400;
public static void main(String args[]){
f.setSize(width,height);
f.setResizable(false);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setTitle("game first");
f.setLocationRelativeTo(null);
System.out.println("main window running!");
}
}
Run Code Online (Sandbox Code Playgroud)
ab.java
package com.Game;
import java.awt.GridLayout;
import javax.swing.*;
public class ab extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
public ac panel;
public ab() {
panel = new ac(this);
setLayout(new GridLayout (1,1,0,0));
add(panel);
System.out.format("the value of width %d\n", aa.width);
}
}
Run Code Online (Sandbox Code Playgroud)
打印出来:
the value of width 0
main window running!
Run Code Online (Sandbox Code Playgroud)