Is there anyway to add text inside picture in JFrame?(无论如何要在JFrame中的图片内添加文本吗?)
问题描述
该死,我的英语有点不对劲.我的意思是问如何在图片内部"添加文字而不是在它上面(上面),文字位于图片的中心.无论如何,谢谢你以前的帮助:).
Damn, my english is a bit off. I meant to ask how to add text "inside" picture not over(above) it , with the text be at the center of picture. thank you for previous helps anyway :).
推荐答案
一种方法是使用 OverlayLayout.对于要在两个轴上位于图像中心的文本,0.5 的对齐值应该用于 X 和Y 对于两个 JLabel 组件如下所示.
One way is to use OverlayLayout. For the text to be at the center of the image in both axes an alignment value of 0.5 should be used for both X & Y for both JLabel components shown below.
public class OverlayLabelApp {
public static void main(String args[]) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame("Overlay App");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
LayoutManager overlay = new OverlayLayout(panel);
panel.setLayout(overlay);
JLabel label1 = new JLabel("Centered Text");
label1.setForeground(Color.GREEN);
label1.setFont(new Font("SansSerif", Font.BOLD, 16));
label1.setAlignmentX(0.5f);
label1.setAlignmentY(0.5f);
panel.add(label1);
JLabel label2 =
new JLabel(new ImageIcon(OverlayLabelApp.class.getResource("/images/sunset.png"))); label2.setAlignmentX(0.5f);
label2.setAlignmentY(0.5f);
panel.add(label2);
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
});
}
}
这篇关于无论如何要在JFrame中的图片内添加文本吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:无论如何要在JFrame中的图片内添加文本吗?
- 将 Java Swing 桌面应用程序国际化的最佳实践是什么? 2022-01-01
- 未找到/usr/local/lib 中的库 2022-01-01
- 获取数字的最后一位 2022-01-01
- Eclipse 的最佳 XML 编辑器 2022-01-01
- 在 Java 中,如何将 String 转换为 char 或将 char 转换 2022-01-01
- GC_FOR_ALLOC 是否更“严重"?在调查内存使用情况时? 2022-01-01
- 如何指定 CORS 的响应标头? 2022-01-01
- 如何使 JFrame 背景和 JPanel 透明且仅显示图像 2022-01-01
- 转换 ldap 日期 2022-01-01
- java.lang.IllegalStateException:Bean 名称“类别"的 BindingResult 和普通目标对象都不能用作请求属性 2022-01-01
