使用NetBeans GUI构建器使用Java MVC方法进行编程 [英] Programming with a Java MVC approach using NetBeans GUI builder

查看:123
本文介绍了使用NetBeans GUI构建器使用Java MVC方法进行编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的任务是创建一个GUI,它基本上需要一些用户输入,并对正在运行程序的机器可访问的各种驱动器上进行一些文件夹/文件操作。在设计这个GUI时,我开始意识到MVC会让我的生活变得更轻松,而其他任何人决定修改代码,但我无法真正看到如何通过NetBeans实现这一点。

I've been tasked with making a GUI that essentially takes a bit of user input and does some folder/file manipulation on various drives accessible by the machine the program is being run on. While designing this GUI, I'm starting to realize that MVC will make my life much easier and anyone else who decides to modify code, but I can't really see how this can be done via NetBeans.

我已经对这个主题做了一些阅读,我无法真正看到关于是否可以在NetBeans上完成这一切的明确答案。当然,如果我以编程方式构建GUI,就可以完成,但这有点违背了我选择使用NetBeans的目的。

I've done a bit of reading up on this topic, and I can't really see any clear cut answers as to whether or not this can be done on NetBeans. Surely it can be done if I programmatically build the GUI, but that somewhat defeats the purpose of why I chose to use NetBeans.

推荐答案

Netbeans可以做到这一点。

Netbeans is fine to do this.

要实现的关键是,尽管所有基本的Swing组件都是MVC,但大多数情况下你不会以这种方式与它们进行交互。一个简单的文本字段有内部模型,但该模型不是您的模型,文本字段更原始。

The key thing to realize is that while all of the basic Swing components are MVC, for the most part you don't interact with them that way. A simple text field has it internal model, but that model isn't your model, the text field is more a primitive.

您的模型处理更高级别的事件(按钮)动作和什么不是),而不是按下按钮和箭头移动和鼠标点击。

Your model deals with higher level events (button actions and what not), rather than button presses and arrow moves and mouse clicks.

因此,对于高级MVC,主要的通信机制是通过PropertyChangeListeners。构建应用程序的基本任务是将各种数据元素的PCL及其GUI组件连接在一起。

So, for high level MVC, the primary mechanism of communication is through PropertyChangeListeners. And the basic task of building your app up is wiring the PCLs of the assorted data elements along with their GUI components together.

例如,一个简单的例子就是你有一个物品清单。并且该列表通过JTable在屏幕上呈现,并且该表位于JPanel上。

For example, a simple case is you have a list of items. And that list is rendered on the screen via a JTable, and that table is on a JPanel.

您的列表具有自己的模型,即它不仅仅是Java列表。它不是List,因为标准Java列表不支持PCL通知。但是你的Model显然会包含这样一个List。

Your list has it's own model, i.e. it's not simply a Java List. It's not a List because standard Java Lists don't support PCL notifications. But your Model would obviously wrap such a List.

现在,接下来的问题是如何将JTable连接到你的List模型。

Now, the next question is how do you wire up JTable to be associated with your List model.

一,您可以将JTable子类化并将其绑定到您的模型。或者,更简单地说,您使用JTable作为基元,并让封闭的JPanel管理模型和JTable之间的交互。

One, you could subclass JTable and bind it to your Model. Or, more simply, you use the JTable as a primitive, and let the enclosing JPanel manage the interaction between your Model and the JTable.

这意味着让您的JPanel实现PropertyChangeListener然后,当连接所有内容时,你会做这样的事情:

That means having your JPanel implement PropertyChangeListener, and then, when wiring everything up, you do something like this:

ListModel myModel = new ListModel();
ListPanel myPanel = new ListPanel();
myModel.addPropertyChangeListener(myPanel);

现在,无论何时更改ListModel,都会通知ListPanel。

Now, whenever your ListModel is changed, is will notify the ListPanel.

在你的ListPanel上你可以有类似的东西:

On your ListPanel you can having something like:

@Override
public void propertyChange(PropertyChangeEvent evt) {
    if (evt.getPropertyName().equals(ListModel.CHANGED)) {
        ListModel model = (ListModel) evt.getSource();
        DefaultTableModel tm = (DefaultTableModel) listTable.getModel();
        tm.setRowCount(0);
        for (String s : model.getList()) {
            tm.addRow(new Object[]{s});
        }
    }
}

现在,你可以看到这个只需重新加载整个表模型,但您可以根据需要将属性更改为细粒度。您还可以看到,如果这是其他模型(如Person或其他),您可以在面板上填充单个文本字段和诸如此类的内容。

Now, you can see this simply reloads the entire table model, but you can make your property changes as fine grained as you want. You can also see that if this was some other model (like a Person or something) you can populate individual text fields and whatnot on the panel.

这非常简单GUI,但它显示了这一切如何连接在一起的基础。我认为在Swing示例中有一点丢失,这对于一个面板屏幕非常有用,但在开始添加其他视图时根本无法扩展。

This is a pretty simple GUI, but it shows the fundamentals of how this all wires together. I think a bit of this is lost in the Swing examples which are great for one panel screens but don't scale at all when you start adding other views.

您的JPanels基本上成为VC的组合,因为你的GUI增加了复杂性,你可以将这些事情考虑在内,但它对于合理数量的屏幕等非常有效。

Your JPanels basically become combined VC, as your GUI gains complexity you can factor those kinds of things out, but its works pretty well for reasonable amounts of screens and such.

这篇关于使用NetBeans GUI构建器使用Java MVC方法进行编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