本文介绍了在 Java 中使用 Swing 在 GUI 开发中正确实现 MVC 模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我来自MVC的大PHP背景,当我开始使用PHP时,我浏览了很多次,尽我所能并在PHP中完善我的MVC-Like设计.很多人喜欢回答对我有很大帮助的答案.

Firstly, I come from a big PHP background with MVC, when I started with PHP, I browsed a lot of times, to try my best and perfect my MVC-Like design in PHP. A lot of people like answered with answers which helped me a lot.

但是在Swing开始GUI开发之后,关于MVC在Swing中的答案就完全不同了.例如,模型也是视图?根据 Oracle 的建议 TextElementModel 不'这里没有任何逻辑业务,它所做的只是标记(设置颜色等)和设置数据(例如设置文本等).当我用 PHP 开发时,没有 AbstractModel 这样的东西,因为我总是被告知模型不是一个类,或者更多,它是处理逻辑业务的整个层.

But after starting GUI development in Swing, the answers about MVC in Swing are totally different. For instance, a model is also a view? According to Oracle's Suggestions TextElementModel doesn't have any logical business here, all it does is markup (set color etc) and setting data such as set text and so on. When I developed in PHP, there is no such thing as AbstractModel because I always got told that a model is not a class, or more, it's a whole layer that processes logical business.

在 PHP 中,我使用了服务、数据映射器和域对象,从这个惊人的答案中提出了建议,这对我在 PHP 中帮助很大:应该如何在 MVC 中构建模型?

In PHP I used Services, Data Mappers and Domain Objects, suggested from this amazing answer which helped me in PHP a lot: How should a model be structured in MVC?

再次阅读后,我想试一试并用Java做类似的事情:

After reading again, I thought to give it a go and do a similar thing in Java:

我有创建所有控制器的 ControllerContainer:

I have the ControllerContainer which creates all controllers:

public class ControllerContainer {

    private JFrame frame;

    public ControllerContainer(JFrame rune) {
        this.frame = frame;
    }

    public void initControllers() {
        Atest test = new Atest(frame);
        test.registerView("test", new ViewTest(test));
    }

}

如您所见,我将带有 ViewTest 实例的名为test"的视图添加到控制器中,现在它将在框架中可见,并且可以接受输入.

As you see, I add the view named "test" with instance of ViewTest to the controller, now it will be visible in the frame, and can take input.

我的 Controller 类,应该是抽象的,但我还没有把它抽象化:

My Controller class, which should be abstract, but I didn't make it abstract yet:

public class Controller {

    private JFrame frame;

    private Map<String, Component> views = new HashMap<String, Component>();

    public Controller(JFrame frame) {
        this.frame = frame;
    }

    protected void registerView(String title, Component c) {
        this.views.put(title, c);
        this.frame.add(c);
    }

    protected void deregisterView(String title) {
        this.frame.remove(this.views.get(title));
        this.views.remove(title);
    }

    protected Component getView(String title) {
        return this.views.get(title);
    }
}

还有一个测试控制器:

public class Atest extends Controller {

    public Atest(JFrame frame) {
        super(frame);
    }

    public void hit() {
        ((ViewTest) super.getView("test")).changeColorBlue();
    }
}

还有我的TestView:

public class ViewTest extends JPanel {

    private final Atest controller;

    public ViewTest(Atest c) {
        this.controller = c;
        setBackground(Color.RED);
        setLocation(0,0);
        setSize(300, 300);
        setLayout(null);

        JButton b = new JButton("hello");
        b.setSize(150, 150);
        b.setLocation(0,0);

        b.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent arg0) {
                controller.hit();
            }
            @Override
            public void mouseEntered(MouseEvent arg0) {
            }
            @Override
            public void mouseExited(MouseEvent arg0) {
            }
            @Override
            public void mousePressed(MouseEvent arg0) {
            }
            @Override
            public void mouseReleased(MouseEvent arg0) {
            }
        });

        add(b);
    }

    public void changeColorBlue() {
        setBackground(Color.BLUE);
    }

}

