【问题标题】:Why does this variable keep getting reset every time I activate this timer?为什么每次激活此计时器时此变量都会不断重置?
【发布时间】:2014-10-13 22:08:29
【问题描述】:

我有两个计时器。按下按钮 1 时它们都应该被激活,按下按钮 3 时它们都应该停止。如果激活,则每隔一秒,定时器将值 1 添加到它们各自的计数器,即 counter 和 counter1。

如果按下停止按钮,则只有第一个计时器的计数器应重置为零,而第二个计时器的计数器应保持不变。

所以如果我按开始,10 秒后按停止,那么第一个计数器的值应该再次为 0,而第二个计数器的值应该为 10。

如果我在此之后再次按开始,10 秒后再次按停止,第一个计数器的值应该再次为 0,而第二个计数器的值现在应该为 20。

我遇到的问题是,无论我做什么,如果我按下停止,第二个计数器的值就会一直重置,所以每次按下停止它都会再次变为 0,而不是保存与停止按钮之前相同的值被按下(所以在示例中应该是 20)。

这是代码(我留下了一些不重要的东西,如果需要可以上传完整的代码):

public class ColoredWordsExperiment {
    Timer timer;
    Timer timer1;
    TimerAction timeraction;
    TimerAction1 timeraction1;
    int counter;
    int counter1;

    ColoredWordsExperiment() {
        button1 = new JButton("Matching");
        button3 = new JButton("Finished");

        buttonHandler = new ButtonHandler(this);
        button1.addActionListener(buttonHandler);
        button3.addActionListener(buttonHandler);

        counter = 0;
        timeraction = new TimerAction(this);
        timer = new Timer(1000, timeraction);
        timer.setInitialDelay(1000);

        counter1 = 0;
        timeraction1 = new TimerAction1(this);
        timer1 = new Timer(1000, timeraction1);
        timer1.setInitialDelay(1000);

        }

    public static void main(String[] arg) {
        new ColoredWordsExperiment();
    }
}

-

class ButtonHandler implements ActionListener {
    ColoredWordsExperiment coloredWords;
    public ButtonHandler(ColoredWordsExperiment coloredWords) {
        this.coloredWords = coloredWords;
    }

