如何在另一个面板中显示一个面板?
我正在用 Java 编写一个简单的图形程序。
所以,我有3节课。
第一个类是 GUI w/c 扩展 JFrame,它加载菜单栏和面板(绘图类)
第二个类是绘图类,它扩展了 JPanel,并且具有简单的 Graphics 命令。
第三个类是动画类,它显示动画。它还扩展了JPanel。
所以我的问题是,如何在 GUI 类的面板中显示动画类。我希望显示它而不是绘图类。当我尝试将其放置在那里时,它会在绘图类面板旁边显示一个小框。我不太擅长框架和面板。预先非常感谢您!
I'm coding a simple graphics program in Java.
So, I have 3 classes.
The first class is the GUI w/c extends JFrame, it load the menu bar and panel (drawing class)
The second class is the drawing class, it extends JPanel, and it has simple Graphics commands.
The third class is an animation class, it displays an animation. It also extends JPanel.
So my question is this, how do I display the animation class in the panel of the GUI class. I want it to be displayed instead of drawing class. When I try to place it there, it displays a tiny box beside the drawing class panel. I am not very good at frames and panels. Thank you very much in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
所有 JPanel 都有一个 LayoutManager,它可以相当方便地管理组件的布局方式。默认布局是 FlowLayout,默认模式会将每个组件简单地放置在最后一个组件的右侧。
如果你想将布局更改为更有用的东西,有很多选择; BorderLayout、GridLayout、GridBag 布局是流行的布局。我自己使用 MigLayout,这是一个非常强大的外部库:)。
至于它看起来很小,请尝试使用 setSize(w, h) 或 setPreferredSize(w, h) 手动强制执行大小。
All JPanels have a LayoutManager which, rather handily, manages how Components are laid out. The default layout is FlowLayout, which default mode will simply place each component to the right of the last component.
If you want to change the layout to something more useful, there are many options; BorderLayout, GridLayout, GridBag layout are popular ones. Myself, I use MigLayout, an external library which is very powerful :).
As for it appearing small, try manually enforcing the size with setSize(w, h) or setPreferredSize(w, h).
您可能想要编写
但是,如果您将其他组件添加到
GUIFrame
中,则行为可能会有所不同。由于您尚未发布 SSCCE,因此很难准确地为您提供帮助。You might want to write
However the behavior may vary if you have other components added to your
GUIFrame
. It is difficult to help you exactly because you have not posted an SSCCE.使用卡片布局。
Use a Card Layout.