我正在尝试创建一个扫雷类型的游戏,并在顶部的GridLayout周围设计了界面,在底部设计了一个JPanel。我在底部的信息面板上安排JComponents时遇到问题。我正在使用GroupLayout来安排信息面板中的项目。
下面是它应该如何显示的基本图:
以下是实际结果:
我不明白为什么最底层的JLabel(大的是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);
}
此致
杰米
答案 0 :(得分:1)
显示JLabel(因此底部的可用空间),但没有颜色。请改用JPanels(背景仅适用于不透明组件,请参阅http://docs.oracle.com/javase/7/docs/api/javax/swing/JComponent.html#setBackground%28java.awt.Color%29)。