简体   繁体   English

在彼此顶部的框架上添加多个面板

[英]Add more than one panel on a frame on top of each other

I'm working on a project where I am displaying a square and a circle. 我正在做一个显示正方形和圆形的项目。 The circle moves on its own, but the user moves the square via the arrow keys. 圆自行移动,但是用户可以通过箭头键移动正方形。 Whenever the circle touches the square, it rebounds. 只要圆圈碰到正方形,它就会反弹。

Square and circle are different classes (2 different panels). 正方形和圆形是不同的类别(2个不同的面板)。 I want to add those two to a frame, one on top of the other such that both are visible. 我想将这两个添加到框架中,一个在另一个之上,以便两者都可见。 Can someone tell me how to do so? 有人可以告诉我该怎么做吗?

JFrame n = new JFrame();
n.setTitle("Background Color for JFrame");
n.setSize(1000,600);
n.setLocationRelativeTo(null);
n.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

n.setResizable(false);

n.add(new Ball());
n.add(new Team());

n.setVisible(true);

User interface components at the same level in a hierarchy are assumed to be non-overlapping by default. 默认情况下,假定层次结构中同一级别的用户界面组件不重叠。 You can explicitly work around this by making your components transparent with setOpaque(false) , assuming you are taking care to only draw whats needed in the components, eg in case of a JPanel ensure that its background is not drawn. 您可以通过使用setOpaque(false)使组件透明来显式地解决此问题,假设您只在绘制组件中需要的内容,例如在使用JPanel的情况下,请确保未绘制其背景。 Its still somewhat random (implementation dependent) which component has precendence over another when doing this. 它在执行此操作时仍然有些随机性(取决于实现)。

There is a component explicitly designed for that: JLayeredPane ( https://docs.oracle.com/javase/tutorial/uiswing/components/layeredpane.html ), which manages "layers" in which components can be put, giving you full control which overlays which. 有一个专门为此设计的组件:JLayeredPane( https://docs.oracle.com/javase/tutorial/uiswing/components/layeredpane.html ),它管理可以放置组件的“层”,从而使您可以完全控制哪些覆盖。

Games often implement this by themselves, since the full features of JComponent are not needed to represent a simple graphical element. 游戏通常自己实现,因为不需要JComponent的全部功能来表示一个简单的图形元素。 In that case a single component is used as a "canvas" to paint self-defined objects onto making use of an override of paintComponent (See: https://docs.oracle.com/javase/tutorial/uiswing/painting/ ) 在那种情况下,单个组件用作“画布”,以使用paintComponent的覆盖来绘制自定义对象(请参阅: https : //docs.oracle.com/javase/tutorial/uiswing/painting/

If you want to do this in swing, as it sounds, I would really recommend making a new class that extends JPanel, and override it's paintComponent method. 听起来,如果您想摇摆不定,我真的建议您创建一个扩展JPanel的新类,并覆盖它的paintComponent方法。 In this method you use the Graphics in the argument to paint to the canvas. 在此方法中,您可以在参数中使用Graphics来绘制画布。 Then you can add this custom panel instead of two separate components to your JFrame, and handle the rendering in there. 然后,您可以将此自定义面板而不是两个单独的组件添加到JFrame中,并在那里处理渲染。 This render panel can then keep track of all objects that needs to be rendered, preferable implementing some interface (Drawable?) with a draw(Graphics g) method. 然后,此渲染面板可以跟踪所有需要渲染的对象,最好使用draw(Graphics g)方法实现某些接口(Drawable?)。 That way you can make your classes that needs to be rendered implement your Drawable interface, keep then as a list in your render panel and just iterate through them in your paintComponent method and call draw on your Drawables. 这样,您可以使需要渲染的类实现Drawable接口,然后将其作为列表保留在渲染面板中,然后在paintComponent方法中对其进行迭代,并在Drawables上调用draw。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM