且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Java ComponentResized - 检测是用户调整了窗口大小还是以编程方式调整了窗口大小

更新时间:2023-11-13 09:41:04

我相应地调整了框架的大小

I resize the frame accordingly

使用 pack() 有什么问题?

Whats wrong with using pack()?

我删除和添加组件,每次这样做时,

I remove and add components and each time I do this,

那么您应该在此处设置布尔值:

Then this is where you should set your Boolean value:

programResize == true:
panel.add(...);
frame.setSize(...); // this should cause the ComponentListener to fire
// the ComponentListener would then use programResize == false;

或者更好的选择可能是:

Or a better option option could be:

component.removeComponentListener(...);
panel.add(...);
frame.setSize(...);
component.addComponentListener(...);

我更喜欢这种方法,因为所有基于手动更新的逻辑都包含在一个地方,不需要定义布尔变量.

I like this approach better because all the logic based on the manual update is self contained in one place and there is no need to define a Boolean variable.