我正在尝试为2D游戏制作基本布局,每个人都说将对象添加到JPanel然后将面板添加到JFrame但是仍然只有一个对象一次显示在这里是我的代码
我有一个矩形点和球类,但他们都在工作的问题是JPanel只显示添加的第一个对象
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.Timer;
public class test{
public static void main (String[] args) throws InterruptedException {
JFrame frame = new JFrame("PONG.s'aight");
JPanel panel = new JPanel(new FlowLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(0,0,800,500);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
Ball a = new Ball (100,100,5,5);
Ball b = new Ball(200,200,15,15);
panel.add(a);
panel.add(b);
frame.getContentPane().add(panel, BorderLayout.CENTER);
/*if(b.isCollide(paddle)) b.reverse(Direction.x);*/
while(true){
if(b.x==770)b.reverse(Direction.x);
if(b.x==0)b.reverse(Direction.x);
if(b.y==450)b.reverse(Direction.y);
if(b.y==0)b.reverse(Direction.y);
if(b.x==770)a.reverse(Direction.x);
if(b.x==0)a.reverse(Direction.x);
if(b.y==450)a.reverse(Direction.y);
if(b.y==0)a.reverse(Direction.y);
b.moveGen();
a.moveGen();
frame.repaint();
Thread.sleep(2);
}
}
}