javalayoutcontainerspanel

Java. Layout for a game. How to put labels at the right top of the window?


This is what I need to get as the result

enter image description here

There is a game area which should be size (800,600) on the left side. On the rest of the window in the right should be Menu/Score Area.

I've got two labels (scoreLabel;pointsLabel;), that I want to put at the top of the menu area, as in the image. in my version I am using gridlayout, but it is not working the way I want.

import javax.swing.*;
import java.awt.*;


public class PongFrame extends JFrame {

        private JLabel scoreLabel;
        private JLabel pointsLabel;

        public PongFrame () {

        this.setTitle("Game");
        this.setSize(1100,600);


        this.scoreLabel = new JLabel("score");
        this.pointsLabel = new JLabel("");

        JPanel labelPanel = new JPanel();
            labelPanel.setLayout(new GridLayout(1,2));
            labelPanel.add(scoreLabel);
            labelPanel.add(pointsLabel);

         JPanel gameArea = new JPanel();
            gameArea.setBackground(Color.orange);
            gameArea.setSize(800,600);


        Container con = this.getContentPane();
            con.setLayout(new GridLayout(1,2));
            con.add(gameArea);
            con.add(labelPanel);

        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

        public void setPoints (String labeltext){
        this.pointsLabel.setText(labeltext);
    }

    public static void main (String [] args){
        PongFrame window = new PongFrame();
        window.pointsLabel.setText("0");
    }

}

Solution

  • You could get this layout by using BorderLayout.

    One for the content panel, to place the game panel (with its preferred size) to the West, and the score panel to the Center (it will gain all the extra space).

    Another one for the score panel, so that you can place your labels (the labelPanel) to the North.

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.Dimension;
    import java.awt.GridLayout;
    
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.WindowConstants;
    
    public class PongFrame extends JFrame {
    
        private final JLabel scoreLabel;
        private final JLabel pointsLabel;
    
        public PongFrame() {
    
            this.setTitle("Game");
            this.setSize(1100, 600);
    
            scoreLabel = new JLabel("score");
            pointsLabel = new JLabel("");
    
            Container con = this.getContentPane();
            con.setLayout(new BorderLayout());
    
            //create score/menu panel
            JPanel scorePanel = new JPanel();
    
            scorePanel.setLayout(new BorderLayout());
    
            JPanel labelPanel = new JPanel();
            labelPanel.setLayout(new GridLayout(1, 2));
            labelPanel.add(scoreLabel);
            labelPanel.add(pointsLabel);
    
            // add the labels to the north
            scorePanel.add(labelPanel, BorderLayout.NORTH);
    
            // create game panel with a preferred size
            JPanel gameArea = new JPanel() {
    
                public Dimension getPreferredSize() {
    
                    return new Dimension(800, 600);
    
                }
            };
            gameArea.setBackground(Color.orange);
    
            // add the game panel to the west
            con.add(gameArea, BorderLayout.WEST);
    
            // add the score/menu panel to the center
            con.add(scorePanel, BorderLayout.CENTER);
    
            this.setVisible(true);
            this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        }
    
        public void setPoints(final String labeltext) {
            pointsLabel.setText(labeltext);
        }
    
        public static void main(final String[] args) {
            PongFrame window = new PongFrame();
            window.pointsLabel.setText("0");
        }
    
    }
    

    Note that you may have to play a little with the labelPanel and text alignment of the JLabels.