admin管理员组文章数量:1389783
I have got a Java Swing application (vocabulary trainer) and want to embed its main JPanel into a Java AWT application. Can this be done at all?
I googled for hours but could not find anything about the issue.
I have got a Java Swing application (vocabulary trainer) and want to embed its main JPanel into a Java AWT application. Can this be done at all?
I googled for hours but could not find anything about the issue.
Share Improve this question asked Mar 17 at 0:21 dragonessdragoness 531 silver badge14 bronze badges 1- Hello dragoness, the short answer is yes, to be more specific, it is not recommended, going into detail, why would you want to use AWT if you have Swing? – Marce Puente Commented Mar 17 at 0:34
1 Answer
Reset to default 3So yes. java.awt.Panel
extends from java.awt.Component
. javax.swing.JPanel
extends from java.awt.Container
which extends from java.awt.Component
. Therefore, the java.awt.Panel.add(Component,Object)
method accepts anything that extends java.awt.Component
. Since javax.swing.JPanel
extends java.awt.Component
through several levels of other base classes (i.e. JPanel -> JComponent -> Container -> Component) it can be passed to the panel. For example:
Panel awtPanel = new Panel(new BordreLayout());
JPanel topPanel = new VocabularyTrainerPanel();
awtPanel.add( topPanel, BorderLayout.CENTER );
Now just because you can do that doesn't mean you should. AWT is beyond ancient. Swing at this point is pretty old too, but AWT has been left for dead. I can only say I wouldn't do this.
本文标签: Is there a way to embed a JPanel (Java Swing) into a Panel (Java AWT)Stack Overflow
版权声明:本文标题:Is there a way to embed a JPanel (Java Swing) into a Panel (Java AWT)? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744583128a2614042.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论