简体   繁体   中英

How can i clean and organize up my View (MVC) better

So i got the grip on the basic MVC patterns in java using Observer / Observable method. Now in interest of keeping it clean and readable i would like some pointers before i move on regarding how to well organize my View as this is where my Classes get most filled up. We been told in school to keep file size per class bellow 20kb to keep it readable and later easier maintainable.

Here is my view:

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)
    {

    }
}

Any suggestions on what to separate in new classes or how to minimize the code are helpful. This is just a cut out of my main View class there are still a lot of JComponents missing so it will get more messy.

I take this opportunity to present my ideal MVC ideas.

The wiring between the components can benefit from the brevity of the class EventHandler , http://docs.oracle.com/javase/6/docs/api/java/beans/EventHandler.html .

Let's constrain the GUI to java swing.

  • GUI Builders, especially if they generate form files, preferably in XML or Java FX, might be feasible, as they provide a pure hierarchical organisation. A canonical ordering.
  • One may create components (JPanel, MyJTextPane), to compose views and predefine styling properties.

To integrate MVC one has a model, possibly composed of submodels. And one could have an abstract view class, possibly composed of subviews. This abstract view class is the factory of all swing components / subviews, with the injection of the model and the binding, by the controller.

In the GUI builder one can add a JTextField a with as custom creation code view.a.create() . After initComponents one can call view.checkRequiredCreated() which might throw an informative exception on missing creations.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM