Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JButton上的两个图像,底部为文本_Java_Image_User Interface_Jbutton_Settext - Fatal编程技术网

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

Java JButton上的两个图像,底部为文本,java,image,user-interface,jbutton,settext,Java,Image,User Interface,Jbutton,Settext,我已经找了很多次了,但我还没有找到它,很抱歉,如果以前有人回答过,但我认为没有。我正在为一个游戏的客户侧商店,我有界面,图像,一切,当我有文字集在中间,它显示完美。p> 但如果我将文本设置为显示在底部,它会执行以下操作: 我正在使用: shopButton.setHorizontalTextPosition(JLabel.CENTER); shopButton.setVerticalTextPosition(JLabel.BOTTOM); 设置让我困惑的文本,但这不是主要问题。我还需要在屏幕

我已经找了很多次了,但我还没有找到它,很抱歉,如果以前有人回答过,但我认为没有。我正在为一个游戏的客户侧商店,我有界面,图像,一切,当我有文字集在中间,它显示完美。p>

但如果我将文本设置为显示在底部,它会执行以下操作: 我正在使用:

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

shopButton.setBounds(x, y, width, height);
不要使用收进边界(…)。我猜你的尺寸不正确,因此文本被截断

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

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

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

谢谢,我会尝试一下,我要在jlabel上添加一个按钮,因为它创建了一个纯图片的界面^^^^至少我就是这样做的。我不认为大小不对,因为我是直接从用于裁剪图像的矩形中选取的。^^@Lygophobia默认情况下,按钮也有边距,这意味着按钮将(必须)大于放置在
图像图标中的图像。(您可以将边距插入设置为(0,0,0,0),但我也不建议手动使用收边…)