我有一个标签,可以显示从“0 ft”到“999 ft”的字符串,因此最大需要大小为 6 的字符串。我正在使用网格布局,以便我可以将该标签放在它所在的特定列中应该。我也想要标签的背景。但问题是我无法正确设置背景的大小。
public Controller() {
super(new GridLayout(9, 9));
try {
Background = ImageIO.read(getClass().getResourceAsStream(
"background.jpg"));
TestImage = ImageIO.read(getClass().getResourceAsStream(
"VTOLHorizontalLevelIndicator.png"));
setPreferredSize(new Dimension(Background.getWidth(null),
Background.getHeight(null)));
} catch (IOException ex) {
ex.printStackTrace();
}
for (Direction direction : Direction.values()) {
directionMap.put(direction, false);
}
setKeyBindings();
Timer timer = new Timer(TIMER_DELAY, new TimerListener());
timer.start();
int i = 3;
int j = 11;
JLabel[][] panelHolder = new JLabel[i][j];
setLayout(new GridLayout(i, j));
for (int m = 0; m <= 2; m++) {
for (int n = 0; n <= 10; n++) {
if (m == 1 && n == 1) {
lblAltitude.setFont(new Font("Gotham Bold",1,12));
lblAltitude.setToolTipText("Altitude");
lblAltitude.setOpaque(true);
lblAltitude.setBackground(Color.black);
lblAltitude.setMinimumSize(new Dimension(50,25));
add(lblAltitude);
} else {
panelHolder[m][n] = new JLabel(" ");
add(panelHolder[m][n]);
}
}
}
}
上面的代码显示了我从中初始化 GridLayout 和标签的构造函数。 我使用了正确的布局吗? 我主要关心的是在正确的位置获取标签,可能不是网格方式,而是在此 GUI 中的某些坐标处。