【问题标题】:how can i make my buttons work? java [duplicate]我怎样才能让我的按钮工作? java [重复]
【发布时间】:2018-08-26 21:27:43
【问题描述】:

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

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();
    }
}

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

我得到的主要错误是:

non static field cannot be referenced from a static context

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

【问题讨论】:

  • 什么错误?这很重要。将其发布在格式为代码的问题上。
  • 这个错误出现在哪一行?
  • 从第 87 行开始,我尝试添加开始和停止按钮
  • 首先让您的 run 方法使用 Swing Timer
  • 在这种情况下我将如何实施和使用摆动计时器来提供帮助?

标签: java swing jbutton actionlistener


【解决方案1】:

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

Swing 是单线程的,不是线程安全的,您当前的方法存在跨线程边界的脏读/写风险。

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

接下来要做的是添加start 方法并更新stop 方法以支持使用Swing Timer...

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();
    }

然后,您的开始和停止按钮只需要更新以调用这些方法...

    stop.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            ball2.stop();
        }
    });
    //...
    start.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            ball2.start();
        }
    });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-22
    • 2011-04-26
    • 1970-01-01
    • 2015-07-29
    • 1970-01-01
    相关资源
    最近更新 更多