更多精彩请到 http://www.139ya.com
终于要还机器了,3年的MOTO生涯算是告一段落,这是在MOTO写的最后一篇blog,算是个纪念吧。
4 classic reads, newly available on Google Books
8 hours ago
更多精彩尽在139ya网址导航 www.139ya.com
package com.sitinspring;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
/** *//**
* 控制类
*
* @author sitinspring(junglesong@gmail.com)
*
* @date 2007-11-5
*/
public class Mvc1Ctrl {
private Mvc1View view;
private Mvc1Model model;
public Mvc1Ctrl() {
view = new Mvc1View();
model = new Mvc1Model();
model.addObserver(view);
handleEvents();
}
// 处理事件响应
private void handleEvents() {
addCloseLintener();
addButtonListener();
addButtonListener2();
}
// 窗体关闭事件相应
private void addCloseLintener() {
view.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.out.println("Exit MVC1");
System.exit(0);
}
});
}
private void addButtonListener() {
view.getButton().addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
model.showText();
}
});
}
private void addButtonListener2() {
view.getButton2().addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
model.showText2();
}
});
}
}
package com.sitinspring;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.util.Observable;
import java.util.Observer;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
/** *//**
* 视图类(View)
*
* @author sitinspring(junglesong@gmail.com)
*
* @date 2007-11-5
*/
public class Mvc1View extends JFrame implements Observer {
private static final long serialVersionUID = 621145935910133202L;
private JButton button;
private JLabel label;
private JButton button2;
private JLabel label2;
public Mvc1View() {
locateView(300, 200);
this.setTitle("MVC1 Program");
setupComponents();
this.setVisible(true);
}
// 当模块更新时,此函数会被调用
public void update(Observable o, Object arg) {
Mvc1Model model = (Mvc1Model) o;
if (model.getUpdateState().equals(Mvc1UpdateState.UpdateLabel)) {
label.setText(model.getResponseText());
} else if (model.getUpdateState().equals(Mvc1UpdateState.UpdateLabel2)) {
label2.setText(model.getResponseText());
}
}
// 定位程序在屏幕正中并设置程序大小
private void locateView(int width, int height) {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
this.setSize(width, height);
this.setLocation(screenSize.width / 2 - width / 2, screenSize.height
/ 2 - height / 2);
}
// 初始化内部组件
private void setupComponents() {
button = new JButton("点击响应事件1");
label = new JLabel(" 等待事件响应1");
button2 = new JButton("点击响应事件2");
label2 = new JLabel(" 等待事件响应2");
setLayout(new GridLayout(2, 2));
add(button);
add(label);
add(button2);
add(label2);
}
public JButton getButton() {
return button;
}
public JButton getButton2() {
return button2;
}
}
package com.sitinspring;
import java.util.Observable;
/** *//**
* 模块类
* @author sitinspring(junglesong@gmail.com)
*
* @date 2007-11-5
*/
public class Mvc1Model extends Observable{
private String responseText;
private String updateState;
// 用于通知View更新,此函数被调用后View的update函数会被调用
private void notifyView(){
setChanged();
notifyObservers();
}
public void showText(){
updateState=Mvc1UpdateState.UpdateLabel;
responseText=" 事件1响应完毕";
notifyView();
}
public void showText2(){
updateState=Mvc1UpdateState.UpdateLabel2;
responseText=" 事件2响应完毕";
notifyView();
}
public String getResponseText() {
return responseText;
}
public String getUpdateState() {
return updateState;
}
}
package com.sitinspring;
public class Mvc1UpdateState{
public static final String UpdateLabel="updateLabel";
public static final String UpdateLabel2="updateLabel2";
}
package com.sitinspring;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
/** *//**
* 控制类
*
* @author sitinspring(junglesong@gmail.com)
*
* @date 2007-11-5
*/
public class Mvc2Ctrl {
private Mvc2View view;
private Mvc2Model model;
public Mvc2Ctrl() {
view = new Mvc2View();
model = new Mvc2Model();
handleEvents();
}
// 处理事件响应
private void handleEvents() {
addCloseLintener();
addButtonListener();
addButtonListener2();
}
// 窗体关闭事件相应
private void addCloseLintener() {
view.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.out.println("Exit MVC2");
System.exit(0);
}
});
}
private void addButtonListener() {
view.getButton().addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
view.getLabel().setText(model.getText());
}
});
}
private void addButtonListener2() {
view.getButton2().addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
view.getLabel2().setText(model.getText2());
}
});
}
}
package com.sitinspring;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
/** *//**
* 视图类(View)
*
* @author sitinspring(junglesong@gmail.com)
*
* @date 2007-11-5
*/
public class Mvc2View extends JFrame {
private static final long serialVersionUID = 621145935910133202L;
private JButton button;
private JLabel label;
private JButton button2;
private JLabel label2;
public Mvc2View() {
locateView(300, 200);
this.setTitle("MVC2 Program");
setupComponents();
this.setVisible(true);
}
// 定位程序在屏幕正中并设置程序大小
private void locateView(int width, int height) {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
this.setSize(width, height);
this.setLocation(screenSize.width / 2 - width / 2, screenSize.height
/ 2 - height / 2);
}
// 初始化内部组件
private void setupComponents() {
button = new JButton("点击响应事件1");
label = new JLabel(" 等待事件响应1");
button2 = new JButton("点击响应事件2");
label2 = new JLabel(" 等待事件响应2");
setLayout(new GridLayout(2, 2));
add(button);
add(label);
add(button2);
add(label2);
}
public JButton getButton() {
return button;
}
public JButton getButton2() {
return button2;
}
public JLabel getLabel() {
return label;
}
public JLabel getLabel2() {
return label2;
}
}
package com.sitinspring;
/** *//**
* 模块类
* @author sitinspring(junglesong@gmail.com)
*
* @date 2007-11-5
*/
public class Mvc2Model{
public String getText(){
return " 事件1响应完毕";
}
public String getText2(){
return " 事件2响应完毕";
}
}