gpt4 book ai didi

Java简单JFrame为null

转载 作者:行者123 更新时间:2023-11-30 07:39:23 26 4
gpt4 key购买 nike

我已经制作了简单的 JFrame,它工作正常,除非当我尝试从其他类访问它时它返回 null。所以我为它做了 getter (public static JFrame getWindow()) 它返回 null。如果我将 JFrame 设置为 public 并尝试以这种方式访问​​它,它将返回 null。当我创建时,它在游戏引擎启动后立即变为空。

主要:

public class Main {

private static String title = "2D SquareWorld 0.";
private static String version = "";

private static JFrame window;
private static Container container;

public static void main(String[] args) {

GameEngine game = new GameEngine();

window = new JFrame();

window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setTitle(title + Version.newVersion());
window.setResizable(false);
window.add(game);
window.setSize(1000, 720);
window.setLocationRelativeTo(null);
window.setVisible(true);

game.start();

}

public static Container getContainer() {

return container;

}

public static JFrame getWindow() {

return window;

}

}

游戏引擎:

package GameEngine;

public class GameEngine extends Canvas implements Runnable, KeyListener, MouseListener, MouseMotionListener {

private static final long serialVersionUID = 1L;

private Thread thread;
private boolean running;

private GameStateManager gsm;

public GameEngine() {

gsm = new GameStateManager();

}

public void start() {

thread = new Thread(this, "Game");
thread.start();

running = true;

init();

}

public void init() {

Data.setValue("ScreenWidth", getWidth());
Data.setValue("ScreenHeight", getHeight());

addKeyListener(this);

}

public void update(double delta) {

}

public void render() {

BufferStrategy bs = getBufferStrategy();

if(bs == null) {

createBufferStrategy(2);

return;

}

Graphics g = bs.getDrawGraphics();
Graphics2D g2 = (Graphics2D) g;

g2.setColor(Color.white);
g2.fillRect(0, 0, getWidth(), getHeight());

gsm.draw(g2);

g2.dispose();
bs.show();

}

public void run() {

long lastLoopTime = System.nanoTime();
long lastFpsTime = 0;

final int TARGET_FPS = Fps.TargetFPS;
final long OPTIMAL_TIME = 1000000000 / TARGET_FPS;

int CurrentFps = 0;

while(running) {

long now = System.nanoTime();
long updateLength = now - lastLoopTime;

lastLoopTime = now;

double delta = updateLength / ((double) OPTIMAL_TIME);

lastFpsTime += updateLength;

CurrentFps++;

if (lastFpsTime >= 1000000000) {

Fps.FPS = CurrentFps;

lastFpsTime = 0;
CurrentFps = 0;

}

update(delta);

render();

try {

if(((lastLoopTime-System.nanoTime() + OPTIMAL_TIME) / 1000000) >= 1) {

Thread.sleep((lastLoopTime-System.nanoTime() + OPTIMAL_TIME) / 1000000);

}

}catch(InterruptedException IE) {

}

}

}

public void mouseDragged(MouseEvent me) {

}

public void mouseMoved(MouseEvent me) {

}

public void mouseClicked(MouseEvent me) {

}

public void mouseEntered(MouseEvent me) {

}

public void mouseExited(MouseEvent me) {

}

public void mousePressed(MouseEvent me) {

}

public void mouseReleased(MouseEvent me) {

}

public void keyPressed(KeyEvent ke) {

gsm.keyPressed(ke.getKeyCode());

}

public void keyReleased(KeyEvent ke) {

}

public void keyTyped(KeyEvent ke) {

}

}

我从这个类中调用了 getWindow:

public class LoginState extends GameState {

private GameStateManager gsm;

public LoginState(GameStateManager gsm) {

this.gsm = gsm;

}

private JTextField username;
private JTextField password;

public void init() {

username = new JTextField(25);

username.setVisible(true);
username.setBounds(20, 20, 50, 50);

Main.getWindow().add(username);

}

public void draw(Graphics2D g) {

g.setColor(Color.gray);
//g.fillRect(0, 0, Data.getIntegerValue("ScreenWidth"), Data.getIntegerValue("ScreenHeight"));

}

游戏状态管理器:

public class GameStateManager {

private ArrayList<GameState> gameStates;

public static final int LOGINSTATE = 0;
public static final int MENUSTATE = 1;
public static final int PLAYSTATE = 2;

private static int currentState;

public GameStateManager() {

gameStates = new ArrayList<GameState>();

currentState = LOGINSTATE;

gameStates.add(new LoginState(this));
gameStates.add(new MenuState(this));
gameStates.add(new PlayState(this));

gameStates.get(currentState).init();

}

请帮忙。

最佳答案

感谢您的更新,但是...您仍然没有显示 LoginWindow 的初始化位置。

猜测 - 您的程序是从与您显示的不同的 main 方法开始的,因此创建 JFrame 并将其分配给静态窗口字段的 main 方法永远不会被调用。我建议您避免以这种方式使用静态方法和字段,它们依赖于 main 方法来初始化。 Java 是一种 OOP 语言 - 因此,请使您的代码符合 OOP 要求,并在保证被调用的代码中创建所需的对象,然后将它们分配给非静态字段。

编辑:只需交换两行即可:

GameEngine game = new GameEngine();
window = new JFrame();

window = new JFrame();
GameEngine game = new GameEngine();

关于Java简单JFrame为null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34908988/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com