简体   繁体   English

摇摆动画开始停止

[英]swing animation start stop

this is my first post, so forgive me beginner´s mistakes. 这是我的第一篇文章,所以请原谅我的错误。

the structure of my programme is the following: 我的程序的结构如下:

I have a class that basicly creates an array of objects: 我有一个基本上创建对象数组的类:

class fieldCreator extends JPanel
{
    ...
    fieldCell[] fieldArray;
    ...
    public fieldCreator()
    {
        while (counterVar < arraySize)
        {
            // fill the array randomly with one object out of three different classes
            if ((int)(Math.random()) == 0)
                this.fieldArray[counterVar] == new cellType0();
            ...
            counterVar++;
        }
    }
    public moveMethod()
    {
        // rearange the content of the array by a certain algorithm
        ...
        try 
        {
           Thread.sleep(150L); // this is to slow down the loop frequency
        }
        catch (Exception e) {}
    }
    public void paintComponent (Graphics g)
    {
        while (counterVar < arraySize)
        {
            // draw a rectangle for each object in the array in a specific color
            // create the illusion of a 2D field 
            counterVar++;
        }   
    }
}

The main class creates the framework end executes the methods: 主类创建框架,最后执行方法:

class Main extends JPanel 
{
    ...
    public static fieldCreator myField;
    ...
    public static void main (String[] args)
    {

        main myMain = new main();
        myField = new fieldCreator();

        main.framework();

        // !!! this loop is what i want to start/stop by a button bash !!!
        while(true)
        {
            myField.moveMethod();
            myField.repaint();
        } 
    }  
    public void frameWork()
    {
        JFrame myFrame = new JFrame();
        JButton startButton = new JButton ("Start");
        JButton stopButton = new JButton ("Stop");
        startButton.addActionListener(new startListener());
        stopButton.addActionListener(new stopListener());
        myFrame.getContentPane().add(BorderLayout.NORTH, startButton);
        myFrame.getContentPane().add(BorderLayout.CENTER, myField);
        myFrame.getContentPane().add(BorderLayout.SOUTH, stopButton);
        ...
    }

    class startListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {   
            //this does not work!!!
            //while(true)
            //{
            //    myField.moveMethod();
            //    myField.repaint();
            //}
        }
    }

    class stopListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            // ---- this needs to be implemented ----
        }
    }
}

The programme works fine by starting and stopping it by the IDE, the field refreshes on every cycle and is displayed correctly. 通过由IDE启动和停止该程序,该程序可以正常工作,该字段在每个周期都会刷新并正确显示。 But when it comes to implementing the Buttons it does not refresh at all. 但是,当涉及到实现按钮时,它根本不会刷新。

I hope the shortening of the code does not affect the understandability :) I appreciate every help! 希望代码的缩短不会影响其易懂性:)感谢您的帮助!

The reason starting the animation from an ActionListener does not work, is that the loop blocks the event dispatch thread . ActionListener启动动画不起作用的原因是,该循环阻塞了事件分发线程 The reason that the code appears to work when run from main() is that main() is run in another thread. main()运行时代码似乎可以正常工作的原因是main()在另一个线程中运行。

For a simple, timed repeating calls, like you have, the easiest is using a swing Timer . 对于像您这样简单,定时的重复通话,最简单的方法是使用swing Timer

As a side note, components should be also created in the EDT. 附带说明,也应在EDT中创建组件。

I solved the problem as follows: 我解决了如下问题:

class Main extends JPanel 
{
    public static void main(String[] args)
    {
        ...
        timer = new Timer(timerDelay, main.new timerListener());
        ...
    }

    class timerListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            myField.moveMethod();
            myField.repaint();
        }
    }

    class startButtonListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            timer.start();
            startButton.setText("RUNNING...");
        }
    }

    class stopButtonListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            timer.stop();
            startButton.setText("START");
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM