java JLabel 文本超出父 JLabel 宽度 [英] java JLabel text exceeds parent JLabel width

查看:53
本文介绍了java JLabel 文本超出父 JLabel 宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 iconLabel 的类,它扩展 JLabel 来制作按钮.我使用 Font Awesome 字体将 JLabel 的文本设置为图标.我还在构造函数中向那个 iconLabel 对象添加另一个 JLabel 以在图标上显示文本.问题是图标上的文字超过了图标的宽度,所以你会得到...".我怎样才能使 JLabel 文本可能超过图标宽度?这是一张图片以供澄清.

解决方案

问题是图标上的文字超过了图标的宽度,所以你会得到...".

一个 JLabel 可以显示一个图标,在图标顶部绘制有文本.不需要自定义类来执行此操作.

基本代码为:

JLabel label1 = new JLabel( new ImageIcon(...) );label1.setText("简单方法");label1.setHorizo​​ntalTextPosition(JLabel.CENTER);label1.setVerticalTextPosition(JLabel.CENTER);

<块引用>

问题是我想使用字体真棒图标

所以您使用 setText() 方法并绘制了一个图标?酷!

也许您可以使用带有 OverlayLayout 的 JPanel.然后将两个标签添加到面板,确保对齐 X/Y 值都是 0.5f,以便组件在面板中居中.现在面板应该是最大标签的大小.

或者您可以使用文本图标.此类允许您从文本字符串创建图标.您仍然需要标记,但尺寸应该可以正常工作.

代码应该是这样的:

JLabel iconLabel = new JLabel();iconLabel.setFont(...);TextIcon icon = new TextIcon(iconLabel, "...")Jlabel label = new JLabel("一些文字");label.setIcon(icon);

I have a class called iconLabel that extends JLabel to make buttons. I use the Font Awesome font to set the text of the JLabel to an icon. I also add another JLabel to that iconLabel object in the constructor to display text on the icon. The problem is that the text on the icon exceeds the width of the icon, so you'll get "...". How can I make it so the JLabel text may exceed the icon width? Here is an image for clarification.

解决方案

The problem is that the text on the icon exceeds the width of the icon, so you'll get "...".

A JLabel can display an Icon with text painted on top of the Icon. There is no need for a custom class to do this.

The basic code is:

JLabel label1 = new JLabel( new ImageIcon(...) );
label1.setText( "Easy Way" );
label1.setHorizontalTextPosition(JLabel.CENTER);
label1.setVerticalTextPosition(JLabel.CENTER);

The thing is that I want to use the font awesome icons

So you use the setText() method and you get an Icon painted? Cool!

Maybe you can use a JPanel with an OverlayLayout. Then you add the two labels to the panel, making sure the alignmentX/Y values are both 0.5f so the components are centered in the panel. Now the panel should be the size of the largest label.

Or maybe you can use the Text Icon. This class allows you to create an Icon from a text String. You will still need to labels but the sizing should work correctly.

The code should be something like:

JLabel iconLabel = new JLabel();
iconLabel.setFont(...);
TextIcon icon = new TextIcon(iconLabel, "...")
Jlabel label = new JLabel("some text");
label.setIcon(icon);

这篇关于java JLabel 文本超出父 JLabel 宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