-2

I'm trying to put a label over the top of my game that shows the score but whenever I do it, the label appears on a blank grey screen and the game isn't visible. I'll attach the code and the area I'm trying to insert the JLabel. (I know the code is poorly written but I'm still learning. Any tips and tricks would be good)

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);
    }


    }
    }
SuperHanz98
  • 2,090
  • 2
  • 16
  • 33
  • Try `frame.add(game,BorderLayout.CENTER);` and `frame.add(score,BorderLayout.NORTH);` , and only call `frame.setVisible(true);` once all your graphical layout is done . – Arnaud Aug 30 '16 at 13:19
  • That worked thanks! – SuperHanz98 Aug 30 '16 at 13:21
  • The default layout manager used by a JFrame (well by its content pane) is BorderLayout, to explain my answer. You can check how layout managers work here : https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html – Arnaud Aug 30 '16 at 13:24
  • Thank you @Berger – SuperHanz98 Aug 30 '16 at 13:26

2 Answers2

4

You should look into layouts, it may be because you are adding both elements to the same frame. Consider adding elements into a panel and then the panel to the frame:

JFrame frame = new JFrame("Grid Hopper");
frame.setSize(1120,800);
frame.setLayout(new FlowLayout());
JPanel pane = new JPanel();
pane.add(game);
pane.add(score);
frame.add(pane);

Theres a more complete description here

Community
  • 1
  • 1
D3181
  • 2,037
  • 5
  • 19
  • 44
4

Start over, and

  1. Get most of that code out of the main method and into instance methods
  2. Use a Swing Timer for your game loop, not a while true loop
  3. Learn about and use the Swing layouts to your advantage. For instance you would then know that JFrames use a BorderLayout and that by adding your JLabel to the BorderLayout.PAGE_START position, it would sit on top. e.g., frame.add(score, BorderLayout.PAGE_START);
  4. Avoid setting sizes and instead work with the layout managers and preferred sizes.
  5. Most important -- start with first principles, including learning clean object-oriented programming, before creating GUI's. This will save you much future grief.
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373