2016-04-28 108 views
1

这是我需要得到的结果Java。游戏的布局。如何将标签放在窗口的右上角?

enter image description here

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

我有两个标签(scoreLabel; pointsLabel;),我想把它放在菜单区域的顶部,就像在图像中一样。在我的版本我使用网格布局,但它不工作我想:)

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

} 
+0

你可以发布你现在得到的结果吗? – Aurasphere

+0

@Aurasphere哦对不起,我已经修改了代码。但问题是,该文本是在菜单/得分区域 – Diana

回答

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

} 

请注意,您可能需要发挥一点与JLabel S的labelPanel和文本对齐方式。

+0

非常感谢:)这正是我需要的。对不起,也许是一个愚蠢的问题。为什么在'setPoints'方法中将变量放在“final”之前会更好? – Diana

+0

@戴安娜:有一个非常好的解释:http://stackoverflow.com/questions/2236599/final-keyword-in-method-parameters。然而,它不是强制性的(我偏好设置Eclipse,自动添加)。 – Berger