你好我正在使用Java Swing定時器來讓它燃燒。除蠟燭幾乎燒到底的末端部分外,一切都很好。計時器應停止時,尺寸較小,然後讓說15 ..現在我做了一些代碼,似乎我所做的if/else語句beeing完全忽略..有人可以告訴我,我做錯了什麼?當特定值達到時停止Java swing定時器
下面你可以看到我的代碼:(PS的getCandleSize方法現在帶來了從500到10> 0,然後我得到負值長度)
package h04SelfBurnCandle;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import h02aKaars.DrawCandle;
public class CandlePanel extends JPanel implements ActionListener {
private boolean aan = true;
private int lengte = 500;
private int x;
private int y;
private int speed = 10;
private final int WACHTTIJD = 100; // wachttijd voor de timer
public CandlePanel() {
javax.swing.Timer autoburn = new javax.swing.Timer(WACHTTIJD,this);
autoburn.start();
}
DrawCandle candle = new DrawCandle();
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
x = 10;
y = getHeight() - (lengte + 10);
for(int i=0; i < 5; i++) {
if(lengte > 15){
candle.drawCandle(g, x, y, lengte, aan);
}
else {
candle.drawCandleBurned(g, x, y, lengte);
}
x = x + 70;
System.out.println(lengte);
}
}
public int getCandleSize() {
do {
lengte -= speed;
}
while(lengte > 15);
return lengte;
}
@Override
public void actionPerformed(ActionEvent e) {
getCandleSize();
//System.out.println(lengte);
repaint();
}
}
謝謝你做到了!是否還有一種方法可以啓動每個蠟燭的計時器(在這種情況下爲5個,就像你在循環中看到的那樣)是分開的? – Reshad
當您不再需要它來阻止浪費的回調時,您還應該在自動執行實例上調用Timer.stop()。您需要讓自動滾屏變量可見,但 – MadProgrammer
我該怎麼做? – Reshad