2014-03-29 32 views
1

我應該如何在視圖上實現一個具有多個JButton的MVC控制器?多個JButton的MVC模式

例如:我有一個開始按鈕,停止按鈕等等。

我試圖做到這一點的啓動按鈕,它工作正常,但那麼我如何實現它停止按鈕觸發?

控制器代碼:

public MVCAuctionController(Auction a, MVCAuctionView v) { 
     auction = a; 
     view = v; 
     view.addProcessBidsListener(new ProcessBidsController()); 
     view.addStopProcessListener(new StopBidsController()); 
    } 


    class ProcessBidsController implements ActionListener 
    { 
     public void actionPerformed(ActionEvent e) 
     { 
      view.disableProcessButton(); 
      Thread thread = new Thread (auction); 
      thread.start(); 
     } 

    } 

addProcessBidsListener - 與START /過程的按鈕相關聯,當我在按鈕上點擊 - 線程開始運行並填充用數據的JTextArea。

現在我的停止按鈕應該停止線程。對於這一點,如果我做這樣的事情它沒有真正停止線程:

class ProcessStartController implements ActionListener 
    { 
     public void actionPerformed(ActionEvent e) 
     { 
      if(e.getSource() == view.start){    
      view.disableStartButton(); 
      new Thread (rest).start(); 
      //thread.start(); 
      System.out.println("inside action performed of start button"); 
      view.kitchen.append("Orders to kitchen"); 
      } 
      else if (e.getSource() == view.stop) 
      { 
       new Thread (rest).interrupt(); 
      } 
     } 


    } 
+0

看起來你已經有一些體面的設置開始了。你有什麼特別的困惑? –

+0

addProcessBidsListener - 與START/Process按鈕相關聯,當我點擊按鈕時 - 線程開始運行並用數據填充JTextArea。 – user3473791

回答

2

使用的Action每個按鈕,它們是自包含的控制器

How to use Actions更多細節

然後,您可以在動作和主控制器之間建立某種關係,如果你需要它,通過某種形式的聽衆,例如