动态更改JDialog的宽度 [英] Dynamically change the width of JDialog
问题描述
我创建了一个包含JLabel的JDialog。因为基于用户输入而改变的文本长度可以包含大量字符,所以需要根据JDialog的长度大小动态地改变JDialog的长度。我尝试过pack()方法,但事实并非如此。谁能给我一些提示?提前致谢!
I have created a JDialog which contains a JLabel. Because the length of text, which changes based on users' input, can contain a large number of characters, there is the need to dynamically change the length of the JDialog based on the size of length of the JDialog. I have tried the pack() method but it's not the case for it. Can anyone give me some tips? Thanks in advance!
推荐答案
-
getPreferredSize
forJLabel
- 基本上你必须从JLabel
获得textLength
像素,有3种正确的方法,但我喜欢:
getPreferredSize
forJLabel
- basically you have to gettextLength
fromJLabel
in pixels, there are 3 correct ways, but I love:
SwingUtilities.computeStringWidth(FontMetrics fm, String str)
现在您可以 setPreferredSize
JLabel
正确(请通过defalut将+5 - +10添加到返回 Integer
> SwingUtilities.computeStringWidth )
Now you are able to setPreferredSize
for JLabel
correctly (please by defalut is there needed to add +5 - +10 to the Integer
that returns SwingUtilities.computeStringWidth
)
这篇关于动态更改JDialog的宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!