如果我在for循环后添加球,为什么球不出现在框架中? [英] Why doesn't the ball show up in the frame if I add the ball after the for loop?

查看:125
本文介绍了如果我在for循环后添加球,为什么球不出现在框架中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该程序让球从左上角到右下角滑动并起作用。但如果我要换行

The program makes a ball glide across from top left to bottom right and works. But if I were to shift the line

frame.getContentPane().add(ball);

从当前位置到for循环之后,为什么球不出现在框架上。
我同意球不应再移动,因为在我将球添加到JFrame之前,for循环中完成的所有移动都会发生,但我不明白为什么球没有出现在屏幕,当我最终将其添加到框架。
这是工作程序的代码,如果你将上面提到的行换到for循环之后,球就不再显示了

from its current position to after the for loop, why doesn't the ball show up on the frame. I agree that the ball should no longer move, because all the shifting done in the for loop happens even before I add the ball to the JFrame,but I don't understand why the ball doesn't show up on the screen when I ultimately add it to the frame. Here's the code of the working program, if you shift the line mentioned above to after the for loop, the ball no longer shows up

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class Animate 
{
    private JFrame frame;
    private int x,y;
    public static void main(String args[])
    {
        Animate ballRoll = new Animate();
        ballRoll.go();
    }

    public void go()
    {
        frame = new JFrame();
        frame.setSize(500,500);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        MyRoll ball = new MyRoll();
        frame.getContentPane().add(ball);
        for(x = 5;x<=350;x++)
        {
            y=x;

            try
            {
                Thread.sleep(50);
            }
            catch(Exception e)
            {
                System.out.println("dsfsd");
            }
            ball.repaint();
        }


    }

    class MyRoll extends JPanel
    {
        public void paintComponent(Graphics g)
        {
            g.setColor(Color.BLACK);
            g.fillRect(0, 0, this.getWidth(), this.getHeight());
            g.setColor(Color.ORANGE);
            g.fillOval(x, y, 100, 100);
        }
    }
}


推荐答案

要记住一些要点:



Some points to remember:


  1. 不要使用 Thread.sleep()有时挂起整个swing应用程序,而不是尝试使用摇摆计时器,最适合摇摆应用。

  1. Don't use Thread.sleep() that sometime hangs the whole swing application instead try with Swing Timer that is most suitable for swing application.

阅读更多如何使用摇摆计时器

不要忘记在重写的 paintComponent()方法中调用 super.paintComponent()

Don't forget to call super.paintComponent() in overridden paintComponent() method.

在添加所有组件后,最后调用 frame.setVisible(true)

Call frame.setVisible(true) in the end after adding all the components.

使用 frame.pack()而不是适合组件的 frame.setSize(500,500)每个组件的首选大小。

Use frame.pack() instead of frame.setSize(500,500) that fits the components as per component's preferred size.

在自定义绘画的情况下,覆盖 getPreferredSize()设置 JPanel 的首选大小。

Override getPreferredSize() to set the preferred size of the JPanel in case of custom painting.

使用 SwingUtilities.invokeLater() EventQueue.invokeLater()以确保 EDT 已正确初始化。

Use SwingUtilities.invokeLater() or EventQueue.invokeLater() to make sure that EDT is initialized properly.

了解更多

SwingUtilities类。 invokeLater

我们是否应该在Java桌面应用程序中使用EventQueue.invokeLater进行任何GUI更新?






示例代码:根据您的自定义绘画更改

private Timer timer;
...

timer = new javax.swing.Timer(50, new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent arg0) {
        y = ++x;
        ball.repaint();

        if (x > 350) {
            timer.stop();
        }
    }
});
timer.setRepeats(true);
timer.start();

public static void main(String args[]) {
    SwingUtilities.invokeLater(new Runnable() {

        @Override
        public void run() {
            Animate ballRoll = new Animate();
            ballRoll.go();
        }
    });
}

class MyRoll extends JPanel {
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        ...
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(..., ...);
    }
}

这篇关于如果我在for循环后添加球,为什么球不出现在框架中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