在 JFrame setVisible 之后更改 JLabel 的首选大小
当我尝试调整 JLabel 大小时遇到问题。在我的应用程序中出现了下一个结构。理解每个列表项就像理解前一个列表项中的内容一样。
JFrame(布局为空,固定大小,不可调整大小,由不同的人使用)。
JPanel(布局为空,所有窗口的大小,我放置工作的地方)。
具有不同布局的各种 JPanel(主面板内的内容区域,您可以将其想象为 html div...)。
在布局为 null 的这一“div”之一内,存在具有流布局的 JPanel 扩展类。
在这个扩展类的每一个内部都是带有preferredSizes的标签。
问题是,当创建所有这些之后,我调用 theFrame.setVisible(true);并且一切正常。
但在我的应用程序流程中,我必须更改其中一个标签的大小。然后,我只需调用 label.setPreferredSize(d) 并且更改不会更改。如果我在设置可见框架之前而不是之后调用该函数,则该函数可以正常工作。
我感觉问题在于我没有使用 pack()、validate()、repaint() 等任何东西,因为我不知道它的作用是什么。我尝试调用 repaint 并验证 jlabel,然后重新绘制主面板,但不起作用。
我对 awt 和 swing 还比较陌生,这是为了做作业。抱歉我英语不好,感谢您的帮助。
I have a problem when I try to resize a JLabel. In my application appears the next strucutre. Understand every list item like something inside the previous list item.
JFrame (Layout null, fixed size, not resizeable, used by different people).
JPanel (Layout null, with a size of all the window, the place were I put my work).
various JPanel with different Layouts (the areas of content inside the main panel, you can think about it like html divs...).
Inside one of this "divs" with Layout null there are extended Classes of JPanel with Flow Layout.
Inside every one of this extedend Classes are labels with preferredSizes.
The thing is that when after create all of this i call theFrame.setVisible(true); and all works propertly.
But in a moment of the flow of my application I have to change the size of one of the labels. Then, I simply call label.setPreferredSize(d) and the change doesn't change. The function works propertly if I call it before set visible the frame, but not after.
I have the feeling that the problem is that I don't use nothing like pack(), validate(), repaint(), etc. Because I don't know what it works. I try call repaint and validate to the jlabel, and repaint the main panel, but doesn't works.
I'm relative new with awt and swing, and this is for homework. Sorry for my bad use of English language, and thank you for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
更改 JLabel 的大小后,请调用 重新验证()。这将导致 JLabel 调整大小,而无需等待触发重新布局的事件(例如调整父 Frame 的大小等)。
另一件需要记住的事情是,因为我不确定你在哪一类上遇到了问题。空布局(绝对定位)与布局管理器混合会导致一些奇怪的事情发生。
After you change the size of your JLabel, call revalidate(). This will cause the JLabel to be resized without waiting for an event that triggers a re-layout (such as resizing the parent Frame, etc...).
One other thing to keep in mind, as I'm not certain which class you're having problems with. Null layouts (absolute positioning) mixed with layout managers are going to cause some strange things to happen.