2

アニメーション用に JPanel を拡張するクラスで使用される Timer があり、ActionListener がそれをリッスンして actionPerformed を実行し、再描画を行い、必要に応じてタイマーを停止します。しかし、タイマーを開始するメソッド animatePanel は、タイマーの実行中も実行を続けます。これは私が望んでいないことです。タイマーが止まるまで待って戻りたい。

Timer は、クラスのコンストラクターで次のように初期化されます。

timer = new Timer(5, taskPerformer);

そして、これがそれがすることです。animatePanel() と呼ばれるものがあります。

    private ActionListener taskPerformer = new ActionListener() {
    public void actionPerformed(ActionEvent evt) {
        ...
        if (some conditions){
            ...
            timer.stop();
            ...
            return;
        }

        ...
    }
};

private void animatePanel() {
    ...
    timer.start();
    System.out.println("Timer stopped."); //always executes before the timer has stopped :(
    //then returns and lets the rest of my program run while the timer is still going, which is BAD
}

タイマーは正常に動作しますが、animatePanel() の戻りが早すぎて残りのプログラムが実行され、問題が発生する場合があります。

4

1 に答える 1

2

イベントディスパッチスレッドのコンテキスト内からこれを行うことはできません。これを行うと、アプリケーションがハングします!

タイマーは別の で開始する必要がありますThread。これにより、スレッド監視 API を利用できるようになります。

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class WaitForTimer {

    public static void main(String[] args) {
        new WaitForTimer();
    }

    public WaitForTimer() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public static class TestPane extends JPanel {

        protected static final Object WAIT_FOR = new Object();
        private Timer timer;
        private int tickCount = 0;
        private JLabel ticks;
        private JButton start;

        public TestPane() {
            timer = new Timer(250, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    tickCount++;
                    if (tickCount > 10) {
                        tickCount = 0;
                        timer.stop();
                        synchronized (WAIT_FOR) {
                            WAIT_FOR.notifyAll();
                        }
                        start.setEnabled(true);
                    }
                    ticks.setText(String.valueOf(tickCount));
                }
            });
            timer.setRepeats(true);
            timer.setCoalesce(true);

            ticks = new JLabel("...");
            start = new JButton("Start");

            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            add(ticks, gbc);
            add(start, gbc);

            start.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    start.setEnabled(false);
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            System.out.println("Starting timer...");
                            timer.start();
                            synchronized (WAIT_FOR) {
                                try {
                                    WAIT_FOR.wait();
                                } catch (InterruptedException ex) {
                                }
                            }
                            System.out.println("Timer finished...");
                        }
                    }).start();
                }
            });

        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }
    }
}
于 2013-04-22T02:05:09.927 に答える