在 运行 之前或游戏循环中不显示 Swing 组件
Not showing Swing component when running before or in the game loop
我实际上是在尝试修复 JFrame
组件的一个问题,当我 运行 我的游戏循环时,该组件不想显示(请参阅代码后的问题)。我已尽可能减少代码,以便您快速理解我的意思:
Run.class
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
Game game = new Game();
game.loop();
}
});
}
Game.class
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
}
}
Frame.class
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);
}
这不是完整的代码,因为我不想给出一个没用的东西。所以在这里,我的问题是:
如果我 运行 此代码,按钮将不会显示在框架中。但是,如果我在 Run.class 中注释 game.loop()
,则 windows 显示按钮。我不明白为什么?
我已经尝试了几天才弄明白。我需要一些帮助。一个人怕是查不出来
想要阻止可以为您处理 "looping" 的 Event Dispatching Thread by running a long process you can use swing Timer:
ActionListener animate = e -> {
game.oneFrame();
panel.repaint();
};
timer = new Timer(50, animate);
timer.start();
public void oneFrame(){
//update what is needed for one "frame"
}
需要更多帮助post mcve.
我实际上是在尝试修复 JFrame
组件的一个问题,当我 运行 我的游戏循环时,该组件不想显示(请参阅代码后的问题)。我已尽可能减少代码,以便您快速理解我的意思:
Run.class
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
Game game = new Game();
game.loop();
}
});
}
Game.class
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
}
}
Frame.class
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);
}
这不是完整的代码,因为我不想给出一个没用的东西。所以在这里,我的问题是:
如果我 运行 此代码,按钮将不会显示在框架中。但是,如果我在 Run.class 中注释 game.loop()
,则 windows 显示按钮。我不明白为什么?
我已经尝试了几天才弄明白。我需要一些帮助。一个人怕是查不出来
想要阻止可以为您处理 "looping" 的 Event Dispatching Thread by running a long process you can use swing Timer:
ActionListener animate = e -> {
game.oneFrame();
panel.repaint();
};
timer = new Timer(50, animate);
timer.start();
public void oneFrame(){
//update what is needed for one "frame"
}
需要更多帮助post mcve.