简体   繁体   中英

Why change height and width values in JFrame on minimize and maximize and resize in java

I am working on a project in java and i have some problem about jframes. I just create a jframe in netbeans and this frame has one jlabel. I am resize this label on formWindowOpened Event something like below.

private void formWindowOpened(java.awt.event.WindowEvent evt){                                  
   label.setSize(200, 200);
} 

But problem is that when I resize or minimize or maximize my jframe then label automatically resize to its default size. Now tell me where I am going wrong.

This is form load picture and its size is 200X200 :

这是表格载入图片

This is when the frame is resized:

这是在调整框架大小时

Swing is designed to be used with "layout managers". Each component is responsible for determining its own "preferred size". The layout manager will then use this information to determine the size/location of every component added to the panel.

When the frame is resized, the layout manager is invoked so your setSize(...) statement is ignored because the layout manager will reset the size.

Why are you trying to assign a random size to a label? The size of the label should be determined by the text and/or Icon added to the label. Let the layout manager to its job.

Read the section from the Swing tutorial on Layout Managers for more information and working examples.

是的我明白了什么是场景...但问题是我想在运行时调整面板大小...当我最小化或调整我的jframe然后再次出现这个问题.....我想这个请告诉我的解决方案,因为它对我来说非常重要.....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM