【发布时间】:2016-08-23 04:59:08
【问题描述】:
这是我需要得到的结果
左侧有一个大小应为 (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");
}
}
【问题讨论】:
-
你能发布你现在得到的结果吗?
-
@Aurasphere 哦,对不起,我已经修改了代码。但问题是,文本位于菜单/乐谱区域的中间
标签: java layout containers panel