我怎样才能让我的按钮工作?java

我知道有很多关于动作监听器等的问题。但是,没有一个可以帮助我解决我的具体问题......无论我尝试哪种方式,我总是会遇到错误。这是我的简单弹跳球程序:


public class ControlledBall extends JPanel implements Runnable {


    int diameter;

    long delay;

    private int x;

    private int y;

    private int vx;

    private int vy;


    public ControlledBall(int xvelocity, int yvelocity) {



        diameter = 30;

        delay = 40;

        x = 1;

        y = 1;

        vx = xvelocity;

        vy = yvelocity;

    }


    protected void paintComponent(Graphics g) {

        super.paintComponent(g);

        Graphics2D g2 = (Graphics2D)g;

        g.setColor(Color.blue);

        g.fillOval(x,y,30,30);

        g.setColor(Color.black);

        g2.drawOval(x,y,30,30); //draw

    }


    public void run() {

        while(isVisible()) {

            try {

                Thread.sleep(delay);

            } catch(InterruptedException e) {

                System.out.println("Something Went Wrong!");

            }

            move();

            repaint();

        }

    }


    public void move() {

        if(x + vx < 0 || x + diameter + vx > getWidth()) {

            vx *= -1;

        }

        if(y + vy < 0 || y + diameter + vy > getHeight()) {

            vy *= -1;

        }

        x += vx;

        y += vy;


    }


    public void stop(){

        x=0;

        y=0;

    }


    public class Action implements ActionListener{

        public void actionPerformed(ActionEvent e){

            stop();

        }

    }


正如您在评论中看到的那样,我尝试了几种不同的技术,但都没有奏效。任何建议将不胜感激。谢谢


我得到的主要错误是:


non static field cannot be referenced from a static context

我认为那是因为我是从 main 方法运行它的。


FFIVE
浏览 138回答 1
1回答

Smart猫小萌

您真正想做的第一件事是摆脱您的run方法(以及您对 的依赖Runnable)。这确实是在 Swing 中执行定期更新的一种不合适的方式。Swing 是单线程的,不是线程安全的,您当前的方法有跨线程边界脏读/写的风险。相反,您想要使用 Swing Timer,请参阅如何使用 Swing 计时器了解更多详细信息。接下来要做的是添加一个start方法并更新该stop方法以支持使用 Swing Timer...public class ControlledBall extends JPanel {&nbsp; &nbsp; //...&nbsp; &nbsp; private Timer timer;&nbsp; &nbsp; public void stop() {&nbsp; &nbsp; &nbsp; &nbsp; if (timer == null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; timer.stop();&nbsp; &nbsp; &nbsp; &nbsp; timer = null;&nbsp; &nbsp; &nbsp; &nbsp; x = 0;&nbsp; &nbsp; &nbsp; &nbsp; y = 0;&nbsp; &nbsp; }&nbsp; &nbsp; public void start() {&nbsp; &nbsp; &nbsp; &nbsp; if (timer != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; timer = new Timer(delay, new ActionListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void actionPerformed(ActionEvent e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; move();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; repaint();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; timer.start();&nbsp; &nbsp; }然后,您只需更新开始和停止按钮即可调用这些方法...&nbsp; &nbsp; stop.addActionListener(new ActionListener() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void actionPerformed(ActionEvent e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ball2.stop();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });&nbsp; &nbsp; //...&nbsp; &nbsp; start.addActionListener(new ActionListener() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void actionPerformed(ActionEvent e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ball2.start();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java