2014-04-07 10 views
1

내가 내 게임 플레이 패널 그래서 got this sample code재설정 카운트 다운 타이머는 자바 스윙 게임 + JDialog를/JOptionPane에 작동하지 내가 자바 스윙과 좋은 아니에요 내가 타이머 3 초</p> <p>그러나 상기의 지연 게임을 시작 사용하려고 해요

: 나는 대화 상자를 표시 할 같은 시간

그래서 내 대화는 다음과 같다 (또한 게임은 초점이 대화에 있어야하므로 3 초 기다려야합니다) 이 :

public class GamePlayPanel extends JPanel implements ActionListener { 
    // attributes 
    private JOptionCountDownTimer countDownDialog; 
    public GamePlayPanel(MainWindow mainWindow) { 
     // initialization attributes 
     initLayoutPanel(); 
     this.timer = new Timer(DELAY, this); 
     // Added a delay of 3 seconds so you can prepare to for the game 
     this.timer.setInitialDelay(3000); 
     resetTime(); 
    }   


    public void startGame() { 
     this.gamePanel.requestFocus(); 
     this.countDownDialog.startCountDown(); 
     startTimer(); // this is my game timer to record the game time 
    } 


    public void restartGame() { 
     this.countDownDialog.resetCountDown(); 
     startTimer();  
     this.gamePanel.requestFocus(); 
    } 
} 

잘 작동하지만 게임을 다시 시작하면 카운트 다운 타이머가 0 -> 2 초에 시작됩니다.

더 좋은 아이디어가 있습니까? JOptionCountDownTimer? 나는 그것이 JDialog 클래스를 확장하려고 노력했다. 그러나 나는 그것이 작용할 수 없었다.

+0

[이] (http://stackoverflow.com/a/12451673/230513)? – trashgod

+0

예, 코드가 있습니다. – t0tec

+0

문제를 설명하고 [원본] (http://stackoverflow.com/a/12451673/230513)을 인용하는 [mcve] (http://stackoverflow.com/help/mcve)를 포함하도록 질문을 편집하십시오.). – trashgod

답변

1

시험해보세요. 효과가 있는지보십시오. 대화 상자 클래스 코드를 가져올 수 있습니다. 당신이해야 할 일은 부모 프레임에 그것을 전달하는 것뿐입니다. 당신은 또한 그것을 아주 좋아할 수도 있습니다. 나는 방금 기능을 제공하고있다.

import java.awt.BorderLayout; 
import java.awt.Font; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JButton; 
import javax.swing.JDialog; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.SwingUtilities; 
import javax.swing.Timer; 
import javax.swing.border.EmptyBorder; 

public class CountDownTimer { 
    public CountDownTimer() { 
     final JFrame frame = new JFrame(); 
     JButton button = new JButton("Open Dilaog"); 
     button.addActionListener(new ActionListener(){ 
      public void actionPerformed(ActionEvent e) { 
       new CountDownTimerDialog(frame, true, 5); 
      } 
     }); 

     frame.add(button); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 

    private class CountDownTimerDialog extends JDialog { 
     private int count; 

     public CountDownTimerDialog(JFrame parent, boolean modal, int seconds) { 
      super(parent, modal); 
      count = seconds; 
      final JLabel countLabel = new JLabel(String.valueOf(seconds), JLabel.CENTER); 
      countLabel.setFont(new Font("impact", Font.PLAIN, 36)); 
      JLabel message = new JLabel("Wait to Start Game"); 
      message.setFont(new Font("verdana", Font.BOLD, 20)); 

      JPanel wrapper = new JPanel(new BorderLayout()); 
      wrapper.setBorder(new EmptyBorder(10, 10, 10, 10)); 
      wrapper.add(countLabel); 
      wrapper.add(message, BorderLayout.SOUTH); 
      add(wrapper); 

      Timer timer = new Timer(1000, new ActionListener(){ 
       public void actionPerformed(ActionEvent e) { 
        if (count == -1) { 
         dispose(); 
        } else { 
         countLabel.setText(String.valueOf(count)); 
         count--; 
        } 
       } 
      }); 
      timer.setInitialDelay(0); 
      timer.start(); 

      pack(); 
      setLocationRelativeTo(parent); 
      setVisible(true); 
     } 
    } 

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

완벽하게 작동합니다! 감사! – t0tec