我正在尝试创建一个扫雷类型的游戏,并围绕顶部的 GridLayout 和底部的 JPanel 设计了界面。我在底部的信息面板上安排 JComponents 时遇到问题。我正在使用 GroupLayout 来排列信息面板中的项目。
下面是它应该如何显示的基本图:
这是实际结果:
我不明白为什么最底部的 JLabels(大的那些是 playerXTurnIndicator 标签)根本没有出现。当然,当默认情况下将它们添加到没有文本的组中时,它们会采用水平或垂直布局规定的区域大小吗?
private void createComponents() {
playerOneNameLabel = new JLabel("Name : ");
playerTwoNameLabel = new JLabel("Name : ");
playerOneScoreLabel = new JLabel("Score : ");
playerTwoScoreLabel = new JLabel("Score : ");
playerOneNameDataLabel = new JLabel("");
playerTwoNameDataLabel = new JLabel("");
playerOneScoreDataLabel = new JLabel("0");
playerTwoScoreDataLabel = new JLabel("0");
playerOneTurnIndicatorLabel = new JLabel();
playerOneTurnIndicatorLabel.setBackground(Color.GREEN);
playerOneTurnIndicatorLabel.setPreferredSize(new Dimension(100, 20));
playerTwoTurnIndicatorLabel = new JLabel();
playerTwoTurnIndicatorLabel.setBackground(Color.RED);
playerTwoTurnIndicatorLabel.setPreferredSize(new Dimension(100, 20));
infoMinesRemainingLabel = new JLabel("Mines Remaining : ");
infoMinesRemainingData = new JLabel("0");
infoTileCoordLabel = new JLabel("Tile : ");
infoTileCoordDataLabel = new JLabel("0, 0");
playerOneBombButton = new JButton("Bomb");
playerTwoBombButton = new JButton("Bomb");
GroupLayout layout = new GroupLayout(this);
layout.setAutoCreateContainerGaps(true);
layout.setAutoCreateGaps(true);
layout.setHorizontalGroup(
layout.createSequentialGroup()
.addGroup(
layout.createSequentialGroup()
.addGroup(
layout.createParallelGroup()
.addGroup(
layout.createSequentialGroup()
.addComponent(playerOneNameLabel)
.addComponent(playerOneNameDataLabel))
.addGroup(
layout.createSequentialGroup()
.addComponent(playerOneScoreLabel)
.addComponent(playerOneScoreDataLabel))
.addComponent(playerOneTurnIndicatorLabel))
.addComponent(playerOneBombButton))
.addGap(10)
.addGroup(
layout.createSequentialGroup()
.addGroup(
layout.createParallelGroup()
.addComponent(infoMinesRemainingLabel)
.addComponent(infoTileCoordLabel))
.addGroup(
layout.createParallelGroup()
.addComponent(infoMinesRemainingData)
.addComponent(infoTileCoordDataLabel)))
.addGap(10)
.addGroup(
layout.createSequentialGroup()
.addComponent(playerTwoBombButton)
.addGroup(
layout.createParallelGroup()
.addGroup(
layout.createSequentialGroup()
.addComponent(playerTwoNameLabel)
.addComponent(playerTwoNameDataLabel))
.addGroup(
layout.createSequentialGroup()
.addComponent(playerTwoScoreLabel)
.addComponent(playerTwoScoreDataLabel))
.addComponent(playerTwoTurnIndicatorLabel))));
layout.setVerticalGroup(
layout.createParallelGroup()
.addGroup(
layout.createParallelGroup()
.addGroup(
layout.createSequentialGroup()
.addGroup(
layout.createParallelGroup()
.addComponent(playerOneNameLabel)
.addComponent(playerOneNameDataLabel))
.addGroup(
layout.createSequentialGroup()
.addGroup(
layout.createParallelGroup()
.addComponent(playerOneScoreLabel)
.addComponent(playerOneScoreDataLabel)))
.addComponent(playerOneTurnIndicatorLabel))
.addComponent(playerOneBombButton)
)
.addGap(50)
.addGroup(
layout.createSequentialGroup()
.addGroup(
layout.createParallelGroup()
.addComponent(infoMinesRemainingLabel)
.addComponent(infoMinesRemainingData))
.addGroup(
layout.createParallelGroup()
.addComponent(infoTileCoordLabel)
.addComponent(infoTileCoordDataLabel)))
.addGap(50)
.addGroup(
layout.createParallelGroup()
.addComponent(playerTwoBombButton)
.addGroup(
layout.createSequentialGroup()
.addGroup(
layout.createParallelGroup()
.addComponent(playerTwoNameLabel)
.addComponent(playerTwoNameDataLabel))
.addGroup(
layout.createParallelGroup()
.addComponent(playerTwoScoreLabel)
.addComponent(playerTwoScoreDataLabel))
.addComponent(playerTwoTurnIndicatorLabel))));
this.setLayout(layout);
}
问候,
杰米