Swing和延迟加载组件 [英] Swing and lazy loading components

查看:169
本文介绍了Swing和延迟加载组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Eclipse插件 Visual Editor 构建Java Swing接口。由于我不是代码WYSIWYG(UI)编辑器生成的忠实粉丝,我想优化它,当我注意到,编辑器使用延迟加载实现所有元素,如下所示:

I have used the Eclipse plugin Visual Editor to construct Java Swing interfaces. As I'm not a big fan of the code WYSIWYG (UI) editors generate, I wanted to optimize it, when I noticed, that the editor implemented all elements using lazy loading like this:

private JPanel getSomePanel ()
{
    if ( somePanel == null )
    {
        somePanel = new JPanel();
        // construct the panel
    }
    return somePanel;
}

我知道延迟加载用于获得更好的性能问题不会立即使用。然而,对于大多数用户界面而言,这没有多大意义,因为例如窗口通常应该从一开始就显示其上的所有组件。在我的情况下也是如此,我有一个相当简单的清晰布局,其中所有组件都应该在显示窗口时存在。

I know that lazy loading is used to get better performance, when the objects in question are not used immediately. However for most user interfaces this makes less sense, as a window for example should usually show all components on it right from the beginning. This is also the case in my situation where I have a rather simple clear layout, where all components are expected to exist when the window is displayed.

Visual Editor添加了一个 initialize 在根容器的构造函数中调用,构造根面板并添加所有其他元素(通过延迟加载)。所以实际上所有组件都是在构造根容器的时候创建的,只是嵌套在多个方法中。

Visual Editor added an initialize call in the root container's constructor in which the root panel is constructed and all the other elements are added (via lazy loading). So actually all components are created right when the root container is constructed, just nested into multiple methods.

在这种情况下是否真的有任何延迟加载用途?在哪些UI情况下我应该使用延迟加载?当使用延迟加载时,我实际上甚至允许直接访问成员变量 - 或者我应该每次都调用getter吗?

Is there actually any use for lazy loading in this case? In which UI cases should I use lazy loading? And when using lazy loading, am I actually even allowed to access the member variables directly - or should I call the getter each time?

谢谢!

推荐答案

使用延迟加载时,每次访问成员变量时都应始终使用getter。这是延迟加载的基本部分。

When you use lazy loading, you should always use the getter each time you access the member variables. This is a fundamental part of lazy loading.

但是,在这种情况下,您说过,没有理由使用延迟加载。我不得不怀疑Visual Editor的作者是否有一些懒惰加载的东西,他觉得它总是需要被使用,或者只是因为某些任意原因而决定他想在工具中使用它。

However, in this case you described, there is no reason to use lazy loading. I have to wonder if the author of Visual Editor didn't just have some thing for lazy loading where he felt it always needed to be used, or just decided that he wanted to use it in the tool for some arbitrary reason.

在构建面板时,组件通常全部被加载,因为它们都是可见的,所以你是完全正确的。在某些情况下,面板的某些部分可能会根据面板上的其他选项出现和消失,并且可以想象在这些情况下可以使用延迟加载。然而,我的观点是,人们可能无论如何都会点击界面并使用所有不同的标签和选项,因此您也可以加载所有内容。

You are exactly right about UI's where components are generally all loaded when the panel is constructed because they're all visible. There are some cases where parts of a panel may appear and disappear based on other choices on the panel, and it's conceivable that you could use lazy loading in these cases. My point of view, however, is that people are likely to click around an interface anyways and use all the different tabs and options, so you might as well load everything to begin with.

显然,当您谈论加载数据时会发生一些不同的事情。如果您在面板出现时隐藏了一个下拉列表,并且在加载时有很多信息,您可能不想加载下拉列表直到它变得可见。我仍然认为没有理由不立即实例化下拉列表,即使它是隐藏的。

Obviously, there's something different going on when you're talking about loading data. If you have a drop-down that's hidden when the panel comes up and has a lot of information if loaded, you may want to not load the drop-down until it becomes visible. I still see no reason not to instantiate the drop-down right away, though, even though it's hidden.

我不认为延迟加载是一个标准行为小组。我无法提供Visual Editor选择以这种方式生成代码的原因。

I would not consider lazy loading to be the norm behavior for a panel at all. I could not offer a reason why Visual Editor chose to generate code in this manner.

这篇关于Swing和延迟加载组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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