只能添加 JLabel 背景或 JPanel 背景

Only able to add JLabel background or JPanel background

我正在 Java 制作我的第一款游戏。这也是第一次使用swing。我在将对象正确添加到框架时遇到困难。

一些不相关的代码已从其他 类 中删除。可能缺少引用的几个组件。

主线:

public class Main { 
    Ball ball = new Ball();
    int yBall = 0;

    public static void main(String[] args) throws InterruptedException {
        Main main = new Main();
        main.run();
        while (true) {
            main.ball(100);
        }
    }
    public void run() throws InterruptedException {
        Data data = new Data();
        Frame frame = new Frame(700, 500);
        test.setAngleX();
        frame.add(data, BorderLayout.CENTER);
    }
}

当我在数据(背景)之后添加球时,我的球在框架上可见,与我的数据重叠。如果我在这里切换位置,我的数据将是可见的但不是我的球,球仍然存在于下方。 frame.add(球); frame.setVisible(真);

public void ball(int yBall) throws InterruptedException {
    ball.Ball();
    yBall = ball.SendY();
}

这是我的框架,我知道这里没有问题:

public class Frame extends JFrame {
    private int frameWidth;
    private int frameHeight;
    int yBall = 0;

    public Frame(int frameWidth, int frameHeight) {
        this.frameWidth=frameWidth;
        this.frameHeight = frameHeight;

        setSize(new Dimension(frameWidth, frameHeight));
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }
}

这是我的球。 paintComponent 在最后,在此之前它只是工作逻辑,我猜这将是无关紧要的。

球:

public class Ball extends JLabel{

Graphics g1;

int x = 325, y = 225;
int speed = 1;
int angleX = 0;
int angleY = 0;

public Ball() {
}

public void setAngleX() {

    angleX = ThreadLocalRandom.current().nextInt(-5, 5 + 1);    
    angleY = ThreadLocalRandom.current().nextInt(-1, 1 + 1);    

    if (angleX == 0) {
        setAngleX();
    }

    if (angleY == 0) {
        setAngleX();
    }
}

public void move() {

    if (x + angleX < 0) {
        angleX = speed;
    } else if (x + angleX > getWidth() - 25) {
        angleX = -speed;
    } else if (y + angleY < 0) {
        angleY = speed;
    } else if (y + angleY > getHeight() - 25) {
        angleY = -speed;
    }

    x = x + angleX;
    y = y + angleY;
}

public void Ball() throws InterruptedException {
    move();
    repaint();

    int sleepSpeed = angleX * speed;
    if (sleepSpeed < 0) {
        sleepSpeed = -sleepSpeed;

        Thread.sleep(10*sleepSpeed);
    }
}


@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.setColor(Color.WHITE);
    g.fillOval(x, y, 25, 25);
}
public int SendY() {
    return y;
}

最后是我的数据;

public class Data extends JPanel{
    private static final long serialVersionUID = 1L;
    private Graphics2D g2;
    public Data() {
    }
    @Override
    public void paintComponent(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;
        g2.setColor(Color.BLACK);
        g2.fillRect(0, 0, getSize().width, getSize().height);
        g2.setColor(Color.lightGray);
        g2.fillRect(350, 0, 10, 400);
    }

所以发生的事情是我要么在我的框架上获取我的数据(背景),要么我让我的球在空背景上弹跳,这取决于我最后添加到我的 main 中的哪一个。我猜这是 bcz 的重叠,但无论我搜索多少,我都找不到适用于我的代码的解决方案..

that I either get my data(background) on my frame or I get my Ball bouncing around as it should but on an empty background, depending on which one I add last in my main.

您不能将两个组件都添加到框架中。你需要有 parent/child 关系。

所以逻辑应该是这样的:

background.add( ball );
frame.add( background );

然后画框架,然后画背景,最后画球。

此外,Ball class 将需要实施 getPreferredSize() 方法,以便组件具有合理的尺寸。您应该在绘制组件方法中以偏移量 (0, 0) 绘制 Ball。当您想在背景面板上移动球时,只需对球使用 setLocation(...) 即可将其重新定位在背景上。创建 Ball 时,您还需要将 Ball 的大小设置为首选大小,以便 Ball 具有 size/location 以绘制在背景面板上。背景面板的布局需要设置为空,这样你就可以在面板上自由移动球。

您不应该扩展 JLabel,您没有使用 JLabel 的任何功能。您正在进行自定义绘画,因此只需扩展 JPanel 或 JComponent。阅读有关 Custom Painting 的 Swing 教程部分,了解更多信息和工作示例。

此外,方法名称不应以大写字符开头。 Ball() 不是正确的方法名称。它看起来像 class 名称。该方法应该更具描述性。

或者另一种方法是使用单个组件,而不是使用两个组件。然后该组件的 paintComopnent(...) 方法将同时绘制:

  1. 背景

然后通过这种方式,您将相对于背景面板绘制球,这就是您当前的逻辑。