Swing组件可以添加到多个容器中吗?
我正在尝试(测试其他内容)将一个 JButton
引用添加到两个 JPanels
中来测试它,它从添加到的第一个面板中消失!
那么,一个Swing组件就不能添加到多个容器中吗?
先感谢您。
I'm trying (testing something else) to add one JButton
reference into two JPanels
to test it, and it disappears from the first panel it was added to!
So, can't a Swing component be added to multiple containers?
Thank you in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
来自: http://download.oracle.com/javase/tutorial/uiswing /components/toplevel.html:
From: http://download.oracle.com/javase/tutorial/uiswing/components/toplevel.html:
正如您所发现的,您无法共享组件。不过,您还可以使用其他方法。
对于 JButton,您可以共享一个 Action:
阅读 Swing 教程中 如何使用操作了解更多信息。
在其他情况下,您可能希望共享模型:
解决方案取决于您的要求。
As you've discovered, you can't share components. However there are other approaches you can use.
In the case of a JButtons you can share an Action:
Read the section from the Swing tutorial on How to Use Actions for more information.
In other cases you might want to share the model:
The solution depends on your requirement.
解决了。
检查 Java 教程的 UI-Swing 部分,它说。
Solved.
Checking at the UI-Swing section of the Java Tutorial, it says.
我认为这是不可能的。您可以做的是让多个组件共享相同的事件处理程序。所以基本上,在您的情况下,声明两个按钮并使用相同的事件处理程序方法。
I do not think that is possible. What you can do, is have is multiple components sharing the same event handler. SO basically, in your case, declare two buttons and use the same event handler method.