2016-05-26 17 views
0

私はJava Appletでパズルゲームを作ったが、プレイヤーがこの時間内にパズルを解決しなければならないタイマーを5分間追加する必要があります。再試行を求めるダイアログボックスが表示されない場合は、だから私はタイマーが再び始まる必要があります。カウントダウンタイマーhelppp

誰かが私にこれをコード化する方法を教えてもらえますか?

public void init(){ 
String MINUTES = getParameter("minutes"); 
if (MINUTES != null) remaining = Integer.parseInt(MINUTES) * 600000; 
else remaining = 600000; // 10 minutes by default 

// Create a JLabel to display remaining time, and set some PROPERTIES. 
label = new JLabel(); 
// label.setHorizontalAlignment(SwingConstants.CENTER); 
// label.setOpaque(false); // So label draws the background color 




// Now add the label to the applet. Like JFrame and JDialog, JApplet 
// has a content pane that you add children to 
    count.add(label); 
    Puzframe.add(count,BorderLayout.SOUTH); 


// Obtain a NumberFormat object to convert NUMBER of minutes and 
// seconds to strings. Set it up to produce a leading 0 if necessary 
format = NumberFormat.getNumberInstance(); 
format.setMinimumIntegerDigits(2); // pad with 0 if necessary 

// Specify a MouseListener to handle mouse events in the applet. 
// Note that the applet implements this interface itself 


// Create a timer to call the actionPerformed() method immediately, 
// and then every 1000 milliseconds. Note we don't START the timer yet. 
timer = new Timer(1000, this); 
timer.setInitialDelay(0); // 
timer.start(); } 



public void start() { resume(); } 



    //The browser calls this to stop the applet. It may be restarted  later. 
    //The pause() method is defined below 

void resume() { 
    // Restore the time we're counting down from and restart the timer. 
lastUpdate = System.currentTimeMillis(); 
timer.start(); // Start the timer 
}` 

//Pause the countdown 



void updateDisplay() { 
long now = System.currentTimeMillis(); // current time in ms 
long elapsed = now - lastUpdate; // ms elapsed since last update 
remaining -= elapsed; // adjust remaining time 
lastUpdate = now; // remember this update time 

// Convert remaining milliseconds to mm:ss format and display 
if (remaining < 0) remaining = 0; 
int minutes = (int)(remaining/60000); 
int seconds = (int)((remaining)/1000); 


label.setText(format.format(minutes) + ":" + format.format(seconds)); 
label.setForeground(new Color(251,251,254)); 
    label.setBackground(new Color(0,0,0)); 
// If we've completed the countdown beep and display new page 
if (remaining == 0) { 
    // Stop updating now. 
    timer.stop(); 
} 

    count.add(label); 

Puzframe.add(label,BorderLayout.SOUTH); } 

これまで私はこれまで何をしていましたが、私の問題はゲームには現れないということです。私はのactionPerformed

+0

これまでに何を試しましたか?特定の問題のあるコードを表示してください。 – ReadyPlayer2

+0

自分の投稿にコードを追加しました。 – Rafa

答えて

1

利用スイングTimerから、私が

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

public class SwingControlDemo { 

    private JFrame mainFrame; 
    private JPanel controlPanel; 
    private Timer timer; 

    public SwingControlDemo(){ 
     prepareGUI(); 
    } 

    public static void main(String[] args){ 
     SwingControlDemo swingControlDemo = new SwingControlDemo(); 
     swingControlDemo.showEventDemo();  
    } 

    private void prepareGUI(){ 
     mainFrame = new JFrame("Java SWING Examples"); 
     mainFrame.setSize(400,400); 
     mainFrame.setLayout(new GridLayout(3, 1)); 

     mainFrame.addWindowListener(new WindowAdapter() { 
     public void windowClosing(WindowEvent windowEvent){ 
      System.exit(0); 
     }   
     });  
     controlPanel = new JPanel(); 
     controlPanel.setLayout(new FlowLayout()); 

     mainFrame.add(controlPanel); 
     mainFrame.setVisible(true); 

    //javax.swing.Timer 
    timer = new Timer(4000, new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent e) { 
     JOptionPane.showMessageDialog(mainFrame, 
       "End Of Game", 
       "5 minutes has passed", 
       JOptionPane.ERROR_MESSAGE); 
     } 
    }); 
    } 

    private void showEventDemo(){ 

     JButton okButton = new JButton("Start Game"); 

     okButton.setActionCommand("OK"); 

     okButton.addActionListener(new ButtonClickListener()); 

     controlPanel.add(okButton); 

     mainFrame.setVisible(true); 
    } 

    private class ButtonClickListener implements ActionListener{ 

     public void actionPerformed(ActionEvent e) { 
     timer.start(); 
     String command = e.getActionCommand(); 
     if(command.equals("OK")) { 
      System.out.println("Timer started"); 
     } 
     } 
    } 
} 
それを証明するために簡単な例を用意し、このようなシナリオ

//javax.swing.Timer 
timer = new Timer(4000, new ActionListener() { 
    @Override 
    public void actionPerformed(ActionEvent e) { 
    JOptionPane.showMessageDialog(mainFrame, 
      "End Of Game", 
      "5 minutes has passed", 
      JOptionPane.ERROR_MESSAGE); 
    } 
}); 

のために作られている)(updateDisplayを呼んでいます

関連する問題