我想给你看一个小程序。这不是一个真正的计划。我写它只是为了演示。 以下是观点:
public class View implements Observer {
private Model model;
private IController controller;
private JLabel firstNumber = new JLabel();
private JLabel secondNumber = new JLabel();
private JLabel resultLabel = new JLabel();
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
Model model = new Model();
IController controller = new Controller();
View view = new View();
view.setModel(model);
view.setController(controller);
view.createAndShowGUI();
}
});
}
public void setModel(Model model) {
this.model = model;
model.addObserver(this);
}
public void setController(IController controller) {
this.controller = controller;
}
public void createAndShowGUI() {
JFrame frame = new JFrame("mvc train");
frame.setSize(400, 400);
frame.getContentPane().add(createMainPanel());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
private Component createMainPanel() {
JPanel mainPanel = new JPanel();
mainPanel.add(firstNumber);
mainPanel.add(secondNumber);
mainPanel.add(resultLabel);
JButton inc1 = new JButton("increment first");
inc1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
controller.execute("inc1", model);
}
});
JButton dec1 = new JButton("decrement first");
dec1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
controller.execute("dec1", model);
}
});
mainPanel.add(inc1);
mainPanel.add(dec1);
JButton inc2 = new JButton("increment second");
inc2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
controller.execute("inc2", model);
}
});
JButton dec2 = new JButton("decrement second");
dec2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
controller.execute("dec2", model);
}
});
mainPanel.add(inc2);
mainPanel.add(dec2);
return mainPanel;
}
@Override
public void update(Observable arg0, Object arg1) {
firstNumber.setText("" + model.getA());
secondNumber.setText("" + model.getB());
resultLabel.setText("" + (model.getA() + model.getB()));
}
}
看起来像这样:
按钮"先增加"增加左边的数字。按钮"递增第二个"递增中心号码。按钮"先减少"递减左边的数字。按钮"减少第二个"递减中心号码。正确的数字是添加左侧和中间数字的结果。 因此,在按下两次按钮后,首先增加"和一个按钮"增加第二个"我们有这张照片:
我在模特中存储数字。这是我的模特:
public class Model extends Observable {
private int a;
private int b;
public void incrementA() {
a++;
setChanged();
notifyObservers();
}
public void incrementB() {
b++;
setChanged();
notifyObservers();
}
public void decrementA() {
a--;
setChanged();
notifyObservers();
}
public void decrementB() {
b--;
setChanged();
notifyObservers();
}
public int getA() {
return a;
}
public int getB() {
return b;
}
@Override
public void addObserver(Observer observer) {
super.addObserver(observer);
setChanged();
notifyObservers();
}
}
这是我的控制器:
public class Controller implements IController {
@Override
public void execute(String action, Model model) {
switch (action) {
case "inc1":
model.incrementA();
break;
case "inc2":
model.incrementB();
break;
case "dec1":
model.decrementA();
break;
case "dec2":
model.decrementB();
}
}
}