[英]What is the best way to link your application to Swing GUI?
If I have Swing GUI class and application, how should I manage communication between these objects? 如果我具有Swing GUI类和应用程序,应如何管理这些对象之间的通信? Should I pass GUI object link to app or app link to GUI?
我应该将GUI对象链接传递给应用程序还是将应用程序链接传递给GUI?
Example: 例:
public class App{
public App()
{
GUI gui = new GUI(this)
}
}
or 要么
public GUI{
public GUI()
{
App gui = new App(this)
}
}
Have a look at the MVC pattern . 看一下MVC模式 。 Ideally you want to present your application such that different GUI components can make use of it (imagine later providing a web/servlet view of your application, or a command-line interface).
理想情况下,您希望展示您的应用程序,以便不同的GUI组件可以使用它(稍后提供您的应用程序的Web / Servlet视图或命令行界面)。 That requirement prohibits your application having implicit Swing knowledge.
该要求禁止您的应用程序具有隐式的Swing知识。
The current answers are all good, but there is one very key thing here: You have the "Multithreaded" tag but no one has addressed it yet. 当前的答案都很好,但是这里有一个非常关键的事情:您具有“多线程”标记,但是还没有人解决。 You definately want to think about the "C" in MVC as something that will be threaded and might even want to follow the Manager Design Pattern via a Manager Interface and maybe even a View Interface--the use of Interfaces will help widen your options even more for future refactoring, or even redesigns.
您绝对希望将MVC中的“ C”考虑为可穿线的东西,甚至可能希望通过Manager界面甚至是View界面遵循Manager设计模式-接口的使用将有助于扩大您的选择范围,甚至更多用于将来的重构甚至重新设计。 In the past I have used java.util.observer and java.util.observable--the original MVC Pattern ever in Java :)-- to cope with communication issues.
过去,我曾使用java.util.observer和java.util.observable(Java中最早的MVC模式:)来解决通信问题。 Just remember not to block your main Thread or your GUI will freeze.
只要记住不要阻塞您的主线程,否则您的GUI将冻结。 Yes MVC is a good start but in the App->GUI you describe there is a bit more than just MVC, Think of the entire GUI as a View for your app, Model will more than likely be some kind of Dataset(DataBase, flatfile, state information) and the Controller your app.
是的,MVC是一个不错的开始,但是在App-> GUI中,您描述的不仅仅是MVC,将整个GUI视为应用程序的视图,模型很可能是某种数据集(DataBase,flatfile ,状态信息)和控制器您的应用。 Keep in mind the GUI more than likely will at the very least have an Interface to it.
请记住,GUI至少将有一个接口。
Yes the MVC pattern will help. 是的,MVC模式会有所帮助。 You really want to remove the tight coupling between the two components.
您确实要删除两个组件之间的紧密耦合。 Or at the VERY least, go with the latter, meaning:
或者至少要与后者一起使用,意思是:
public GUI {
public GUI() {
App gui = new App(this)
}
}
That way, atleast your application logic doesn't rely on, or is bound to, you GUI. 这样,您的应用程序逻辑就不会依赖或绑定到GUI。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.