    @Override
    public void actionPerformed(ActionEvent e){
        //if button1 pressed
        if (e.getActionCommand().equals("Matching")) {
            coloredWords.timer.start();
            coloredWords.timer1.start();
        //if button3 pressed
    }   else if (e.getActionCommand().equals("Finished")) {
            coloredWords.timer.stop();
            coloredWords.counter = 0;
            coloredWords.timer1.stop();
    }        

-

class TimerAction implements ActionListener {
    ColoredWordsExperiment coloredWords;
    public TimerAction(ColoredWordsExperiment coloredWords) {
        this.coloredWords = coloredWords;
    }

    @Override
    public void actionPerformed(ActionEvent event) {
        coloredWords.counter++;
    }

}

-

class TimerAction1 implements ActionListener {
    ColoredWordsExperiment coloredWords;
    public TimerAction1(ColoredWordsExperiment coloredWords) {
        this.coloredWords = coloredWords;
    }

    @Override
    public void actionPerformed(ActionEvent event) {
        coloredWords.counter1++;
    }

}

【问题讨论】:

  • 也许我遗漏了一些东西,但我在发布的代码中看不到您的问题的原因。这向我表明问题可能存在于未发布的代码中。作为一个侧面问题,我确实担心您的代码对字段的非 OOP 直接操作。这增加了复杂性增加的风险和可能的看不见的副作用,而良好的 OOP 实践旨在减少这种情况。

标签: java swing timer


【解决方案1】:

您的修改后的代码对我有用:

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

@SuppressWarnings("serial")
public class ColoredWordsExperiment extends JPanel {
   Timer timer;
   Timer timer1;
   TimerAction timeraction;
   TimerAction1 timeraction1;
   private int counter;
   private int counter1;
   private JButton button1;
   private JButton button3;
   private ButtonHandler buttonHandler;
   private JLabel counterLabel = new JLabel("   ");
   private JLabel counter1Label = new JLabel("   ");

   ColoredWordsExperiment() {
      button1 = new JButton("Matching");
      button3 = new JButton("Finished");

      buttonHandler = new ButtonHandler(this);
      button1.addActionListener(buttonHandler);
      button3.addActionListener(buttonHandler);

      counter = 0;
      timeraction = new TimerAction(this);
      timer = new Timer(1000, timeraction);
      timer.setInitialDelay(1000);

      counter1 = 0;
      timeraction1 = new TimerAction1(this);
      timer1 = new Timer(1000, timeraction1);
      timer1.setInitialDelay(1000);

      add(button1);
      add(button3);
      add(new JLabel("Counter:"));
      add(counterLabel);
      add(new JLabel("Counter1:"));
      add(counter1Label);

   }

   public int getCounter() {
      return counter;
   }


   public void setCounter(int counter) {
      this.counter = counter;
      counterLabel.setText(String.valueOf(counter));
   }


   public int getCounter1() {
      return counter1;
   }


   public void setCounter1(int counter1) {
      this.counter1 = counter1;
      counter1Label.setText(String.valueOf(counter1));
   }


   private static void createAndShowGui() {
      ColoredWordsExperiment mainPanel = new ColoredWordsExperiment();

      JFrame frame = new JFrame("Colored Words Experiment");
      frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

class ButtonHandler implements ActionListener {
   ColoredWordsExperiment coloredWords;

   public ButtonHandler(ColoredWordsExperiment coloredWords) {
      this.coloredWords = coloredWords;
   }

   @Override
   public void actionPerformed(ActionEvent e) {
      // if button1 pressed
      if (e.getActionCommand().equals("Matching")) {
         coloredWords.timer.start();
         coloredWords.timer1.start();
         // if button3 pressed
      } else if (e.getActionCommand().equals("Finished")) {
         coloredWords.timer.stop();
         //!! coloredWords.counter = 0;
         coloredWords.setCounter(0); //!! 
         coloredWords.timer1.stop();
      }
   }
}

class TimerAction implements ActionListener {
   ColoredWordsExperiment coloredWords;

   public TimerAction(ColoredWordsExperiment coloredWords) {
      this.coloredWords = coloredWords;
   }

   @Override
   public void actionPerformed(ActionEvent event) {
      coloredWords.setCounter(coloredWords.getCounter() + 1);
      //!! coloredWords.counter++;
   }

}

class TimerAction1 implements ActionListener {
   ColoredWordsExperiment coloredWords;

   public TimerAction1(ColoredWordsExperiment coloredWords) {
      this.coloredWords = coloredWords;
   }

   @Override
   public void actionPerformed(ActionEvent event) {
      //!! coloredWords.counter1++;
      coloredWords.setCounter1(coloredWords.getCounter1() + 1);
   }

}

向我确认您的问题出在未显示代码的其他地方。

作为一个侧面问题,我确实担心您的代码对字段的非 OOP 直接操作。这增加了复杂性增加和可能的看不见的副作用的风险,这是良好的 OOP 实践旨在减少的。

【讨论】:

  • 谢谢,我已经发现了这个错误,它确实在代码的其他地方。我现在仍然会查看您的代码。
  • 是否有理由保留TimerTimerAction 变量package private 而不是private?哦,我明白了……原因是缺少 getter 方法:D。
  • @Tom 是否有任何对象应该有权访问这些字段的原因?这将允许他们修改它们会限制,这不太可能是你真正想要发生的事情。最好控制或拒绝访问
  • @MadProgrammer 您的评论没有任何意义。我问为什么这些变量不是private 而你告诉我一些关于public 变量的事情。
  • @MadProgrammer 澄清一下:我不是在谈论公共 getter 方法。我认为上下文会说明这一点......
猜你喜欢
  • 1970-01-01
  • 2017-05-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-28
  • 1970-01-01
  • 2014-08-19
  • 1970-01-01
  • 2022-06-27
相关资源
最近更新 更多