动态更改JDialog的宽度 [英] Dynamically change the width of JDialog

查看:135
本文介绍了动态更改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!

推荐答案


  1. getPreferredSize for JLabel - 基本上你必须从 JLabel 获得 textLength 像素,有3种正确的方法,但我喜欢:

  1. getPreferredSize for JLabel - basically you have to get textLength from JLabel 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屋!

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