首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >我怎样才能让我的按钮工作呢?java

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

Stack Overflow用户
提问于 2018-08-27 05:27:43
回答 1查看 42关注 0票数 -3

我知道有很多关于动作监听器等的问题,但是,没有人能帮助我解决我的特定issue...no问题,不管我用哪种方式去做,我总是得到一个错误。下面是我的简单弹跳球程序:

代码语言:javascript
复制
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();
        }
    }



    public static void main(String[] args) {
        ControlledBall ball2 = new ControlledBall(12,2);
        JFrame window = new JFrame("Controlled Ball");
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        window.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        JButton stop = new JButton("Stop");
        stop.setSize(4,400);
        stop.setVisible(true);
        stop.setText("Stop");
 //       stop.addActionListener(new Action());

        stop.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

           }
        });

        window.add(stop);
        JButton start = new JButton("Start");
        start.setSize(100,100);
        start.setVisible(true);
        start.setText("Start");
        window.add(start);
        start.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

            }
        });


        c.ipadx = 400;
        c.ipady = 400;
        c.gridx = 10;
        c.gridy = 10;
        window.add(ball2,c);
       c.ipadx = 50;
      c.ipady = 20;
       c.gridx = 10;
       c.gridy = 10;

        window.pack();
        window.setSize(600,600);
        window.setLocation(250,200);
        window.setVisible(true);


        new Thread(ball2).start();
    }
}

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

我得到的主要错误是:

代码语言:javascript
复制
non static field cannot be referenced from a static context

我假设这是因为我是从main方法运行的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-27 06:10:43

你真正想做的第一件事是摆脱你的run方法(以及你对Runnable的依赖)。这确实不适合在Swing中执行定期更新。

Swing是单线程的,并且不是线程安全的,您当前的方法冒着跨线程边界进行脏读/写的风险。

相反,您希望使用Swing Timer,有关更多详细信息,请参阅How to Use Swing Timers

您想要做的下一件事是添加start方法并更新stop方法以支持Swing Timer的使用……

代码语言:javascript
复制
public class ControlledBall extends JPanel {

    //...

    private Timer timer;

    public void stop() {
        if (timer == null) {
            return;
        }
        timer.stop();
        timer = null;
        x = 0;
        y = 0;
    }

    public void start() {
        if (timer != null) {
            return;
        }
        timer = new Timer(delay, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                move();
                repaint();
            }
        });
        timer.start();
    }

然后,只需更新启动和停止按钮,即可调用这些方法……

代码语言:javascript
复制
    stop.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            ball2.stop();
        }
    });
    //...
    start.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            ball2.start();
        }
    });
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52030392

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档