我正在使用Java中的Swing制作Pong。当将JPanels添加到JFrame时,它们会重叠在一起,而我最后放置的是显示的那个。如何获得所有物体(桨和球)都显示出来?
public class Main {
final static int WIDTH = 1000;
final static int HEIGHT = 600;
public static void main(String[] args) {
//things for creating window
JFrame frame = new JFrame("Pong");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(WIDTH, HEIGHT);
frame.setVisible(true);
frame.setResizable(false);
//left paddle
Paddle left = new Paddle(20,(HEIGHT/2)-100);
frame.getContentPane().add(left);
//right paddle
Paddle right = new Paddle(WIDTH-60,(HEIGHT/2)-100);
frame.getContentPane().add(right);
//ball
Ball ball = new Ball();
frame.getContentPane().add(ball);
}
}
每个对象都是其自己的JPanel对象,该类具有扩展JPanel并实现paintComenent()的类。