JavaSEmvc

来源:互联网 发布:oceanbase数据库免费 编辑:程序博客网 时间:2024/05/10 09:22

存储和处理数据的组件称为模型,包含组件的实际内容。

表示数据的组件称为视图,它处理组件所有必要的行为,完成组件的所有显示。

控件通常是用来获取数据的组件。

Mimport java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.util.ArrayList;public class CircleModel {private double radius = 10;// 填充属性private boolean filled;private java.awt.Color color;// 存放监听器ArrayList<ActionListener> actionListenerList;public double getRadius() {return radius;}public void setRadius(double radius) {this.radius = radius;//改变这个参数的时候会触发该事件processEvent(new ActionEvent(this, ActionEvent.ACTION_PERFORMED,“radius”));}public boolean isFilled() {return filled;}public void setFilled(boolean filled) {this.filled = filled;processEvent(new ActionEvent(this, ActionEvent.ACTION_PERFORMED,“filled”));}public java.awt.Color getColor() {return color;}public void setColor(java.awt.Color color) {this.color = color;processEvent(new ActionEvent(this, ActionEvent.ACTION_PERFORMED,“color”));}public synchronized void addActionListener(ActionListener l) {if (actionListenerList == null)actionListenerList = new ArrayList<ActionListener>();actionListenerList.add(l);}public synchronized void removeActionListener(ActionListener l) {if (actionListenerList != null && actionListenerList.contains(l))actionListenerList.remove(l);}/*** 根据ActionEvent参数来触发对应的事件—-** 事件的actionPerformed函数将被调用*/private void processEvent(ActionEvent e) {ArrayList list;//给代码块加锁,保证只有一个线程调用,保证了actionListenterList的数据安全synchronized (this) {if (actionListenerList == null)return;list = (ArrayList) actionListenerList.clone();}// 遍历程序中actionListenerList(为model注册的事件将添加进来)for(int i = 0; i<list.size(); i++){ActionListener listener = (ActionListener)list.get(i);listener.actionPerformed(e);}}}

Vimport java.awt.Graphics;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JPanel;public class CircleView extends JPanel implements ActionListener {private CircleModel model;@Override//继承了ActionListener接口public void actionPerformed(ActionEvent e) {// TODO 自动生成的方法存根repaint();}/t a modelpublic void setModel(CircleModel newmodel){model = newmodel;//注册一个监听器把view添加进去if(model!=null)model.addActionListener(this);repaint();}public CircleModel getModel(){return model;}public void paintComponent(Graphics g){super.paintComponent(g);if(model == null)return;g.setColor(model.getColor());int xCenter = getWidth()/2;int yCenter = getHeight()/2;int radius = (int)model.getRadius();if(model.isFilled()){g.fillOval(xCenter – radius, yCenter – radius, radius*2, radius*2);}else{g.drawOval(xCenter – radius, yCenter – radius, radius*2, radius*2);}}}


Cimport java.awt.BorderLayout;import java.awt.GridLayout;import java.awt.Panel;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JComboBox;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JTextField;public class CircleController extends JPanel {private CircleModel model;private JTextField jtfRadius = new JTextField();private JComboBox jcboFilled = new JComboBox(new Boolean[]{new Boolean(true),new Boolean(false)});public CircleController(){//JPanel panel1 = new JPanel();panel1.setLayout(new GridLayout(2, 1));panel1.add(new JLabel(“radius”));panel1.add(new JLabel(“filled”));//JPanel panel2 =new JPanel();panel2.setLayout(new GridLayout(2,1));panel2.add(jtfRadius);panel2.add(jcboFilled);setLayout(new BorderLayout());add(panel1, BorderLayout.WEST);add(panel2, BorderLayout.CENTER);//jtfRadius.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {if(model ==null) return;model.setFilled(((Boolean) jcboFilled.getSelectedItem()).booleanValue());}});}public void setModel(CircleModel newModel){model = newModel;}public CircleModel getModel(){return model;}}



0 0
原创粉丝点击