Java 是否可以将文本放在按钮中的图像顶部?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2713480/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to put text on top of a image in a button?
提问by Roman
I have .jpg images in my buttons. I also would like to put some text on top of the images. I use the following syntax for that:
我的按钮中有 .jpg 图像。我还想在图像上放一些文字。为此,我使用以下语法:
JButton btn = new JButton(label,icon);
But I do not see text in the buttons (only image). What am I doing wrong?
但是我在按钮中看不到文字(只有图像)。我究竟做错了什么?
回答by camickr
I have no idea why you are not seeing the text and icon. By default the text should be painted to the right of the icon.
我不知道你为什么看不到文字和图标。默认情况下,文本应绘制在图标的右侧。
To display the text on top of the icon you use:
要在您使用的图标顶部显示文本:
JButton button = new JButton(...);
button.setHorizontalTextPosition(JButton.CENTER);
button.setVerticalTextPosition(JButton.CENTER);
回答by ring bearer
Are you trying to overlay text on top of image or just position text outside of the image? Positiioning text outside the image is easy and is default behavior as @camickr has mentioned. To move text to top, you may :
您是要在图像顶部叠加文本还是仅将文本置于图像之外?在图像外定位文本很容易,并且是@camickr 提到的默认行为。要将文本移到顶部,您可以:
ImageIcon icon = new ImageIcon(pathToicon);
JButton myButton = new JButton("Press me!", icon);
myButton.setVerticalTextPosition(SwingContstants.TOP);
Also, note that only .gif, .jpg, and .png. will be handled by ImageIcon
另请注意,只有 .gif、.jpg 和 .png。将由 ImageIcon 处理
回答by Jason Day
By default, swing buttons have a horizontal text position of SwingConstants.TRAILING
, which will draw the text after the icon (think of a checkbox or radio button). If you want the text centered over the icon, you just need to set the button's horizontal text position:
默认情况下,swing 按钮的水平文本位置为SwingConstants.TRAILING
,它将在图标之后绘制文本(想想复选框或单选按钮)。如果你想让文本居中在图标上,你只需要设置按钮的水平文本位置:
JButton button = new JButton(label, icon);
button.setHorizontalTextPosition(SwingConstants.CENTER);
回答by bragboy
If you want to play any way you like in any swing component, you can very well override the paint() method. This way you can do whatever you like.
如果您想在任何摇摆组件中以您喜欢的方式演奏,您可以很好地覆盖paint() 方法。这样你就可以为所欲为。
package test;
import java.awt.Graphics;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class ButtonTest {
public static void main(String[] args){
new ButtonTest().test();
}
public void test(){
JFrame frame = new JFrame("Biohazard");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel pnl = new JPanel();
pnl.add(new MyButton());
frame.add(pnl);
frame.setSize(600, 600);
frame.setVisible(true);
}
class MyButton extends JButton{
public void paint(Graphics g){
//anything you want
}
}
}
回答by quicksilver
JButton button = new JButton(text,icon);
button.setVerticalTextPosition(SwingConstants.TOP);
button.setHorizontalTextPosition(SwingConstants.CENTER);
JButton button = new JButton(text,icon);
button.setVerticalTextPosition(SwingConstants.TOP);
button.setHorizontalTextPosition(SwingConstants.CENTER);
This worked for me.
这对我有用。