如何避免在Java Swing MVC中编写样板代码?

时间:2014-10-02 03:47:23

标签: java swing model-view-controller windowbuilder boilerplate

我已按照此示例创建了一个简单的Swing MVC应用程序:https://stackoverflow.com/a/16850977/1966873

我正在使用Eclipse Luna和WindowBuilder。

每次向模型添加新字段时都必须复制很多代码,例如

public void setName(String name) {
            String old = this.name;
            this.name = name;

            spcs.firePropertyChange("name", old, name);
        }

@Override
        public void propertyChange(PropertyChangeEvent evt) {
            String propName = evt.getPropertyName();
            Object newVal = evt.getNewValue();

            if("name".equalsIgnoreCase(propName)){
                view.getLblTest().setText((String)newVal);
            }
        }

每当我向模型添加新字段时,有没有办法自动生成这些?或者是否有另一种方法可以实现我想要的,即将模型绑定到视图。

我根据该答案创建的完整示例应用程序:

Model.java

public class Model {
    private SwingPropertyChangeSupport spcs;

    private String name;

    public Model(){
        spcs = new SwingPropertyChangeSupport(this);
    }

    public void addPropertyChangeListener(PropertyChangeListener listener){
        spcs.addPropertyChangeListener(listener);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        String old = this.name;
        this.name = name;

        spcs.firePropertyChange("name", old, name);
    }
}

View.java

public class View {
    private JFrame frame;
    private JPanel panel;
    private JLabel lblTest;
    private JButton btnClickMe;

    public View() {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                initialize();
                frame.setVisible(true);
            }
        });
    }

    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(new BorderLayout(0, 0));
        frame.getContentPane().add(getPanel(), BorderLayout.CENTER);
    }
    public JPanel getPanel() {
        if (panel == null) {
            panel = new JPanel();
            panel.setLayout(new MigLayout("", "[][][][][][][][][]", "[][][]"));
            panel.add(getBtnClickMe(), "cell 6 2");
            panel.add(getLblTest(), "cell 8 2");
        }
        return panel;
    }
    public JLabel getLblTest() {
        if (lblTest == null) {
            lblTest = new JLabel("Test");
        }
        return lblTest;
    }
    public JButton getBtnClickMe() {
        if (btnClickMe == null) {
            btnClickMe = new JButton("Click me");
        }
        return btnClickMe;
    }
}

Controller.java

public class Controller implements PropertyChangeListener{
    private Model model;
    private View view;

    public Controller(Model model, View view){
        this.model = model;
        this.view = view;

        this.model.addPropertyChangeListener(this);

        setUpViewEvents();
    }

    private void setUpViewEvents(){
        view.getBtnClickMe().addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                model.setName("John Smith");
            }
        });
    }

    @Override
    public void propertyChange(PropertyChangeEvent evt) {
        String propName = evt.getPropertyName();
        Object newVal = evt.getNewValue();

        if("name".equalsIgnoreCase(propName)){
            view.getLblTest().setText((String)newVal);
        }
    }
}

Main.java启动程序

public class Main {
    public static void main(String[] args) {
        Model model = new Model();
        View view = new View();

        Controller controller = new Controller(model, view);
    }
}

0 个答案:

没有答案