[英]JButton background image text
I placed an image on a JButton using the btn.setIcon
method.The problem is that the text is pushed right by the image and I can see the background button color and only the first letter of the text even thogh their horizontal alignement are both placed on center(I tried all variations). 我使用
btn.setIcon
方法将图像放置在JButton上,问题是图像向右推动了文本,我可以看到背景按钮的颜色,并且即使它们的水平对齐方式也仅放置了文本的第一个字母在中心(我尝试了所有变体)。
NOTE:What I need is a full background image with the text OVER the image.Not just an icon. 注意:我需要的是一张完整的背景图像,图像上方有文字,而不仅仅是一个图标。
How can I make that happen?
我该如何实现?
If you post your code, as a working example, we will be able to give more help. 如果您发布代码(作为工作示例),我们将能够提供更多帮助。 – user2891133
–用户2891133
There it is: 那里是:
public void placeResizedImage(JBUtton btn){
URL buttonImage=this.getClass().getResource("/source/woodButton.png");
BufferedImage bg=null;
try {
bg = ImageIO.read(buttonImage);
} catch (IOException e) {
System.out.println("Background image does not exist!");
return null;
}
Image imageBuff=bg.getScaledInstance((int)btn.getSize().getWidth(),(int) btn.getSize().getHeight(),
Image.SCALE_SMOOTH);
btn.setIcon(new ImageIcon(imageBuff));
}
JButton button = new JButton( "Centered" );
button.setIcon( new ImageIcon("mong.jpg") );
button.setHorizontalTextPosition(JButton.CENTER);
button.setVerticalTextPosition(JButton.CENTER);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.