简体   繁体   English

在JPanel-BorderLayout上添加JLabel

[英]Add JLabel on JPanel - BorderLayout

I want to create a Pong game with a moving ball and all the stuff. 我想用一个动球和所有东西来创建一个Pong游戏。 I'm now working on my score Labels that I want to add on each side of the center line. 我现在正在处理要在中线两侧添加的乐谱标签。 One label for the computer score and an other for the player score. 一个标签用于计算机分数,另一个标签用于玩家分数。 The problem is that my panel is set to the BorderLayout. 问题是我的面板设置为BorderLayout。 I don't know how i can add my panel to that location. 我不知道如何将面板添加到该位置。

here is my constructor code : 这是我的构造函数代码:

 * Constructor : PlayPanel.java
 */
// ==============================================
public PlayPanel() {
    super(new BorderLayout());
    setBackground(Color.DARK_GRAY);

    panPlayer1 = new JPanel();
    panComputer = new JPanel();

    padPlayer1 = new JPanel();
    padComputer = new JPanel();

    padPlayer1.setPreferredSize(PADPANEL_SIZE);
    padComputer.setPreferredSize(PADPANEL_SIZE);

    panPlayer1.setBackground(PAN_PLAY);
    panComputer.setBackground(PAN_PLAY);

    panPlayer1.add(padPlayer1);
    panComputer.add(padComputer);

    add(panPlayer1, BorderLayout.WEST);
    add(panComputer, BorderLayout.EAST);

    player1Score.setFont(FONT_SCORE);
    ComputerScore.setFont(FONT_SCORE);

    // Add them to each side if the line !?!?!?!?!?!?
    add(player1Score);
    add(ComputerScore);

    addMouseMotionListener(this);

    panPlayer1.addComponentListener(new ComponentAdapter() {

        @Override
        public void componentResized(ComponentEvent arg0) {
            setPanPanelWidth(arg0.getComponent().getSize().width);
            setPanPanelHeight(arg0.getComponent().getSize().height);
        }

    });

    addComponentListener(new ComponentAdapter() {

        @Override
        public void componentResized(ComponentEvent arg0) {

            setPlayPanelWidth(arg0.getComponent().getSize().width);
            setPlayPanelHeight(arg0.getComponent().getSize().height);
        }

    });
}

You have two options: 您有两种选择:

  1. Add them both to a 1 row, 2 column GridLayout and then add that to BorderLayout.NORTH of your main panel 将它们都添加到1行2列GridLayout ,然后将其添加到主面板的BorderLayout.NORTH

  2. Incorporate them into your playerPanel and ComputerPanel objects. 将它们合并到playerPanelComputerPanel对象中。 (I would also re-case the ComputerPanel to be computerPanel ). (我还将将ComputerPanel的大小写重新设置为computerPanel )。

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

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