如何暂停Java Swing计时器?

3

我希望在某个时间点停止 Timer,然后从暂停的位置恢复它。方法 stop()start() 不起作用。

是否有暂停 Timer 然后恢复的函数?我猜不存在。

有没有人知道如何实现适当的 Timer 暂停?


2
当你使用 stop()start() 方法时会发生什么? - orien
1
可能是https://dev59.com/ZE_Ta4cB1Zd3GeqPFPAx的重复问题。 - yati sagade
1
我尝试了@yati提供的链接中提供的示例,它使用了停止/启动,并且在我的计算机上确实可以工作。 - toto2
3个回答

3

javax.swing.Timer.setDelay(int);

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

public class DelayedPaint {

    private CenterPanel center;
    private JFrame frame;
    private JPanel panel;

    public static void main(String args[]) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new DelayedPaint().createAndShowGUI();
            }
        });
    }

    void createAndShowGUI() {
        frame = new JFrame("Delayed Paint");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setSize(400, 400);
        panel = new JPanel(new BorderLayout());
        frame.setContentPane(panel);
        NorthPanel north = new NorthPanel();
        panel.add(north, BorderLayout.NORTH);
        center = new CenterPanel();
        panel.add(center, BorderLayout.CENTER);
        frame.setVisible(true);
        north.startTimer();
    }

    class NorthPanel extends JPanel {

        private JLabel lb;

        public NorthPanel() {
            lb = new JLabel("Good morning");
            add(lb);
        }

        public void startTimer() {
            ActionListener taskPerformer = new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent evt) {
                    setLayout(new FlowLayout(FlowLayout.LEFT));
                    lb.setText("Left");
//    timer.stop(); // Not needed if setRepeats(false).
                    center.startTimer();
                }
            };
            javax.swing.Timer timer = new javax.swing.Timer(2000, taskPerformer);
            timer.setRepeats(false);
            timer.start();
        }
    }

    class CenterPanel extends JPanel {

        private int icnt;
        private Font boldFont = new Font("Dialog", Font.BOLD, 15);
        private Properties centerProps;
        private javax.swing.Timer timer;

        public CenterPanel() {
            centerProps = new Properties();
            centerProps.setProperty("circle", "false");
            centerProps.setProperty("lastString", "0");
        }

        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g;
            if (Boolean.valueOf(centerProps.getProperty("circle"))) {
                Dimension dim = frame.getSize();
                g2.draw(new Arc2D.Double(50, 25, dim.height - 100, dim.width - 100, 0, 360, Arc2D.OPEN));
            }
            int j = Integer.parseInt(centerProps.getProperty("lastString"));
            if (j > 0) {
                g2.setFont(boldFont);
                for (int i = 1; i <= j; i++) {
                    g2.drawString("" + (char) (i + 48), i * 10, 50);
                }
            }
        }

        public void startTimer() {
            ActionListener taskPerformer = new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent evt) {
                    repaint(); // in paintComponent(...) icnt is already icnt+1.
                    icnt++;
                    if (icnt == 1) {
                        centerProps.setProperty("circle", "true");
                        timer.setDelay(500);
                    } else if (icnt <= 10) {
                        centerProps.setProperty("lastString", String.valueOf(icnt - 1));
                        if (icnt == 10) {
                            timer.stop();
                        }
                    }
                }
            };
            timer = new javax.swing.Timer(2000, taskPerformer);
            timer.start();
        }
    }
}

2

我不知道有什么方法可以真正“暂停”Swing计时器,但是有简单的方法来模拟它。我知道的最简单的方法是将计时器中的所有操作都包围在以下代码中:

 if(!paused) {
  // place code here.

  System.out.println("I am not Paused.");
 } else {

  System.out.println("I am Paused.");
 }

一个程序中使用此示例的例子如下:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;



public class ExampleClass implements ActionListener {
 static boolean paused = false;

 // delay in miliseconds.
 static int delay = 5;

 static Timer timer = new Timer(delay, new ExampleClass());

 static JFrame jf = new JFrame();
 static JPanel jp = new JPanel();
 static JButton pause = new JButton("PAUSE");
 static JButton unpause = new JButton("UNPAUSE");

 public static void main(String[] args) {
  System.out.println("Starting Program...");

  pause.addActionListener(new ExampleClass());
  unpause.addActionListener(new ExampleClass());
  jp.add(pause);
  jp.add(unpause);



  jf.setSize(100, 100);
  jf.setTitle("Window");
  jf.setResizable(false);
  jf.setLocationRelativeTo(null);
  jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE);
  jf.add(jp);
  jf.setVisible(true);


  timer.start();

 }

@Override
public void actionPerformed(ActionEvent e) {
 if(!paused) {
     System.out.println("IM NOT PAUSED!!! YAY!!!!");

     if(e.getSource() == pause) {
         paused = true;
     }
 } else {
     System.out.println("urk... can't move... help me...");
     if(e.getSource() == unpause) {
         paused = false;
     }
 }


}
}

好的,就在那里。 而且,你可以使用getter和setter代替暂停和取消暂停按钮。


0

无法暂停计时器,只能停止并重新开始。

您想要实现什么目标?


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接