[英]MVC Java Swing usage
I want to write an application in Java SWING using the MVC architecture, but I have problem with using multiple controllers because I don't know how to do this. 我想使用MVC架构在Java SWING中编写一个应用程序,但是使用多个控制器存在问题,因为我不知道该怎么做。
public class Application {
public static void main(String[] args) {
Application app = new Application();
app.Start();
}
public void Start()
{
MainModel model = new MainModel();
MainView view = new MainView();
MainController controller = new MainController(view,model);
view.setVisible(true);
}}
MainController: 主控制器:
public class MainController {
private MainView theView;
private MainModel theModel;
public MainController(MainView theView, MainModel theModel)
{
this.theModel = theModel;
this.theView = theView;
}}
MainView: 主视图:
public class MainView extends JFrame{
private JMenu menu = new JMenu();
public MainView()
{
JPanel panel = new JPanel();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(800,800);
panel.add(menu);
this.add(panel);
}}
I do not put here because the model is now empty. 我没有放在这里,因为模型现在为空。
Okay, so I don't know how to use multiple controllers. 好的,所以我不知道如何使用多个控制器。 For example: I want to create new controller UserController and I don't know how to implement this.
例如:我想创建新的控制器UserController,但我不知道如何实现它。 Should I create new controller in MainController?
我应该在MainController中创建新的控制器吗? If I create UserController in MainController how to use it?
如果我在MainController中创建UserController怎么使用?
Just create UserController in your Application class where you added your MainController. 只需在添加MainController的Application类中创建UserController。
Example: if you have 2 buttons you can add actionlistener. 示例:如果您有2个按钮,则可以添加actionlistener。
Button1 will do UserController.doSomething();
Button1将执行
UserController.doSomething();
Button2 will do MainController.doSomeOtherThings();
Button2将执行
MainController.doSomeOtherThings();
Thats the way you use it ;) 那就是您使用它的方式;)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.