问题

如您所见,我的视图创建了一个新按钮,并向其添加了一个鼠标侦听器.侦听器将有权访问控制器以传递输入.控制器获取输入并更改视图.

The problem

As you see, my view creates a new button, and adds a mouse listener to it. the listener will have access to the controller to pass input. The controller gets the input and changes the view.

基本上控制器立即强制更新视图,没有任何严重的逻辑业务,因为在我的情况下不需要.

Basically the controller instantly forced to update the view, without any serious logical business because it's not needed in my case.

根据我在上面发布的链接,由 tereško 回答,我该如何使用他的想法 &正确使用 Java Swing 的建议?

According to the link I have posted above, answered by tereško, how can I use his idea & suggestions with Java Swing correctly?

在PHP背景之后,我真的很困惑.

I am really confused, after the PHP background.

也许我误解了,不同的语言应该做不同的事情?但我认为模式应该始终以相同的方式实现.

Maybe I am misunderstanding and everything should be done differently in different languages? But I thought that patterns should always be implemented the same way.

如果您需要更多信息,请告诉我.

If you need more information, let me know.

推荐答案

没有误会;模式只是应用不同.

There is no misunderstanding; the pattern is merely applied differently.

评论@ordous 以及@udalmik 的这个答案,Swing 应用程序可能有多个实现MVC 模式.如此处此处,并非每个交互都需要通过应用程序的控制器."相比之下,网络应用很可能在视图和控制器之间具有 1:1 的关系".

As noted in a comment by @ordous and this this answer by @udalmik, a Swing application may have multiple implementations of the MVC pattern. As noted here and here, "not every interaction needs to pass through your application's controller." In contrast, a web application may well "have a 1:1 relation between views and controllers."

Swing 可分离模型 架构,引用 ,将每个组件的视图和控制器部分折叠成一个 UI(用户界面)对象."Swing 控制器分散在 JComponent 的后代中,通常在组件的UI 委托.作为一个具体的例子,BasicButtonUI 包含一个 BasicButtonListener 处理用户鼠标交互.

The Swing separable model architecture, cited here, "collapses the view and controller parts of each component into a single UI (user-interface) object." Swing controllers are scattered among the descendants of JComponent, typically in the component's UI delegate. As a concrete example, BasicButtonUI contains a BasicButtonListener that handles user mouse interaction.

几乎使用了链接中的答案,但事实上他的控制器扩展了JPanel毁了它,让我完全困惑.

这可能会令人困惑,因为一个简单的 Swing 程序可能根本没有显式控制器.正如此大纲所建议的,控制器可以直接访问任何相关的视图模型;它还可以监听用户与视图的交互.示例旨在说明最简单的此类交互.这种效果是由用户与视图组件的交互引起的,这只是巧合.模拟引用了这里,例如,具有更新应用程序视图的视图组件的ControlPanel和模型.DisplayPanel 直接侦听需要模型更新的 ComponentEvent.等

This can be confusing, as a simple Swing program may have no explicit controller at all. As suggested in this outline, the controller has direct access to any relevant view and model; it may also listen to user interaction with the view. The example was intended to illustrate the simplest such interaction. It is mere coincidence that the effect is evoked by user interaction with a view component. The simulation cited here, for example, has a ControlPanel of view components that update the application's view and model. The DisplayPanel listens directly for a ComponentEvent that requires a model update. Etc.

然后您的应用程序的控制器可以自由地专注于应用程序的需求.

Your application's controller is then free to focus on the application's needs.

@Marco13 对此进行了详细说明,并在此相关答案中引用了其他示例.

@Marco13 elaborates on this and cites additional examples in this related answer.

这篇关于在 Java 中使用 Swing 在 GUI 开发中正确实现 MVC 模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 12:07