2014-02-19 77 views
1

My t.stop();方法不起作用。我瘋了,試圖找出爲什麼我的停止方法不起作用。定時器t =新定時器(50,b); t.stop();不起作用

我在我的代碼中使用了一個計時器,我無法停下來。任何人都可以看看,並告訴我這是怎麼回事?:

/*Gilberto Rose*/ 
package homework2; 

import java.awt.Graphics; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.util.ArrayList; 
import java.util.List; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.Timer; 

public class MultipleBalls extends JFrame implements ActionListener 
{ 

    int dx = 2; 
int dy = 2; 
int x = 1; 
int y = 1; 
int i = 0; 

public static void main(String[] args) 
{ 
    Runnable balls = new Ball2(); 

    Thread thread1 = new Thread(balls); 

    thread1.run(); 

} 

@Override 
public void actionPerformed(ActionEvent arg0) 
{ 
    repaint(); 
    System.out.println(i++); 
} 



}// End of Ball class 



class Ball2 extends JPanel implements Runnable 
{ 
MultipleBalls b = new MultipleBalls(); 
JButton g = new JButton("resume"); 
JButton f = new JButton("suspend"); 
JButton e = new JButton("-1"); 
JButton d = new JButton("+1"); 
List<Ball2> L = new ArrayList<Ball2>(); 
Timer t = new Timer(50, b); 
public int x = 6; 

public void loopstop() 
{ 
    t.stop(); 
}// end of loopstop method 

Ball2() 
{ 
    controller4(); 
    controller3(); 
    controller2(); 
    controller(); 
    add(d); 
    add(e); 
    add(f); 
    add(g); 

}// End of Ball2 constructor 



public void run() 
{ 

    Ball2 c = new Ball2(); 
    b.setSize(500, 500); 
    b.setVisible(true); 
    b.add(c); 
    t.start(); 
} // End of run method 


public void controller() 
{ 
    d.addActionListener(new ActionListener() 
    { 
     public void actionPerformed(ActionEvent e) 
     { 
      L.add(new Ball2()); 
     } 
    }); 
}// End of controller 

public void controller2() 
{ 
    e.addActionListener(new ActionListener() 
    { 
     public void actionPerformed(ActionEvent e) 
     { 
      System.out.println("subtracter"); 
      L.remove(L.size()-1); 
     } 
    }); 
}// End of controller2 

public void controller3() 
{  
    f.addActionListener(new ActionListener() 
    { 

     public void actionPerformed(ActionEvent e) 
     { 
      loopstop(); 
     } 
    }); 
}// End of controller3 

public void controller4() 
{ 
    g.addActionListener(new ActionListener() 
    { 
     public void actionPerformed(ActionEvent e) 
     { 
     System.out.println("Resume"); 
     } 
    }); 
}// End of controller4 


public void paintComponent(Graphics g) 
{ 

    if(L.size() > 0) 

     { 
     int i = 0; 
      do 
     {  
      g.fillOval(L.get(i).ballx(), L.get(i).bally(), 90, 90); 

      i++; 
      }while(i < L.size() && true); // End of Do while loop 


     }// End of if statement 

    }// End of paintComponent 


MultipleBalls bb = new MultipleBalls(); 

public int ballx() 
{ 
    if (bb.x == 0 || bb.x == 500) 
    { 
     bb.dx *= -1; 

    } // End of if statement 

    bb.x += bb.dx; 

    return bb.x; 
} 

public int bally() 
{ 
    if (bb.y == 0 || bb.y == 500) 
    { 
     bb.dy *= -1; 
    }// end of if statement 

    bb.y += bb.dy; 
    return bb.y; 

}// End of bally 




}// End of Ball2 class 
+1

我建議:http://www.sscce.org/ –

回答

4

你的代碼是非常令人費解,我認爲,它是從一種叫cyclomatic complexity苦難,那麼多的話,就很難對您或我們看看什麼對象在創建什麼其他對象,什麼在運行什麼。這是你的問題。您至少有兩個MultipleBall對象,兩個Ball2對象,並且您正在爲其中一個Ball2對象啓動計時器並將其停止。

