Java操作侦听器问题 [英] Java Action Listener Issues

查看:81
本文介绍了Java操作侦听器问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序,可以在Jpanel中为球设置动画。我有两个按钮,即停止和继续。 Stop停止球移动,go是指球移动。在我的球类中,我有一个布尔变量,如果为true,则球会移动;如果为false,则球不会移动。因此,我想到在主类中创建框架并将球类放置在面板中时,可以根据按钮的使用情况使用按钮将变量更改为false或true。

I have a program that animates a ball in a Jpanel. I have two buttons Stop and Go. Stop stops the ball moving and go is meant for the ball to move around. In my ball class I have a boolean variable that if it is true the ball moves and if it is false the ball doesn't move. So I thought in my main class when I create the frame and put the ball class in the panel I could use the buttons to change the variable to false or true depending on the button press.

public class BallTask extends JPanel implements ActionListener{


public static boolean run = false;

public BallTask(){
    this.setPreferredSize(new Dimension(width, height));
    Thread gameThread = new Thread() {
        public void run() {
            while (run) {

             .... working code


public static void main(String[] args) {

    SwingUtilities.invokeLater(new Runnable() {
        public void run() {

            JFrame window = new JFrame();
            window.setLayout(new BorderLayout());
            window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            window.add(new BallTask());
            JPanel buttons = new JPanel();
            JButton stop = new JButton("STOP");
            stop.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    run = false;
                }
            });
            JButton go = new JButton("GO");
            go.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    run = true;
                }
            });
            buttons.add(go);
            buttons.add(stop);
            window.add(buttons,BorderLayout.SOUTH);
            window.pack();
            window.setVisible(true);
        }
    });

我的代码存在的问题是按钮实际上并没有更改布尔值BallTask​​类。有想法吗?

The problem I have with the code is that the buttons don't actually change the boolean value in the BallTask class. Any ideas?

推荐答案

使用 javax.swing.Timer 。您的while循环将阻止EDT,从而不允许调度按钮事件。

Look into using a javax.swing.Timer. Your while loop will block the EDT not allowing the button events to be dispatched.

请参见如何使用Swing计时器

有了计时器,您就可以使用它了方法 stop start 可以启动和停止计时器。您可以在此处

With the timer, you can just use it's methods stop and start to, well, start and stop the timer. You can see an example here

这是一个使用 Timer 和代码的可运行示例。上面链接中的代码使用 Ball 对象更加简洁。我只是很快就把它扔了

Here's a runnable example using a Timer with your code. The code in the link above is cleaner, using a Ball object. I just threw this one together really quick

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class BallTask {

    public static boolean run = false;
    private Timer timer;
    private BallPanel ballPanel = new BallPanel();

    public BallTask() {
        timer = new Timer(30, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (ballPanel.x < 0 || ballPanel.x > ballPanel.getWidth()) {
                    ballPanel.dx = -ballPanel.dx;
                }
                if (ballPanel.y < 0 || ballPanel.y > ballPanel.getHeight()) {
                    ballPanel.dy = -ballPanel.dy;
                }
                // Adjust ball position
                ballPanel.x += ballPanel.dx;
                ballPanel.y += ballPanel.dy;
                ballPanel.repaint();
            }
        });
        JPanel buttons = new JPanel();
        JButton stop = new JButton("STOP");
        buttons.add(stop);
        stop.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                timer.stop();
            }
        });
        JButton go = new JButton("GO");
        buttons.add(go);
        go.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                timer.start();
            }
        });

        JPanel mainPanel = new JPanel(new BorderLayout());
        mainPanel.add(ballPanel);
        mainPanel.add(buttons, BorderLayout.SOUTH);

        JFrame window = new JFrame();
        window.add(mainPanel);
        window.pack();
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setLocationRelativeTo(null);
        window.setVisible(true);

    }

    private class BallPanel extends JPanel {

        private int x;
        private int y;
        int dx = 4; // Increment on ball's x-coordinate
        int dy = 4; // Increment on ball's y-coordinate
        int radius = 15; // Ball radius

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.fillOval(x, y, 30, 30);
        }

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

    public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new BallTask();
            }
        });
    }
}

这篇关于Java操作侦听器问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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