在 JFrame setVisible 之后更改 JLabel 的首选大小

发布于 2024-10-07 20:56:58 字数 700 浏览 0 评论 0原文

当我尝试调整 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猫性小仙女 2024-10-14 20:56:58

更改 JLabel 的大小后,请调用 重新验证()。这将导致 JLabel 调整大小,而无需等待触发重新布局的事件(例如调整父 Frame 的大小等)。

JLabel lbl_test;

lbl_test.setPreferredSize(new Dimension(100, 100) );
lbl_test.revalidate();

另一件需要记住的事情是,因为我不确定你在哪一类上遇到了问题。空布局(绝对定位)与布局管理器混合会导致一些奇怪的事情发生。

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...).

JLabel lbl_test;

lbl_test.setPreferredSize(new Dimension(100, 100) );
lbl_test.revalidate();

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文