【问题标题】:JLabel on top of another JLabel?JLabel 在另一个 JLabel 之上?
【发布时间】: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]);
    }}

感谢您的帮助!附言我无法让代码正确地放入代码标签中。最后一个括号应该在下一行,而不是在倒数第二个括号旁边。

http://i.stack.imgur.com/OFkKg.png

【问题讨论】:

  • 请分享预期输出的屏幕截图链接,以使其更清晰。
  • 根据发布的代码几乎没有任何建议。除了“不要使用空布局”和“你不应该为textAreaLabel设置OverlayLayout,而是使用tabAreaLabel.setLayout(null)”。是的,这是一个矛盾。但是可以选择从头开始重写大部分代码,使用完全不同的结构,或者以某种方式以最小的更改使其运行......

标签: java swing jlabel


【解决方案1】:
tabAreaLabel.setBounds(tabArea.getLocation().getX(), tabArea.getLocation().getY(), 250, 338);

tabAreaLabel 的大小为 (250, 338)。

short[] xLocation = {
    549, 574, 605, 635, 673, 704, 730, 577, 605, 637, 674, 705, 732
};

所有标签的 x 位置都大于 250,因此它们不适合标签。固定坐标或固定 tabAreaLabel 的大小。

通常,您不应硬编码图像的大小。相反,您可以使用以下内容:

tabAreaLabel.setSize( tabAreaLabel.getPreferredSize() );
tabAreaLabel.setLocation(...);

另外,不要使用 short[] 数组。使用 int[] 数组。

你的for循环也一样:

for (byte b = 0; b < 13; b++) {

不要使用字节,只需使用 int。

【讨论】:

    【解决方案2】:

    你想要的是 Swing 的 OverlayLayout。这应该使您能够对组件进行分层。

    http://docs.oracle.com/javase/7/docs/api/javax/swing/OverlayLayout.html

    【讨论】:

    • 我已经使用过 OverlayLayout,但它似乎不起作用。我在 OverlayLayout 问题的帖子末尾发布了一个图像链接。如果我取出 OverlayLayout,tabIconLabel(s) 将不会显示。
    • 不要使用 OverlayLayout。使用此布局时,您无法控制精确定位。
    • 当我移除 OverlayLayout 或设置 JLabel 的布局时(好的编程习惯?),标签图标 (tabIconLabel) 不会显示。
    • 或许试试LayeredPane,似乎能提供更精细的控制。 docs.oracle.com/javase/tutorial/uiswing/components/…
    猜你喜欢
    • 2010-12-03
    • 1970-01-01
    • 2013-03-17
    • 2016-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多