Also ich habs jetzt wie folgt (das kann man so schon ausführen):
Ich muss aber nachdem ich gestoppt hab, auch wieder starten können und da kommt leider ein Fehler.
Wie könnte man es denn machen???
Code:
public class Clickbattle extends java.applet.Applet{
int iZeit=-1;
private JButton btStart = new JButton("Start");
private JButton btStop = new JButton("Stop");
private JLabel lbZeit = new JLabel();
private Timer zeit = new Timer(true);
private TimerTask zaehler = new TimerTask(){
public void run(){
iZeit = iZeit + 1;
lbZeit.setText(String.valueOf(iZeit));}};
private ActionListener al;
public void init(){
this.setSize(600,600);
this.setBackground(Color.LIGHT_GRAY);
this.setLayout(null);
setKomps();
implementActionListener();}
private void setKomps(){
btStart.setBounds(10,20,100,20);
this.add(btStart);
btStop.setBounds(10,20,100,20);
this.add(btStop);
btStop.setVisible(false);
lbZeit.setBounds(200,20,200,20);
this.add(lbZeit);
lbZeit.setFont(new Font("Arial", Font.BOLD, 20));
lbZeit.setHorizontalAlignment(SwingConstants.CENTER);
lbZeit.setVerticalAlignment(SwingConstants.CENTER);}
private void runningKomps(){
btStart.setVisible(false);
btStop.setVisible(true);}
private void stoppedKomps(){
btStop.setVisible(false);
btStart.setVisible(true);}
private void implementActionListener(){
al = new ActionListener(){
public void actionPerformed(ActionEvent e){
if (btStart == e.getSource()){
runningKomps();
zeit.schedule(zaehler, 0, 1000);}
else if (btStop == e.getSource()){
stoppedKomps();
zeit.cancel();
iZeit = -1;}}};
btStart.addActionListener(al);
btStop.addActionListener(al);}
}
Ich muss aber nachdem ich gestoppt hab, auch wieder starten können und da kommt leider ein Fehler.
Wie könnte man es denn machen???