java 对于固定大小的组件,使用覆盖 getPreferredSize() 而不是使用 setPreferredSize()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10866762/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 02:53:45  来源:igfitidea点击:

Use of overriding getPreferredSize() instead of using setPreferredSize() for fixed size Components

javaswinglayout-manager

提问by IchBinKeinBaum

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

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

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

instead of

代替

setPreferredSize(new Dimension(500, 500));

Isn't the second one better because it creates only one Dimensionobject 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?

第二个不是更好,因为它只创建一个Dimension对象而第一个可能创建几个(即使它没有那么多浪费的内存)?还是我错了?有什么区别吗?

采纳答案by wattostudios

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.

一个很大的区别是值如何随时间变化,因此您选择的值应该取决于您想要对代码做什么。

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.

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

However, if you override the getPreferredSize()method in your code, it will alwaysreturn 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.

但是,如果您getPreferredSize()在代码中覆盖该方法,它将始终返回 500x500。您的任何代码是否调用该setPreferredSize()方法都没有关系,因为它们实际上被忽略了。如果您还覆盖了getMinimumSize()and getMaximumSize(),则可以在组件上强制固定大小,无论窗口和其他组件的大小如何,该大小都不应更改。

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.

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

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 JFrameor JPanel.

我还覆盖了任何自定义组件的getPreferredSize()方法(加号getMinimumSize()getMaximumSize()),例如需要具有特定尺寸才能正确绘制组件的颜色选择器。如果不覆盖这些方法,Swing 布局管理器就无法理解如何根据JFrameor 的大小适当地定位和调整自定义组件的大小JPanel