【问题标题】:MVC Java Swing usageMVC Java Swing 用法
【发布时间】:2016-08-01 12:08:46
【问题描述】:

我想使用 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);
}}

主控制器:

public class MainController {

private MainView theView;
private MainModel theModel;

public MainController(MainView theView, MainModel theModel)
{
    this.theModel = theModel;
    this.theView = theView;

}}

主视图:

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);
}}

我没有放在这里,因为模型现在是空的。

好的,所以我不知道如何使用多个控制器。 例如:我想创建新的控制器 UserController,但我不知道如何实现它。我应该在 MainController 中创建新控制器吗? 如果我在 MainController 中创建 UserController 怎么用?

【问题讨论】:

标签: java swing model-view-controller


【解决方案1】:

只需在添加 MainController 的 Application 类中创建 UserController。

示例:如果您有 2 个按钮,则可以添加 actionlistener。

Button1 会做UserController.doSomething();

Button2 会做MainController.doSomeOtherThings();

这就是你使用它的方式;)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多