Я на самом деле пытаюсь исправить проблему с компонентом 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);
}
Это не полный код, потому что я не хочу давать бесполезную вещь.Итак, вот моя проблема:
Если я запустил этот код, кнопка не будет отображаться в кадре.Но если я прокомментирую game.loop()
в Run.class, в окнах появится кнопка.И я не понимаю, ПОЧЕМУ?
Я пытался понять это несколько дней.Мне нужна помощь для этого.В одиночку, боюсь, не узнаю.