Java Swing - CardLayout,如何“更新”跑步时卡?
我在 . 方面还很新。 Java Swing,并且正在为我的应用程序使用CardLayout。在此应用程序中,用户可以创建活动,并在其他 JPanel 中查看现有活动。
我在更新卡片(标签所在位置)时遇到问题。如果我添加活动,当您重新启动应用程序时,它可以正常工作。我正在寻找一种不需要重新启动的方法。
简而言之,我想知道如何在运行时最好地重建/更新面板,以便显示新的标签数据。
提前致谢!
I am pretty new at . Java Swing, and am using the CardLayout for my application. In this app a user can create activities and, in an other JPanel, view the existing ones.
I am having trouble updating the card (where the label is in). If I add the activity, when you restart the app it works fine. I am looking for a way that doesn't require restarting.
So in short, I would like to know how I should best reconstruct/update a panel whilst running so that the new label data is shown.
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您将组件添加到可见 GUI 时,您需要告诉 GUI 已添加组件,以便可以调用布局管理器:
When you add a component to a visible GUI you need to tell the GUI that a component has been added so the layout manager can be invoked:
听起来您正在寻找的是观察者设计模式。
基本上,它的全部内容是您有一个观察者实例,您可以将所有面板添加到其中。当您更新面板中的值时,您会向观察者通知某些更改。然后,观察者将通知所有观察到的面板值已更改,并且他们将更新其值。
http://en.wikipedia.org/wiki/Observer_pattern
It sounds like what you are looking for is the Observer design pattern.
Basicly what it's all about is you have one Observer instance, which you add all your panels to. When you update a value in a panel, you nofity the Observer something's changed. The Observer will then inform all his observed panels that a value has changed, and they'll update their value.
http://en.wikipedia.org/wiki/Observer_pattern