码迷,mamicode.com
首页 > 其他好文 > 详细

设计模式 - 模板方法模式(template method pattern) JFrame 详解

时间:2014-06-22 21:36:47      阅读:283      评论:0      收藏:0      [点我收藏+]

标签:mystra   设计模式   模板方法模式   jframe   java   

模板方法模式(template method pattern) JFrame 详解


本文地址: http://blog.csdn.net/caroline_wendy


参考模板方法模式(template method pattern): http://blog.csdn.net/caroline_wendy/article/details/32159455


模板方法模式(template method pattern), Java的JFrame使用模板方法模式, paint()是可以覆盖的方法, 

覆盖paint()方法, 可以定制JFrame的显示画面.


代码:

/**
 * @time 2014年6月20日
 */
package template_method.jframe;

import java.awt.Graphics;

import javax.swing.JFrame;

/**
 * @author C.L.Wang
 *
 */
public class MyFrame extends JFrame {

	public MyFrame(String title) {
		// TODO Auto-generated constructor stub
		super(title);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		this.setSize(300, 300);
		this.setVisible(true);
	}
	
	public void paint(Graphics graphics) {
		super.paint(graphics);
		String msg = "I am a girl! ";
		graphics.drawString(msg, 100, 100);
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		MyFrame myFrame = new MyFrame("Head First Design Patterns");
	}

}

输出:

bubuko.com,布布扣





bubuko.com,布布扣


设计模式 - 模板方法模式(template method pattern) JFrame 详解,布布扣,bubuko.com

设计模式 - 模板方法模式(template method pattern) JFrame 详解

标签:mystra   设计模式   模板方法模式   jframe   java   

原文地址:http://blog.csdn.net/caroline_wendy/article/details/32718135

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!