这是我需要得到的结果Java。游戏的布局。如何将标签放在窗口的右上角?
有一个游戏区至极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");
}
}
你可以发布你现在得到的结果吗? – Aurasphere
@Aurasphere哦对不起,我已经修改了代码。但问题是,该文本是在菜单/得分区域 – Diana