JButton上的两个图像,底部有文本

时间:2014-08-22 20:03:21

标签: java image user-interface jbutton settext

我已经找到了相当多的东西而且我还没有找到它,对不起,如果之前已经回答过,但我不认为它已经存在。我正在为游戏制作一个客户端商店,我有界面,图像,一切,当我在中间设置文本时它显示完美。

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循环遍历我拥有的所有按钮。如何在背景图像上添加另一个图像,以便在我不悬停在它上面以及何时悬停在它上面时显示?如果您需要更多信息,请告诉我,我会在看到后立即回复。

1 个答案:

答案 0 :(得分:0)

shopButton.setBounds(x, y, width, height);

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

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

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

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