欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > > 文章正文

JavaSEmvc,

来源: javaer 分享于  点击 46781 次 点评:288

JavaSEmvc,


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

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

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

M

import 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);
}
}

}

V

import 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 model
public 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);
}
}

}


C
import 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() {

@Override
public 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;
}
}



相关文章

    暂无相关文章

用户点评