按钮事件到动画

时间:2015-12-21 16:58:06

标签: java swing actionlistener

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

public class MovingCircleGUI
{
    JFrame frame;

    public int x,y;
    public int vx = 30,vy=20;
    public int width = 500,height = 500;
    public int diameter=100;

    CircleDrawPanel drawPanel;
    Color color = Color.magenta.darker();
    JButton button;
    Timer timer2 = new Timer(10, new AnimateCircleListener());

    boolean isRunning = false;

    public static void main (String[] args)
    {
        MovingCircleGUI gui = new MovingCircleGUI();
        gui.go();
    }

    //this method sets up the JFrame and adds the draw panel to the frame
    public void go()
    {
        frame = new JFrame("MovingCircleGUI");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        drawPanel = new CircleDrawPanel();
        frame.getContentPane().add(BorderLayout.CENTER, drawPanel);

        button = new JButton("Click me to start the animation");
        drawPanel.add(button);
        frame.getContentPane().add(BorderLayout.SOUTH , button);
        button.addActionListener(new  AnimateCircleListener());

        frame.setSize(width,height);
        frame.setVisible(true);
    }

    class CircleDrawPanel extends JPanel
    {
        public void paintComponent (Graphics g)
        {
            super.paintComponent(g);
            Graphics2D g2=(Graphics2D)g;
            g2.setColor(color);
            g2.fillOval(x,y,diameter,diameter);
        }
    }

    public void MovingBall()
    {

        x = x + vx;
        y = y + vy;

            if( y >= height)
            {
                y=0;
                boolean xIsSame = true;
                int randomX = 0;
                do
                {
                    randomX = Math.round((float)Math.random()*width);

                    if (randomX != x)
                    {
                        x = randomX;
                    }

                }
                while(!xIsSame);
            }

            if(x <= 0)
            {
                x = width+x;
            }

            if (x >= width)
            {
                x = x-width;
            }

            timer2.start();
            frame.repaint();

    }

    class AnimateCircleListener implements ActionListener
    {
        @Override
        public void actionPerformed(ActionEvent e) 
        {
            if(e.getSource()== button)
            {
                if(isRunning)
                {
                    isRunning = false;
                    button.setText("Click me to start the animation");
                    button.revalidate();
                }
                 else
                {
                    isRunning = true;
                    MovingBall();
                    button.setText("Click me to stop the animation");
                    button.revalidate();
                }
            }
        }

    }

    public int getX() 
    {
            return x;
    }

    public void setX(int x) 
    {
            this.x = x;
    }

    public int getY() 
    {
            return y;
    }

    public void setY(int y) 
    {
            this.y = y;
    }
}

我正在尝试构建一个处理两个事件的按钮,主要用于启动和停止动画。我要做的是当用户点击按钮时,弹跳球的动画将开始,按钮的文本将变为&#34;点击我停止&#34;,当用户点击按钮时再次,动画将停止。我正在使用计时器。

动画没问题,我把它解决了,当用户点击按钮时,动画开始也没问题。我唯一遇到的问题是我如何处理同一个按钮的另一个事件?

2 个答案:

答案 0 :(得分:3)

你可以做类似的事情。这样,您只需要一个Action Listener和一个boolean来告诉它要执行的操作。

boolean isRunning = false

button.addActionListener(new YourActionListener());

public class YourActionListener implements ActionListener
{
    @Override
    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource()==button)
        {
            if(isRunning)
            {
                isRunning = false;
                button.setText("Click me to start");
                button.revalidate();
            }
            else
            {
                isRunning = true;
                button.setText("Click me to stop");
                button.revalidate();
            }
        }
    }
}

修改

您的代码现在有效。您想要做的是在MoveBall解雇timer2时调用Action Listener方法。

这可以通过

完成
if(e.getSource()==timer2)
{
    MovingBall();
}

所以在你的代码中它看起来像。

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

public class MovingCircleGUI
{
    JFrame frame;

    public int x,y;
    public int vx = 10,vy=5;
    public int width = 500,height = 500;
    public int diameter=50;

    CircleDrawPanel drawPanel;
    Color color = Color.magenta.darker();
    JButton button;
    Timer timer2 = new Timer(25, new AnimateCircleListener());

    boolean isRunning = false;

    public static void main (String[] args)
    {
        MovingCircleGUI gui = new MovingCircleGUI();
        gui.go();
    }

    //this method sets up the JFrame and adds the draw panel to the frame
    public void go()
    {
        frame = new JFrame("MovingCircleGUI");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        drawPanel = new CircleDrawPanel();
        frame.getContentPane().add(BorderLayout.CENTER, drawPanel);

        button = new JButton("Click me to start the animation");
        drawPanel.add(button);
        frame.getContentPane().add(BorderLayout.SOUTH , button);
        button.addActionListener(new  AnimateCircleListener());

        frame.setSize(width,height);
        frame.setVisible(true);
    }

    class CircleDrawPanel extends JPanel
    {
        public void paintComponent (Graphics g)
        {
            super.paintComponent(g);
            Graphics2D g2=(Graphics2D)g;
            g2.setColor(color);
            g2.fillOval(x,y,diameter,diameter);
        }
    }

    public void MovingBall()
    {
        x = x + vx;
        y = y + vy;

        if( y >= height)
        {
            y=0;
            boolean xIsSame = true;
            int randomX = 0;
            do
            {
                randomX = Math.round((float)Math.random()*width);

                if (randomX != x)
                {
                    x = randomX;
                }

            }
            while(!xIsSame);
        }

        if(x <= 0)
        {
            x = width+x;
        }

        if (x >= width)
        {
            x = x-width;
        }

        frame.repaint();
    }

    class AnimateCircleListener implements ActionListener
    {
        @Override
        public void actionPerformed(ActionEvent e) 
        {
            if(e.getSource()== button)
            {
                if(timer2.isRunning())
                {
                    button.setText("Click me to start the animation");
                    button.revalidate();
                    timer2.stop();
                }
                else
                {
                    button.setText("Click me to stop the animation");
                    button.revalidate();
                    timer2.start();
                }
            }

            if(e.getSource()==timer2)
            {
                MovingBall();
            }
        }
    }

    public int getX() 
    {
        return x;
    }

    public void setX(int x) 
    {
        this.x = x;
    }

    public int getY() 
    {
        return y;
    }

    public void setY(int y) 
    {
        this.y = y;
    }
}

答案 1 :(得分:0)

您可以使用there was one(myMock).foo(List(<some_data_here>).iterator) 删除ActionListener上的上一个JButton,然后使用button.removeActionListener(<your first action listener>)添加第二个动作侦听器。然后,要更改按钮上的文字,只需使用button.addActionListener(<your second action listener>)