Adding JLabel text over JLabel Icon. Using the same JLabel text
Sorry about the title being vague but I couldn't think of what I wanted to do in the title. Anyway, for text over image, I have used a JLabel text and added it to the Icon.
JLabel icon = new JLabel(new ImageIcon);
icon.setLayout(new GridBagLayout());
add(icon);
JLabel text = new JLabel();
text.setText(language.getString("translation"));
text.setLocation(10, 10);
text.setSize(text.getPreferredSize());
icon.add(text);
I am doing i18n for my app and every time I switch locales, it adds the same JLabel to the previous one so you can see the text on top of each other. How do I use the same Jlabel instead of adding a new one on top?
Thanks
I think what you want to create is a JLabel with custom icon
You should create a JLabel and set it's icon, like this:
JLabel label = new JLabel(language.getString("translation"));
label.setIcon(new ImageIcon(/* path of your icon */));
And if you want fix the text overlap, you should set the text alignment like this:
lblText.setHorizontalTextPosition(JLabel.CENTER);
将文本标签保留为类的字段,然后调用setText()而不是重新创建/添加它们。
您可以使用setText(“abc”)在imageLabel上设置文本,并可以使用setHorizontalTextPosition(JLabel.CENTER)和setVerticalTextPosition(JLabel.BOTTOM)方法来定位文本。
dragLabel[i].setText("default");
dragLabel[i].setHorizontalTextPosition(JLabel.CENTER);
dragLabel[i].setVerticalTextPosition(JLabel.BOTTOM);
链接地址: http://www.djcxy.com/p/67664.html
上一篇: 鼠标滚过JLabel时不显示文本