CardLayout与手动添加/删除JPanels有什么特别之处? [英] What's so special about CardLayout vs manual adding/removal of JPanels?

查看:104
本文介绍了CardLayout与手动添加/删除JPanels有什么特别之处?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

StackOverflow上有很多次用户提出这样的问题...

There have been many times on StackOverflow where a user asks a question like this...


我有一个主 JPanel 包含一个孩子 JPanel 。当用户单击
按钮时,子 JPanel 应更改为其他 JPanel 。我怎么能实现这个

I have a main JPanel that contains a child JPanel. When the user clicks a button, the child JPanel should change to a different JPanel. How can I achieve this.

用户实际上经常尝试实现这个问题,但是让它运转起来。

More often than not, the user has actually tried to implement this problem, but can't get it working.

每当我回答这个问题时,我都会告诉他们做这样的事情(简单地说)......

Whenever I answer this question, I tell them to do something like this (put simply)...

JPanel myFrame = new JPanel();
myFrame.remove(oldPanel);
myFrame.add(newPanel);

我认为这是一个非常合理的答案,我个人在我自己的Java中使用过这个项目没有问题。但是,我总是得到我的回答,每个人只是说使用 CardLayout

I see this as quite a legitimate answer, and I personally have used this in many of my own Java projects without problem. However, I always get downvotes for my answer, and everyone just says "Use a CardLayout".

所以我的问题是,为什么每个人都对 CardLayout 如此着迷,以至于我的答案值得一试?我为什么要选择使用 CardLayout 而不是使用上面的代码添加/删除面板?

So my question is, why is everyone so fascinated with CardLayout, to the point where my answer deserves a downvote? Why should I choose to use a CardLayout rather than adding/removing panels using my code above?

进一步问题,对于具有动态JPanel的接口,您是否仍在建议 CardLayout 。例如,我的大多数程序都实现了一个自定义插件框架,其中可能有数百个 JPanels ,但我只是加载并显示实际需要的面板。对于程序的正常使用,大多数面板实际上永远不会被加载或需要。对于这种情况,我的编码方法是否是最好的解决方案,因为据我所知, CardLayout 会要求我实际创建所有 JPanels 即使大多数永远不会被使用?

As a further question, would you still be suggesting CardLayout for interfaces that have dynamic JPanels. For example, most of my programs implement a custom plugin framework where there could be many hundreds of JPanels, but I only load and display the panels as they are actually required. For the normal use of the program, most of the panels would never actually be loaded or required. For this type of scenario, would my coding approach be the best solution, as I understand that CardLayout would require me to actually create all of the JPanels even though most will never be used?

推荐答案


  • 使用CardLayout,它是更容易松散耦合(虽然不是不可能自己滚动)

  • 使用CardLayout,持卡人的preferredSize是它持有的最大卡。

  • CardLayout更难上手,并允许几乎无足轻重的连续组件交换其 next() prev()方法。

  • 您可以轻松地将所需组件与常量相关联 - 无需创建 Map< String,Component> 为此目的,因为它已经在你身边。我不经常使用枚举。

  • 无需记得调用 repaint()交换组件时重新验证()

  • 它的构建和允许轻松重用组件。

    • With CardLayout, it's easier to have loose coupling (though not impossible with roll your own)
    • With CardLayout, the preferredSize of the card-holder is that of the largest card it holds.
    • CardLayout is harder to fark-up, and allows almost trivial contiguous component swapping its next() and prev() methods.
    • You can easily associate the desired component with a constant -- no need to have to create a Map<String, Component> for this purpose as it's already there for you. I've not infrequently used enums for this.
    • No need to remember to call repaint() and revalidate() when swapping components.
    • It's built for and allows for easy re-use of components.
    • 我无法解释投票失败的原因,除非他们感到不安,否则你没有提到需要记得打电话给重绘()交换组件时, revalidate()。如果他们有足够的勇气回应,你将不得不问下选民。

      I can't explain the reason for a down-vote though, unless they're upset you didn't mention the need to remember to call repaint() and revalidate() when swapping components. You'll have to ask the down-voter if they are brave enough to respond.

      这篇关于CardLayout与手动添加/删除JPanels有什么特别之处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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