我试图在我的游戏顶部放置一个标签来显示分数,但每当我这样做时,标签就出现在一个空白的灰色屏幕上,游戏也不可见。我将附加代码和我试图插入JLabel的区域。 (我知道代码写得不好,但我还在学习。任何提示和技巧都会很好)
public static void main(String[] args) throws InterruptedException {
int timer = 0;
int Time = 0;
JFrame frame = new JFrame("Grid Hopper");
frame.setSize(1120,800);
frame.setResizable(false);
frame.setVisible(true);
frame.setTitle("Grid Hopper");
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridHopper game = new GridHopper();
game.setBackground(Color.gray);
frame.add(game);
JLabel score = new JLabel();
score.setText("Score to be calculated");
frame.add(score);
//ACTUAL GAME-RUN WHILE LOOP!
while(game.dead != true){
game.repaint();
//game.moveBall(); NOW USED FOR PLAYER MOVEMENT
if (Time > 3){
game.moveBall5();
}
if (Time > 10){
game.moveBall4();
}
if (Time > 30){
game.moveBall2();
}
if (Time > 60){
game.moveBall3();
}
game.collision();
Thread.sleep(2);
timer++;
if (timer%500 == 0){
Time++;
System.out.println(Time);
}
}
}
答案 0 :(得分:4)
您应该查看布局,这可能是因为您要将两个元素添加到同一帧中。考虑将元素添加到面板中,然后将面板添加到框架中:
Array
(
[cat1] => Array
(
[0] => mani
[1] => harry
)
[cat2] => Array
(
[0] => mani
[1] => rocky
)
[cat3] => Array
(
[0] => ravi
[1] => rocky
)
[cat4] => Array
(
[0] => ravi
[1] => harry
)
)
有更完整的说明here
答案 1 :(得分:4)
重新开始,
frame.add(score, BorderLayout.PAGE_START);