java - 在游戏循环之前或之中运行时不显示 Swing 组件
问题描述
我实际上是在尝试解决JFrame
在我运行游戏循环时不想出现的组件的问题(请参阅代码后面的问题)。我已尽可能减少代码,以便您快速了解我的意思:
运行类
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
Game game = new Game();
game.loop();
}
});
}
游戏类
private Frame frame;
private HashMap<String,ActionListener> actions;
private HashMap<String,Image> ressources;
public Game() {
this.setActions();
this.setRessources();
frame = new Frame(actions,ressources);
}
public void loop() {
double FPS = 60;
double UPS = 60;
long initialTime = System.nanoTime();
final double timeU = 1000000000 / UPS;
final double timeF = 1000000000 / FPS;
double deltaU = 0, deltaF = 0;
int frames = 0, ticks = 0;
long timer = System.currentTimeMillis();
boolean running = true;
boolean RENDER_TIME = false;
while (running) {
...code for update, render, with a fps control
}
}
框架类
public Frame(HashMap<String,ActionListener> actions, HashMap<String,Image> ressources) {
this.setTitle(Constants.GAME_NAME);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(Constants.GAME_SIZE_X, Constants.GAME_SIZE_Y);
this.setLayout(new FlowLayout());
JButton myButton = new JButton("My Button");
this.add(myButton);
this.revalidate();
this.repaint();
this.setVisible(true);
}
这不是完整的代码,因为我不想给出无用的东西。所以在这里,我的问题是:
如果我运行此代码,则该按钮不会显示在框架中。但是,如果我game.loop()
在 Run.class 中发表评论,则窗口会显示该按钮。我不明白为什么?
我已经尝试了几天来弄清楚。我需要一些帮助。恐怕我一个人不会发现。
解决方案
推荐阅读
- c# - UnityWebRequest - 错误:415 不支持的媒体类型
- c++ - 通过按下向下按钮将变量“Richtung”更改为 0
- ios - 你能用 swift 将字符串附加到 Firebase 数组吗
- mysql - 如何在node.js lambda aws的httpget请求中获取mysql数据
- typescript - 从 createSelector 工厂推断返回类型
- python - 如何使用 nltk (python) 获得 K 均值簇的各个质心
- r - tsibble - 当没有隐含间隙时,你如何绕过隐含间隙
- ios - 一个 pod 找不到另一个 pod lib 中的 .h fie
- drupal - 在 Consumers Drupal 8 模块中获取“consumer__roles 表不存在:SELECT t.* FROM”
- unity3d - 如何将 float3 打包成一个 float