使用重写getPreferredSize()而不是对固定大小的组件使用setPreferredSize() [英] Use of overriding getPreferredSize() instead of using setPreferredSize() for fixed size Components

查看:116
本文介绍了使用重写getPreferredSize()而不是对固定大小的组件使用setPreferredSize()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里阅读了一些帖子,我开始为什么有些人会这样做

I read some posts here and I started why some people do

@Override
public Dimension getPreferredSize() {
    return new Dimension(500, 500);
}

而不是

setPreferredSize(new Dimension(500, 500));

不是第二个更好,因为它只创建一个维度对象,而第一个可能创建几个(即使它没有那么多浪费的内存)?或者我错了?是否存在差异?

Isn't the second one better because it creates only one Dimension object whereas the first one possibly creates several (even if it's not that much wasted memory)? Or am I wrong? Is there a difference at all?

推荐答案

一个很大的区别是价值随时间变化的方式,以及你选择的那个应该取决于你想要用代码做什么。

A big difference is how the value can change over time, and so the one you choose should be dependent on what you're wanting to do with the code.

如果你只是调用 setPreferredSize(new Dimension(500,500)) ; 在您的代码中,它将按预期执行 - 它将首选大小设置为500x500。但是,应用程序中的其他代码可能会使用新值覆盖此值 - 任何事物都可以调用 setPreferredSize(),最后一次调用此方法将是最终结果。

If you simply call setPreferredSize(new Dimension(500, 500)); in your code, it will do as you expect - it sets the preferred size to 500x500. However, other code in your application can potentially overwrite this value with a new one - anything can call setPreferredSize() and the last call to this method will be the end result.

但是,如果在代码中覆盖 getPreferredSize()方法,它将始终返回500x500。如果您的任何代码调用 setPreferredSize()方法,则无关紧要,因为它们实际上被忽略了。如果你还覆盖 getMinimumSize() getMaximumSize(),你可以强制一个组件上的固定大小t无论窗口大小和其他组件如何都会发生变化。

However, if you override the getPreferredSize() method in your code, it will always return 500x500. It doesn't matter if any of your code calls the setPreferredSize() method, because they are effectively ignored. If you also override getMinimumSize() and getMaximumSize(), you can force a fixed size on a component that shouldn't change regardless of the size of the window and the other components.

然而,正如@Andrew Thompson在评论中提到的那样,这并不能保证,因为一些布局管理员可以选择忽略这些,特别是如果你自己编写布局管理器以及向某些父容器添加自定义组件也将忽略这些方法,具体取决于组件的使用位置/方式。无论如何,它仍然比调用 setPreferredSize()更加严格,这可以被其他代码轻松调用并被完全覆盖。

However, as mentioned by @Andrew Thompson in the comments, this isn't guaranteed as some layout managers can choose to ignore these, especially if you're writing your own layout manager, and adding a custom component to some parent containers will also ignore these methods, depending on where/how the component is used. Regardless, it's still more rigid than calling setPreferredSize() which can easily be called by other code and be totally overwritten.

我还覆盖 getPreferredSize()方法(加上 getMinimumSize() getMaximumSize( ))我的任何自定义组件,例如颜色选择器,需要具有正确绘制的组件的特定尺寸。在不重写这些方法的情况下,Swing布局管理器无法理解如何根据 JFrame JPanel的大小来定位和调整自定义组件的大小

I also override the getPreferredSize() method (plus getMinimumSize() and getMaximumSize()) for any of my custom components, such as a color picker that needs to have specific dimensions for the component to be painted properly. Without overriding these methods, the Swing layout managers don't understand how your custom component can be positioned and sized appropriately for the size of the JFrame or JPanel.

这篇关于使用重写getPreferredSize()而不是对固定大小的组件使用setPreferredSize()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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