繁体   English   中英

Java的。 游戏布局。 如何在窗口的右上方放置标签?

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

这就是我需要得到的结果

在此输入图像描述

左侧有一个游戏区域(尺寸为800,600)。 在右侧窗口的其余部分应为菜单/分数区域。

我有两个标签(scoreLabel; pointsLabel;),我想把它放在菜单区域的顶部,就像在图像中一样。 在我的版本我使用gridlayout,但它不是我想要的方式:)

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

}

您可以使用BorderLayout获得此布局。

一个用于内容面板,将游戏面板(具有其首选尺寸)放置到西方,将得分面板放置到中心(它将获得所有额外空间)。

另一个用于评分面板,以便您可以将标签( labelPanel )放置到北方。

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

}

请注意,您可能需要使用JLabellabelPanel和文本对齐方式。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM