2014-10-02 63 views
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); 
     } 
    } 
}** 
+0

@HovercraftFullOfEels我很抱歉。您的答案帮助我构建应用程序变得更加容易。再次,我很抱歉,我不知道堆栈溢出的规则,因为我是新的论坛。非常感谢你的帮助。 – sumanth 2014-10-04 02:13:57

+0

没关系。谢谢回复。我们是志愿者,宁愿不要忽视,所以你的回复意味着很多。 – 2014-10-04 02:19:15

回答

3

只使用一个的ActionListener或许更好,AbstractAction,给它一个布尔变量,然后把它改变基于布尔变量的状态。

例如,

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

public class ActionWithMultipleBehaviors extends JPanel { 
    private TimerButtonAction timerBtnAction = new TimerButtonAction("Start", "Reset"); 
    private JButton timerButton = new JButton(timerBtnAction); 

    public ActionWithMultipleBehaviors() { 
     add(timerButton); 
    } 

    class TimerButtonAction extends AbstractAction { 
     private boolean stateStart = true; 
     private String name; 
     private String secondName; 

     public TimerButtonAction(String name, String secondName) { 
     super(name); 
     this.name = name; 
     this.secondName = secondName; 
     } 

     @Override 
     public void actionPerformed(ActionEvent e) { 
     String newName; 
     if (stateStart) { 
      newName = secondName; 

      // TODO: add start timer code 

     } else { 
      newName = name; 

      // TODO: add reset timer code 

     } 
     putValue(NAME, newName); 
     stateStart = !stateStart; 
     } 
    } 

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

     JFrame frame = new JFrame("ActionWithMultipleBehaviors"); 
     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(); 
     } 
     }); 
    } 
}