这个问题在我通过Stackoverflow(Update JLabel every X seconds from ArrayList<List> - Java)获得帮助的另一个问题上展开,该问题使我的标签每X秒更新一次。无论如何...我现在想增加或减少计时器的速度,并使其在文件中循环遍历。

我的打印语句如下所示:(int tM当前设置为300 ...)

private void printWords() {
        final Timer timer = new Timer(tM, null);

        ActionListener listener = new ActionListener() {
            private Iterator<Word> w = words.iterator();
            @Override
            public void actionPerformed(ActionEvent e) {
                if (w.hasNext()) {
                    _textField.setText(w.next().getName());
                    //Prints to Console just Fine...
                    //System.out.println(w.next().getName());
                }
                else {
                    timer.stop();
                }
            }
        };
        timer.addActionListener(listener);
        bPlay.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
              timer.start();
            }
          });
        bPause.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
              timer.stop();
            }
          });

}


我想做的是使用其他两个按钮(更快和更慢)提高或降低速度。

如何更改使用中的计时器间隔?

bFaster.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
              tM = 100;
            }
          });
        bSlower.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
              tM = 1000;
            }
          });


感谢您的任何想法。

你的
JF

最佳答案

您不能执行以下操作吗?时机不是完美的,但可能对用户而言并不明显:

   bFaster.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
          tM = 100;
          timer.stop();
          timer.setDelay( tM );
          timer.start();
        }
      });
    bSlower.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
          tM = 1000;
          timer.stop();
          timer.setDelay( tM );
          timer.start();
        }
          });

10-01 18:49
查看更多