[英]How to add 2 JComponent to JPanel at the same place
How to add 2 JComponent to JPanel at the same place stretched on full JPanel? 如何在整个JPanel上延伸的同一位置向JPanel添加2个JComponent? I tryed to:
我试图:
add(ship,null);
add(ship,null);
add(ground,null);
加(ground,null);
I'm guessing as to your use case, but is "ground" a background image containing JPanel? 我在猜测您的用例,但是是否将包含JPanel的背景图像“磨碎”了? If so, then add it in such a way so that it fills the container, perhaps via BorderLayout.CENTER with the container using BorderLayout of course.
如果是这样,则以某种方式添加它,以使其充满容器,当然可以使用BorderLayout通过BorderLayout.CENTER与容器一起填充。
The ship, I'm guessing is a sprite that is drawn on the background. 我猜这艘船是在背景上绘制的精灵。 If so, consider having it not be a JPanel but rather a logical object that holds an image Sprite that can be drawn in the ground's paintComponent method.
如果是这样,请考虑使其不是JPanel,而是一个逻辑对象,该对象包含可以在地面的paintComponent方法中绘制的图像Sprite。
As always the devil will be found in the details. 与往常一样,细节中会发现魔鬼。 You first.
你先。
Edit 编辑
You state in comment: 您发表评论:
Stage is my background and it extends JPanel.
舞台是我的背景,它扩展了JPanel。 Ship is a player element (JComponent).
Ship是一个播放器元素(JComponent)。 Ground (JComponent) is something which player can't touch.
地面(JComponent)是玩家无法触摸的东西。 I wanted to have this two elements (ground and ship) like layers where the image is drawn.
我想像绘制图像的图层一样具有这两个元素(地面和船只)。 I need to have one point reference to check collisions.
我需要参考一点来检查碰撞。
I assume that you have a paintComponent(Graphics g)
override method in your Stage object where you draw your background image. 我假设您在绘制背景图像的Stage对象中有一个
paintComponent(Graphics g)
重写方法。
Based on this assumption, I change my previous recommendation in that I feel that neither ground nor ship should extend JPanel since you don't really need for them to do this. 基于此假设,我更改了先前的建议,因为我认为地面和舰船均不应扩展JPanel,因为您实际上不需要他们这样做。 You already have a JPanel on which you can do your drawing and game interaction, so why create more when they'll only clutter things up?
您已经拥有一个JPanel,可以在其上进行绘画和游戏交互,那么当它们只会使事情变得混乱时,为什么还要创建更多呢? The only exception I see is if either ground or ship need to hold other components, and if so, then yes, they should extend JPanel.
我看到的唯一例外是地面或船舶是否需要容纳其他组件,如果是,则可以,它们应该扩展JPanel。
Consider: 考虑:
draw(Graphics g)
method that draws their respective images at the x an y location when called and passed a valid Graphics instance. draw(Graphics g)
方法,该方法在调用并传递有效的Graphics实例时在x和y位置绘制各自的图像。 paintComponent(Graphics g)
method draw both instances by calling their respective draw methods. paintComponent(Graphics g)
方法通过调用它们各自的draw方法来绘制这两个实例。 Edit 2 编辑2
You now ask: 您现在问:
One more question: When should i have keyListener?
还有一个问题:我什么时候应该拥有keyListener?
I recommend that you not use a KeyListener. 我建议您不要使用KeyListener。
KeyListener is a low-level construct, and in general you should prefer to use higher-level constructs such as Key Bindings as they are safer and easier to use without causing side effects or having problems. KeyListener是一个低级构造,通常,您应该首选使用诸如Key Bindings之类的高级构造,因为它们更安全,更易于使用,而不会引起副作用或出现问题。 For instance, it is easy to run into problems with focus issues if you use a KeyListener since it only works if the component listened to has the focus.
例如,如果使用KeyListener,很容易遇到焦点问题,因为它仅在侦听的组件具有焦点时才起作用。 This is easily circumvented if you used key bindings instead.
如果您改用键绑定,则很容易避免。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.