2014-04-15 1 views
0

타이머를 다시 시작하거나 특정 코드 행을 수행 한 후에 다른 지연을 추가해야합니다.Java Swing 재시작 타이머 작동 후

private static class ButtonHandler implements ActionListener { 
    public void actionPerformed (ActionEvent e) { 
     final JButton button = (JButton)e.getSource(); 
     Timer timer = new Timer(1000, new ActionListener() { 
        public void actionPerformed(ActionEvent e) { 
         String tc = random(); 
         them.setText("They chose: " + tc + "!"); 

         if (button == rock) { 
          whoWins("rock", tc); 
         } else if (button == paper) { 
          whoWins("paper", tc); 
         } else if (button == scissors) { 
          whoWins("scissors", tc); 
         } 
         yourWins.setText("Your wins: " + yw); 
         theirWins.setText("Their wins: " + tw); 
        } 
       }); 
     timer.setRepeats(false); 
     timer.start();  
    } 
} 

나는 내가 타이머를 다시 시작하는데 그렇다면 나는,이 작업을 수행하는 방법을 잘 모르겠습니다 아직까지

them.setText("they chose: " + tc + "!"); 

후 타이머의 제 2 지연을 구현하고자하는 곳에 나는 것 그 코드 줄을 써주시겠습니까? 미리 감사드립니다.

+0

사용자 상호 작용없이 메시지가 표시된 직후 (그들은 ...를 선택했습니다). 그것은 기본적으로 화면에 첫 번째 메시지를 표시하는 데 오랜 시간이 걸리는 중간에 일시 중지 된 두 개의 지연이 있어야합니다. – patrickstarpants

+0

더 이상 도움이 필요하지 않으므로 모든 도움을 주셔서 감사 드리며 드디어 간단한 프로그램을 개발할 수 있습니다. – patrickstarpants

답변

2

나는 이것을 귀하의 previous question에 게시 할 예정 이었지만, 당신은 약간의 진전을 보였습니다.

좋아요,이 예제에서는 세 가지 버튼 중에서 선택할 수 있습니다. 하나를 클릭하면 선택한 내용이 기록되고 버튼이 비활성화되고 1 초를 기다리는 Timer이 시작됩니다.

TimerActionListener 다음 선택하고 출력을 어떤 업데이트를 확인 후 ... 다음 버튼을 사용할 수 있도록 다시 1 초, 대기 다른 Timer 시작

enter image description here

import java.awt.BorderLayout; 
import java.awt.EventQueue; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.Timer; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class Chocies { 

    public static void main(String[] args) { 
     new Chocies(); 
    } 

    public Chocies() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
       } 

       JFrame frame = new JFrame("Testing"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setLayout(new BorderLayout()); 
       frame.add(new TestPane()); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    public class TestPane extends JPanel { 

     private JButton choice1; 
     private JButton choice2; 
     private JButton choice3; 

     private JLabel output; 

     private JButton choice; 

     public TestPane() { 
      setLayout(new BorderLayout()); 
      choice1 = new JButton("Door 1"); 
      choice2 = new JButton("Door 2"); 
      choice3 = new JButton("Door 3"); 
      JPanel panel = new JPanel(); 
      panel.add(choice1); 
      panel.add(choice2); 
      panel.add(choice3); 

      output = new JLabel("Pick a door"); 
      output.setHorizontalAlignment(JLabel.CENTER); 

      add(output, BorderLayout.NORTH); 
      add(panel); 

      ButtonHandler handler = new ButtonHandler(); 
      choice1.addActionListener(handler); 
      choice2.addActionListener(handler); 
      choice3.addActionListener(handler); 
     } 

     public class ButtonHandler implements ActionListener { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       output.setText("Wait for it..."); 
       choice = (JButton) e.getSource(); 
       choice1.setEnabled(false); 
       choice2.setEnabled(false); 
       choice3.setEnabled(false); 
       Timer timer = new Timer(1000, new TimerHandler()); 
       timer.setRepeats(false); 
       timer.start(); 
      } 

     } 

     public class TimerHandler implements ActionListener { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       if (choice1 == choice) { 
        output.setText("Door 1 selected"); 
       } else if (choice2 == choice) { 
        output.setText("Door 2 selected"); 
       } else if (choice3 == choice) { 
        output.setText("Door 3 selected"); 
       } 

       Timer timer = new Timer(1000, new ActionListener() { 
        @Override 
        public void actionPerformed(ActionEvent e) { 
         choice1.setEnabled(true); 
         choice2.setEnabled(true); 
         choice3.setEnabled(true); 
        } 
       }); 
       timer.setRepeats(false); 
       timer.start(); 
      } 

     } 

    } 

} 
+0

알겠습니다 감사합니다. 정말 고마워요. 방금 첫 번째 메시지를 인쇄 한 직후에 다른 타이머 기능을 추가했습니다. 결함없이 작동합니다. 시간 내 주셔서 다시 한번 감사드립니다! – patrickstarpants

+0

@ user3534031 너무 오래 걸려서 미안 해요.) – MadProgrammer