使用线程在 Swing (Java) 中实现 MVC 范例
Implementing MVC paradigm in Swing (Java) using Threads
我正在尝试使用多线程在 Java (Swing) 中实现 MVC 范例。目前,我正在扩展视图 class 和观察者 Class 以及模型 Class 和 Observable Class 及其工作正常(我的代码是沿着这个例子构建的: http://austintek.com/mvc/)。控制器 class 只需调用相应的模型和视图函数,并且有一个主 "glue" 程序可以设置所有内容。但是,这种方法不使用线程。 有什么方法可以同时使用和和Observer/Observableclass线程来实现这个吗?
(我的目标 是将 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
My aim is to implement each of the M, V and C as a separate thread.
这可能不是一个有用的划分。必须在 event dispatch thread, and users expect Swing controls to remain responsive, as discussed here.
上 仅 构建和操作 Swing 视图
取而代之,运行任何一个耗时的后台模型SwingWorker
, and update listening views in your implementation of process()
. In this example, the view components are updated directly, but you can also fire a custom event for registered listeners, as suggested here. In this example, the chart
listens to its data model, series
, via SeriesChangeEvent
; it responds to the worker's update in process()
. You can show progress via a PropertyChangeEvent
, as suggested here or here。
我正在尝试使用多线程在 Java (Swing) 中实现 MVC 范例。目前,我正在扩展视图 class 和观察者 Class 以及模型 Class 和 Observable Class 及其工作正常(我的代码是沿着这个例子构建的: http://austintek.com/mvc/)。控制器 class 只需调用相应的模型和视图函数,并且有一个主 "glue" 程序可以设置所有内容。但是,这种方法不使用线程。 有什么方法可以同时使用和和Observer/Observableclass线程来实现这个吗?
(我的目标 是将 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
My aim is to implement each of the M, V and C as a separate thread.
这可能不是一个有用的划分。必须在 event dispatch thread, and users expect Swing controls to remain responsive, as discussed here.
上 仅 构建和操作 Swing 视图取而代之,运行任何一个耗时的后台模型SwingWorker
, and update listening views in your implementation of process()
. In this example, the view components are updated directly, but you can also fire a custom event for registered listeners, as suggested here. In this example, the chart
listens to its data model, series
, via SeriesChangeEvent
; it responds to the worker's update in process()
. You can show progress via a PropertyChangeEvent
, as suggested here or here。