【发布时间】:2014-05-10 23:09:29
【问题描述】:
我目前正在尝试将 tabIconLabels(它们是 JLabels)放在更大的 JLabel (tabAreaLabel) 上(所有标签都附加了一个 ImageIcon)。我尝试使用 OverlayLayout,但 tabIconLabels 的位置不在正确的位置。我还尝试将 tabAreaLabel 的 LayoutManager 设置为 null,然后为所有 tabIconLabels 设置 setBounds 方法,但这也不起作用。 tabIconLabels 都在一个随机位置,我不知道为什么。我也希望不使用 g.drawImage 方法,因为我希望在不久的将来删除 JLabels。
public GamePanel(final Player player) {
super(null);
this.setMemory(Memory.LOW);
this.setRevisionType(RevisionType.THREE_ONE_SEVEN);
this.setPlayer(player);
this.addMouseListener(this);
this.addMouseMotionListener(this);
this.requestFocusInWindow();
this.setFocusable(true);
final ChatBox chatBox = this.getPlayer().getChatBoxSystem().getChatBox();
final MapArea mapArea = this.getPlayer().getMapSystem().getMapArea();
final TabArea tabArea = this.getPlayer().getTabAreaSystem().getTabArea();
// final Compass compass = this.getPlayer().getCompass();
final JLabel chatBoxLabel = chatBox.getImageLabel();
this.add(chatBoxLabel);
final JLabel mapAreaLabel = mapArea.getImageLabel();
this.add(mapAreaLabel);
final JLabel tabAreaLabel = tabArea.getImageLabel();
this.add(tabAreaLabel);
chatBoxLabel.setBounds(chatBox.getLocation().getX(), chatBox.getLocation().getY(), 519, 165);
mapAreaLabel.setBounds(mapArea.getLocation().getX(), mapArea.getLocation().getY(), 246, 168);
tabAreaLabel.setBounds(tabArea.getLocation().getX(), tabArea.getLocation().getY(), 250, 338);
final short[] xLocation = {
549, 574, 605, 635, 673, 704, 730, 577, 605, 637, 674, 705, 732
};
final short[] yLocation = {
176, 175, 175, 173, 175, 175, 175, 471, 471, 472, 470, 470, 470
};
final short[] width = {
20, 25, 22, 30, 25, 24, 24, 24, 24, 27, 26, 19, 20
};
final short[] height = {
19, 24, 23, 29, 28, 27, 24, 23, 23, 24, 27, 24, 25
};
for (byte b = 0; b < 13; b++) {
final JLabel tabIconLabel = tabArea.getTabs()[b].getTabIcon();
tabAreaLabel.setLayout(new OverlayLayout(tabAreaLabel));
// tabAreaLabel.setLayout(null);
tabAreaLabel.add(tabIconLabel);
tabIconLabel.setBounds(xLocation[b], yLocation[b], width[b], height[b]);
}}
感谢您的帮助!附言我无法让代码正确地放入代码标签中。最后一个括号应该在下一行,而不是在倒数第二个括号旁边。
【问题讨论】:
-
请分享预期输出的屏幕截图链接,以使其更清晰。
-
根据发布的代码几乎没有任何建议。除了“不要使用空布局”和“你不应该为
textAreaLabel设置OverlayLayout,而是使用tabAreaLabel.setLayout(null)”。是的,这是一个矛盾。但是可以选择从头开始重写大部分代码,使用完全不同的结构,或者以某种方式以最小的更改使其运行......