2015-04-26 69 views
1

我正嘗試使用多線程實現Java(Swing)中的MVC範例。目前,我使用Observer類和Model類以及Observable Class擴展了View類,並且它的工作正常(我的代碼沿着這個例子:http://austintek.com/mvc/)。控制器類簡單地調用相應的模型和視圖函數,並且有一個主要的「粘合」程序來設置所有的東西。但是,這種方法不使用線程。 有沒有什麼辦法可以通過使用線程 Observer/Observable類同時實現?使用線程在Swing(Java)中實現MVC範例

(我的目的是實現各M,V和C的作爲單獨的線程)

以下是我的視圖代碼部分:

public class View implements Observer 
{ 

/*************************************** View ************************************* 
     ** 
    ** This function is the constructor of the View class. 
    ** 
    **  PRE: <nothing> 
    **  POST: the GUI is created and the directory is displayed on the screen. 
    **  RETURN: <N/A> 
    ** 
    **/ 
    View(String name) 
    { 
     threadName = name; 

     //frame in constructor and not an attribute as doesn't need to be visible to whole class 
     JFrame frame = new JFrame("simple MVC"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     addElements(frame); // this is implemented separately 

     // frame.addWindowListener(new CloseListener());  
     frame.setSize(900, 732); 
     // frame.setLocation(100,100); 

     /** Display the window **/ 
     //frame.pack(); 
     frame.setVisible(true); 

    }// View 

而下面是部分我的型號代碼:

public class Model extends java.util.Observable 
    { 
     /****************** variable DECLARATIONS *********************/ 

    // The hash table in which the directory is stored 
    private Hashtable<String, Integer> tel_dir 
        = new Hashtable<String, Integer>(); 

    // The path of the telephone directory during runtime 
    private URL filePath 
      = Model.class.getClassLoader().getResource("directory.txt"); 

    // the name of the thread 
    private String threadName; 
         /****** private Thread t; ******/ 
    // the object in which the message is sent to the view 
    private Message message = new Message(); 

    /** GETTERS and SETTERS **/ 

    .... 


    /*************************************** Model ************************************* 
    ** 
    ** This function is the constructor of the model class. 
    ** 
    **  PRE: <nothing> 
    **  POST: the telephone directory is input from the file and stored in a hash 
    **   table. 
    **  RETURN: <N/A> 
    ** 
    **/ 
    Model(String name) 
    { 
     int phone_num; 
     String customer_name; 
     Scanner scanner = null; 
     threadName  = name; 

     /** Opening the handle to the file containing the telephone directory **/ 
     try 
     { 
      scanner = new Scanner(new File(filePath.getPath())); 
     } 
     catch (FileNotFoundException e1) 
     { 
      e1.printStackTrace(); 
     } 

     /** Inputting from the file and storing it in the hash table **/ 
     while (scanner.hasNextInt()) 
     { 
      phone_num  = scanner.nextInt(); 
      customer_name = scanner.nextLine(); 
      customer_name = customer_name.trim(); 
      tel_dir.put(customer_name, phone_num); 
     } 

    }// Model 
+1

Swing已經扮演了視圖和控制器的角色。你想如何解耦? –

回答

3

我的目標是實現每個M的,V一d C作爲單獨的線程。

這可能不是一個有用的部門。 Swing視圖必須在event dispatch thread上構建和操縱,並且用戶期望Swing控件保持響應,如討論的here

取而代之,在SwingWorker的後臺運行任何耗時的模型,並在您的實施process()中更新監聽視圖。在此example中,視圖組件直接更新,但您也可以爲已註冊的偵聽器啓用自定義事件,如建議here。在此example中,chart通過SeriesChangeEvent偵聽其數據模型series;它響應process()中的工作人員更新。您可以通過PropertyChangeEvent顯示進度,如建議herehere