JLabel 宽度在构造函数中始终返回 0。那么如果宽度超过X值,是否插入换行符?
我创建了一个扩展 JLabel 类的类。这个类本质上使我能够“更好”地将相关的 IconImage 附加到我的 JLabel,并正确调整其大小。
当我通过代码创建一个新的 JLabel 时,我现在通过 something = new MyClass("message", 1);
对于上面的代码,我将使用 MyClass(String msg, int type) {}
构造函数。在此构造函数中,我希望能够获取用户指定的文本的宽度。如果该宽度超过某个值,假设300px
,那么我想插入一个换行符(本质上是根据最大宽度使 JLabel 自动换行)。
我删除了下面代码中将图像添加到 JLabel 的部分,因为这与问题无关 - 我创建了一个单独的类来处理这个问题,它已经可以工作了。
package org.me.myimageapp;
import javax.swing.JLabel;
/**
*
* @author Jordan
*/
public class chatBubble extends JLabel {
public static final int BUBBLE_TO_USER = 1;
public static final int BUBBLE_FROM_USER = 2;
chatBubble(String message, int bubble_type) {
if ( (bubble_type!=1) && (bubble_type!=2) ) {
return;
}
this.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
this.setText(message);
this.setVisible(true);
System.out.println("WIDTH: "+this.getSize().toString()); //WIDTH: java.awt.Dimension[width=0,height=0]
}
}
当我运行此代码时,它返回“WIDTH: java.awt.Dimension[width=0,height=0]”,就像我在注释中放置的那样。
有什么方法可以在构造函数中获取 JLabel 的宽度吗?还有 JLabel 有什么方法可以让我自动换行吗?
I've created a class which extends the JLabel class. This class essentially makes it "nicer" for me to attach the relevant IconImage to my JLabel, and size it correctly too.
When I create a new JLabel, by code, I now do it by something = new MyClass("message", 1);
For that code above I'd have the MyClass(String msg, int type) {}
constructor in place. In this constructor I want to be able to get the width of the text the user specifies. If that width is over a certain value, let's say 300px
then I'd like to insert a line break (essentially making the JLabel word-wrap according to a maximum width).
I've removed the part in the code below which adds the image to the JLabel because that's not relevant to the question - I've created a separate class to handle this, which already works.
package org.me.myimageapp;
import javax.swing.JLabel;
/**
*
* @author Jordan
*/
public class chatBubble extends JLabel {
public static final int BUBBLE_TO_USER = 1;
public static final int BUBBLE_FROM_USER = 2;
chatBubble(String message, int bubble_type) {
if ( (bubble_type!=1) && (bubble_type!=2) ) {
return;
}
this.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
this.setText(message);
this.setVisible(true);
System.out.println("WIDTH: "+this.getSize().toString()); //WIDTH: java.awt.Dimension[width=0,height=0]
}
}
When I run this code it returns "WIDTH: java.awt.Dimension[width=0,height=0]" like I placed in the comments.
Is there any way that I can get the width of a JLabel in the constructor? Also is there any way that exists for a JLabel which would allow me to word wrap?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
任何 Swing 组件的大小在渲染之前都将为零,这是没有办法解决的。您可以使用 来估计文本的大小FontMetrics 类(查看 API 链接)。
在 JLabel 中自动换行
还有一些拼凑的方法可以让您使用 HTML Edit 1
关于你的问题:
我不知道,因为我没有使用 FontMetrics。我建议您尝试一下它,看看您能想出什么,并在这个论坛的答案中搜索它的用途。
关于我的第二个解决方案,请在此处检查我对问题的回答:word-wrap- in-jlist-items
Any Swing component's size will be zero until it has been rendered, no way around that. You could estimate the size of the text though by using the FontMetrics class (check out the link to the API).
There are also kludges available to gain you word wrap in a JLabel by using HTML
Edit 1
Regarding your question:
I don't know as I haven't used FontMetrics. I'd recommend playing with it some and seeing what you can come up with, and also searching for its use in answers on this forum.
Regarding my second solution, please check my answer to a question here: word-wrap-in-jlist-items
您可以使用 html 在标签文本中获取换行符:
但是,您无法在构造函数中获取任何 swing 组件的大小。
You can use html to get a line break in the label's text:
You can't get the size of any swing component in the constructor, however.