根据Java中的Model-View-Controller创建视图

时间:2015-04-18 10:32:15

标签: java model-view-controller

我有一个包含部门名称列表的应用程序 - 当用户点击其中一个显示其详细信息的JPanel时。我在我的模型专用类中加载了一个Department对象,但是将整个对象传递到自定义DepartmentDetailsPanel构造函数并从中提取数据(例如名称,用户成员列表等等)是不好的做法?

public class DepartmentDetailsPanel extends JPanel {

    private ClassLoader cl = this.getClass().getClassLoader();
    private JPanel iconAndName = new JPanel();
    private JLabel icon;
    private JLabel name;

    private JPanel body = new JPanel();

    private JPanel basicInfo = new JPanel();
    private JLabel headLabel = new JLabel(Constants.head_name_lable);
    private JLabel managerLabel = new JLabel(Constants.manager_name_label);
    private JLabel typeLabel = new JLabel(Constants.type_label);

    private JLabel head;
    private JLabel manager;
    private JLabel type;

    private JPanel membersPanel = new JPanel();
    private JLabel usersLabel = new JLabel(Constants.members_label);
    private JList members;
    private JPanel memberButtons = new JPanel();
    private JButton removeButton = new JButton(Constants.remove);
    private JButton addButton = new JButton(Constants.add);

    public DepartmentDetailsPanel(Department d) {
        setLayout(new BorderLayout());
        setBorder(new EmptyBorder(10, 10, 10, 10) );

        iconAndName.setLayout(new FlowLayout(FlowLayout.LEFT));
        int dType = d.getType();
        if (dType == 1)
            icon = new JLabel(new ImageIcon(cl.getResource("leader.png")));
        if (dType == 2)
            icon = new JLabel(new ImageIcon(cl.getResource("creative.png")));
        if (dType == 3) 
            icon = new JLabel(new ImageIcon(cl.getResource("finance.png")));

        name = new JLabel(d.getName());
        iconAndName.add(icon);
        iconAndName.add(name);
        add(iconAndName, BorderLayout.NORTH);

        basicInfo.setLayout(new GridLayout(3, 2));
        basicInfo.add(typeLabel);
        type = new JLabel(Constants.department_types[dType-1]);
        basicInfo.add(type);
        basicInfo.add(headLabel);
        head = new JLabel(d.getHeadName());
        basicInfo.add(head);
        basicInfo.add(managerLabel);
        manager = new JLabel(d.getManagerName());
        basicInfo.add(manager);
        body.setLayout(new BoxLayout(body, BoxLayout.Y_AXIS));
        body.add(basicInfo);

        membersPanel.setLayout(new BorderLayout());
        membersPanel.add(usersLabel, BorderLayout.NORTH);
        JComponent memberList = makeList(extractMembers(d), members);
        membersPanel.add(memberList, BorderLayout.CENTER);
        memberButtons.setLayout(new BoxLayout(memberButtons, BoxLayout.Y_AXIS));
        memberButtons.add(addButton);
        memberButtons.add(removeButton);
        membersPanel.add(memberButtons, BorderLayout.EAST);
        body.add(membersPanel);
        add(body, BorderLayout.CENTER);
    }

    private JComponent makeList(String[] data, JList list) {
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(1, 1));
        panel.setBackground(Constants.lightGrey);
        if (data == null){
            JLabel message = new JLabel(Constants.nothing_to_view);
            panel.add(message);
        } else {
            list = new JList(data);
            list.setVisibleRowCount(10);
            list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
            list.setLayoutOrientation(JList.VERTICAL); 
            list.setBackground(Constants.lightGrey);
            panel.add(new JScrollPane(list));
        }
        return panel;
    }

    private String[] extractMembers(Department d){
        String[] members = null;
        List<User> list = d.getMembers();
        if (list != null || list.size()==0){
            members = new String[list.size()];
            int i = 0;
            for (User u: list){
                members[i] = u.getName() +" "+ u.getLastname();
                i++;
            }
        }
        return members;
    }

}

2 个答案:

答案 0 :(得分:2)

只要你没有做任何事情,除了获得字段值,它就没问题。视图来使用模型,如何才能显示模型?

答案 1 :(得分:0)

您应该注意对象和数据结构之间的区别。 数据结构是仅具有getter和setter的类的实例。对象更智能,因为它们进行一些计算或一些处理。在这里找到更多http://blog.8thlight.com/uncle-bob/2013/10/01/Dance-You-Imps.html

到目前为止,MVC已成为最令人困惑的模式之一,因为它不再用于最初为其创建的目的。

MVC模式中的视图层基本上有两个角色:表示数据和接收用户操作。数据表示意味着以某种方式保持视图状态和模型状态。例如,即使我不喜欢这种方法,也可以将模型数据传递给视图。但是您可以使用其他机制。例如,您可以使用在视图和模型上更新数据状态的绑定技术。但最重要的是要保留一种方法在所有应用程序中进行数据表示,尤其是在您与团队合作时。 您可以在此处阅读有关GUI架构的更多信息: http://martinfowler.com/eaaDev/uiArchs.html