设计模式

模板方法模式(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");
	}

}

输出:








原文地址:https://www.cnblogs.com/bhlsheji/p/5093052.html