【问题标题】:Two Images on a JButton with Text at bottomJButton 上的两个图像,底部有文本
【发布时间】:2014-08-22 20:03:20
【问题描述】:

我已经找了很多次但我没有找到它,很抱歉,如果之前已经回答过这个问题,但我认为没有。我正在为游戏制作一个客户端商店,我有界面、图像、一切,当我在中间设置文本时,它显示得非常好。

http://prntscr.com/4ffdds

但如果我将文本设置为显示在底部,它会这样做:http://prntscr.com/4ffdn9 我正在使用:

shopButton.setHorizontalTextPosition(JLabel.CENTER);
shopButton.setVerticalTextPosition(JLabel.BOTTOM);

设置让我感到困惑的文本,但这不是主要问题。我还需要在屏幕上的框中有一个图像,这是我用来创建按钮的函数:

public void createBuyButton(final JButton shopButton, JLabel image, BufferedImage sprite, int x, int y, int width, int height, int i){
        shopButton.setIcon(new ImageIcon(cropImage(sprite, button.getButtonRect("BUYBUTTON"))));
        shopButton.setRolloverIcon(new ImageIcon(cropImage(sprite, button.getButtonRect("BUYBUTTON_OVERLAY"))));
        shopButton.setText(shopItems.getNameOfItems()[i]);
        shopButton.setHorizontalTextPosition(JLabel.CENTER);
        shopButton.setVerticalTextPosition(JLabel.BOTTOM);
        shopButton.setBounds(x, y, width, height);
        image.add(shopButton);
    return;
}

我正在使用 for 循环来遍历我拥有的所有按钮。我将如何在背景图像上添加另一个图像,以便它在我没有悬停在它上面以及当我悬停在它上面时显示?如果您需要更多信息,请告诉我,我会尽快回复。

【问题讨论】:

    标签: java image user-interface jbutton settext


    【解决方案1】:
    shopButton.setBounds(x, y, width, height);
    

    不要使用 setBounds(...)。我猜你的大小不正确,所以文本被截断。

    让组件的布局管理器计算尺寸。默认情况下,JLabel 没有布局管理器,因此如果您想向标签添加按钮,请尝试以下操作:

    image.setLayout( new BorderLayout() );
    image.add( shopButton );
    

    虽然我必须承认我不明白你为什么要向标签添加按钮?

    【讨论】:

    • 谢谢,我会试一试,我正在向 jlabel 添加一个按钮,因为它创建了一个纯图片的界面 ^^ 至少我就是这样做的。我不认为尺寸是错误的,因为我是直接从我用来裁剪图像的矩形中取出的^^
    • @Lygophobia 默认情况下,该按钮也有一个边距,这意味着该按钮将(必须)大于您放入ImageIcon 的图像。 (您可以将边距插图设置为 (0,0,0,0),但我也不建议手动使用 setBounds...)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-18
    • 1970-01-01
    • 2019-01-05
    • 2018-04-04
    • 1970-01-01
    • 1970-01-01
    • 2013-07-28
    相关资源
    最近更新 更多