1
我有一个小的查询请让我解释一下这个场景。我有一个swing jframe,其中有一个名为“start”的按钮,它以秒为单位启动计时器,所以无论何时我点击开始,它都会将按钮本身转换为“reset”,这应该使秒数为零,并且应该再次将其转换为“开始”。我关心的是,这两个场景我必须运行两组代码,我使用了两个类来实现ActionListener接口,有没有什么方法可以将这两组代码包含在实现ActionListener的同一个类中,并切换代码块取决于布尔变量在按钮更改时更改其值。使用布尔变量代替ActionListner接口在挥杆
我试过了,但我面临性能问题,如冻结应用程序,甚至没有像预期的那样工作。
请查看下面的代码。
public class SuperTimer extends JFrame
{
JButton start;
private final StartCountDown countdown;
public SuperTimer()
{
countdown= new StartCountDown();
start.addActionListener(countdown);
}
public class StartCountDown implements ActionListener
{
public void actionPerformed(ActionEvent l)
{
if(l.getSource()==start)
{
count = Long.parseLong(input.getText());
start.setText("Reset");
reset = new Reset();
start.removeActionListener(countdown);
start.addActionListener(reset);
invalid.setVisible(false);
}
TimeClass tc = new TimeClass(count);
timer = new Timer(1000,tc);
timer.start();
}
}
public class Reset implements ActionListener
{
public void actionPerformed(ActionEvent j)
{
start.setText("Start");
time.setText("00:00:00");
input.setText("");
timer.stop();
timeup.setVisible(false);
start.removeActionListener(reset);
start.addActionListener(countdown);
}
}
}**
@HovercraftFullOfEels我很抱歉。您的答案帮助我构建应用程序变得更加容易。再次,我很抱歉,我不知道堆栈溢出的规则,因为我是新的论坛。非常感谢你的帮助。 – sumanth 2014-10-04 02:13:57
没关系。谢谢回复。我们是志愿者,宁愿不要忽视,所以你的回复意味着很多。 – 2014-10-04 02:19:15