[英]JAVA : How do I reduce the timer's time when the timer is running?
我想使每次輸入錯誤的答案都減少時間,並且我有更多這樣的面板,我想使當我移至下一個面板時開始計時,而不是在我編譯文件計時器時同時啟動。
int time = 7000;
ans2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
if(ans2.getText().toUpperCase().equals("CAT")){
word.setText("CAT");
ans2.setText("");
if(word.getText().equals("CAT") && word2.getText().equals("CARD") && word3.getText().equals("TILES") && word4.getText().equals("MIRROR") && word5.getText().equals("COUCH") && word6.getText().equals("STRAW")){
JOptionPane.showMessageDialog(null, "COMPLETE\n Go to Next Stage");
word.setText(""); word2.setText(""); word3.setText(""); word4.setText(""); word5.setText(""); word6.setText("");
timer.stop();
CardLayout cl = (CardLayout) main.getLayout();
cl.show(main, "Stage2");
}
}
//i want to make "else if wrong answer time reduce 2 sec"
}
});
JProgressBar progressBar = new JProgressBar(JProgressBar.HORIZONTAL, 0, 70);
progressBar.setValue(70);
progressBar.setPreferredSize(new Dimension(600, 20));
ActionListener listener = new ActionListener() {
int counter = 70;
public void actionPerformed(ActionEvent ae) {
counter--;
progressBar.setValue(counter);
if (counter<1) {
JOptionPane.showMessageDialog(null, "Game Over!");
timer.stop();
CardLayout cl = (CardLayout) main.getLayout();
cl.show(main, "Main Menu");
}
}
};
timer = new Timer(time, listener);
timer.start();// i have main menu and i want the timer start when i press start button in main menu
您應該使用Swing Timer
而不是AWT計時器。 閱讀API,有一些方法可以讓您設置/更改計時器延遲。
不要使用KeyListener來處理文本字段上的Enter鍵。 相反,您應該在文本字段中添加一個ActionListener。 Enter鍵將調用該動作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.