解決方案:大大簡化此代碼。

  • 創建一個MultipleBalls對象,只有一個。
  • 沒有MultipleBalls實現ActionListener。而是爲你的ActionListener使用一個匿名的內部類,並在你需要的地方創建它。
  • 只創建一個Ball2對象,只有一個。

還要注意的是,你幾乎從來沒有打電話run()一個Thread對象,而是start(),不過話說回來,我甚至不能確定,你應該使用你使用它,其中一個Thread對象。


編輯
我的主類是簡單的,並且只會有得到的東西開始的重要手段和支持方法。喜歡的東西:

public class MultipleBalls { 

    private static void createAndShowGui() { 
     BallsPanel mainPanel = new BallsPanel(); 

     JFrame frame = new JFrame("Multiple Balls"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().add(mainPanel); 
     frame.pack(); 
     frame.setLocationByPlatform(true); 
     frame.setVisible(true); 
    } 

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

編輯

對於關注點分離的例子:

import java.awt.Dimension; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.KeyEvent; 
import java.util.ArrayList; 
import java.util.Iterator; 
import java.util.List; 

import javax.swing.*; 

public class MultipleBallsZ { 

    private static void createAndShowGui() { 
     BallsPanelZ ballsPanel = new BallsPanelZ(); 
     new Control(ballsPanel); 

     JFrame frame = new JFrame("Multiple Balls"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().add(ballsPanel); 
     frame.pack(); 
     frame.setLocationByPlatform(true); 
     frame.setVisible(true); 
    } 

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

@SuppressWarnings("serial") 
class BallsPanelZ extends JPanel { 
    private static final int TIMER_DELAY = 200; 
    private static final int PREF_W = 400; 
    private static final int PREF_H = PREF_W; 

    private Timer timer = new Timer(TIMER_DELAY, new TimerListener()); 
    private int counter = 0; 
    private Control control = null; 

    public BallsPanelZ() { 
     timer.start(); 
    } 

    @Override 
    public Dimension getPreferredSize() { 
     return new Dimension(PREF_W, PREF_H); 
    } 

    public Timer getTimer() { 
     return timer; 
    } 

    private class TimerListener implements ActionListener { 
     @Override 
     public void actionPerformed(ActionEvent e) { 
     counter++; 
     System.out.printf("Count: %03d%n", counter); 
     } 
    } 

    public void setControl(Control control) { 
     this.control = control; 
     for (Action action : control) { 
     add(new JButton(action)); 
     } 
    } 
} 

@SuppressWarnings("serial") 
class Control implements Iterable<Action> { 
    private List<Action> actionList = new ArrayList<>(); 
    private BallsPanelZ ballsPanel; 

    public Control(BallsPanelZ ballsPanel) { 
     actionList.add(new PauseAction()); 
     actionList.add(new ResumeAction()); 

     this.ballsPanel = ballsPanel; 
     ballsPanel.setControl(this); 
    } 

    private class PauseAction extends AbstractAction { 
     public PauseAction() { 
     super ("Timer Pause"); 
     putValue(MNEMONIC_KEY, KeyEvent.VK_P); 
     } 

     @Override 
     public void actionPerformed(ActionEvent e) { 
     ballsPanel.getTimer().stop(); 
     } 
    } 

    private class ResumeAction extends AbstractAction { 
     public ResumeAction() { 
     super("Timer Resume"); 
     putValue(MNEMONIC_KEY, KeyEvent.VK_R); 
     putValue(DISPLAYED_MNEMONIC_INDEX_KEY, 6); 
     } 

     @Override 
     public void actionPerformed(ActionEvent e) { 
     ballsPanel.getTimer().restart(); 
     } 
    } 

    @Override 
    public Iterator<Action> iterator() { 
     return actionList.iterator(); 
    } 
}