JAVA中不论是触发事件都是以类和接口实现的,因而比起C++来有更具有面向对象.................
呵呵,不过还没有接触事件模型,但是对于事件模型应该是理解了.....
java 代码
- /*
- * FontTest.java
- *
- * Created on 2007年9月16日, 下午7:25
- *
- * To change this template, choose Tools | Template Manager
- * and open the template in the editor.
- */
- package visualswt;
- import java.awt.*;
- import java.awt.font.*;
- import java.awt.geom.*;
- import javax.swing.*;
- /**
- *
- * @author 龙斌
- */
- public class FontTest {
- /** Creates a new instance of FontTest */
- public static void main(String[] args) {
- FontFrame frame=new FontFrame();
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.show();
- }
- }
- class FontFrame extends JFrame
- {
- public FontFrame(){
- setTitle("字体测试的用户界面程序");
- setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
- FontPanel panel=new FontPanel();
- Container contentPane=getContentPane();
- contentPane.add(panel);
- }
- public static final int DEFAULT_WIDTH=500;
- public static final int DEFAULT_HEIGHT=200;
- }
- class FontPanel extends JPanel
- {
- public void paintComponent(Graphics g){
- super.paintComponent(g);
- Graphics2D g2=(Graphics2D)g;
- String message="我也写个用户界面";
- Font f=new Font("华文行草",Font.BOLD,50);
- g2.setFont(f);
- FontRenderContext context=g2.getFontRenderContext();
- Rectangle2D bounds=f.getStringBounds(message,context);
- double x=(getWidth()-bounds.getWidth())/2;
- double y=(getHeight()-bounds.getHeight())/2;
- double ascent=-bounds.getY();
- double baseY=y+ascent;
- g2.drawString(message,(int)x,(int)baseY);
- g2.setPaint(Color.RED);
- g2.draw(new Line2D.Double(x,baseY,(x+bounds.getWidth()),baseY));
- Rectangle2D rect=new Rectangle2D.Double(x,y,bounds.getWidth(),bounds.getHeight());
- g2.draw(rect);
- }
- }