所以我使用Observer / Observable方法掌握了java中的基本MVC模式。现在为了保持它的清洁和可读性,我想在我继续讨论如何很好地组织我的视图之前有一些指示,因为这是我的课程最充满的地方。我们在学校被告知要保持每个类的文件大小低于20kb以保持可读性,以后更容易维护。
以下是我的观点:
package view;
import model.*;
import helper.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Observable;
import java.util.Observer;
import net.miginfocom.swing.MigLayout;
public class View extends JFrame implements Observer
{
private Model model;
private JPanel left = new JPanel(new MigLayout());
private JPanel center = new JPanel(new MigLayout());
private JPanel right = new JPanel(new MigLayout());
private void setConstraints()
{
this.left.setMinimumSize(new Dimension(252, 540));
this.left.setMaximumSize(new Dimension(252, 37500));
this.center.setMinimumSize(new Dimension(298, 540));
this.right.setMinimumSize(new Dimension(250, 540));
this.right.setMaximumSize(new Dimension(250, 37500));
}
//Left panel contents
private Towers box = new Towers();
private Modules tree = new Modules();
private JPanel setupLeft()
{
this.left.add(this.box, "growx, pushx, wrap");
this.left.add(new JScrollPane(this.tree), "grow, push");
return this.left;
}
//Center panel contents
private Browser browser = new Browser();
private JPanel setupCenter()
{
this.center.add(new JScrollPane(this.browser), "grow, push");
return this.center;
}
//Right panel contents
private JLabel tower = new JLabel("No tower selected.");
private JLabel cap = new JLabel("Capacitor");
private JLabel cpu = new JLabel("CPU");
private JLabel shield = new JLabel("0");
private JLabel armor = new JLabel("0");
private JLabel em = new JLabel("0.0");
private JLabel th = new JLabel("0.0");
private JLabel kn = new JLabel("0.0");
private JLabel ex = new JLabel("0.0");
private JPanel setupRight()
{
this.right.add(this.tower, "span, wrap");
this.right.add(this.cap, "span, wrap");
this.right.add(this.cpu, "span, wrap");
this.right.add(this.shield, "span, wrap");
this.right.add(this.armor, "span, wrap");
this.right.add(this.em, "span, wrap");
this.right.add(this.th, "span, wrap");
this.right.add(this.kn, "span, wrap");
this.right.add(this.ex, "span, wrap");
return this.right;
}
public View(Model ui_model)
{
model = ui_model;
this.setTitle("MVC Experiment 6");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setExtendedState(JFrame.MAXIMIZED_BOTH);
this.setMinimumSize(new Dimension(800, 600));
this.setLayout(new MigLayout());
this.setConstraints();
this.add(this.setupLeft(), "dock west");
this.add(this.setupCenter(), "dock center");
this.add(this.setupRight(), "dock east");
}
//Left panel contents - Listeners and methods for addressing JComponents
public void xTowersBrowser(ActionListener event)
{
this.box.addActionListener(event);
}
public void xModulesBrowser(MouseListener event)
{
this.tree.addMouseListener(event);
}
public Towers getTowersBrowser()
{
return this.box;
}
public Modules getModulesBrowser()
{
return this.tree;
}
//Left panel - END
//Center panel - components :: listeners and methods
public void xBrowser(MouseListener event)
{
this.browser.addMouseListener(event);
}
public Browser getBrowser()
{
return this.browser;
}
//Center panel - END
public void update(Observable o, Object arg)
{
}
}
有关在新类中分隔内容或如何最小化代码的任何建议都很有帮助。这只是我的主要View类的一个缺失,仍然有很多JComponents丢失,所以它会变得更加混乱。
答案 0 :(得分:2)
我借此机会展示我理想的MVC创意。
组件之间的连线可以受益于班级EventHandler
,http://docs.oracle.com/javase/6/docs/api/java/beans/EventHandler.html的简洁。
让我们将GUI限制为java swing。
要集成MVC,我需要一个可能由子模型组成的模型。人们可以拥有一个抽象的视图类,可能由子视图组成。这个抽象视图类是所有swing组件/子视图的工厂,由控制器注入模型和绑定。
在GUI构建器中,可以添加JTextField a
作为自定义创建代码view.a.create()
。在initComponents之后,可以调用view.checkRequiredCreated()
,这可能会对缺失的创建产生信息性异常。