text.java

来自「Java 入门书的源码」· Java 代码 · 共 52 行

JAVA
52
字号
//Copyright (c) 1998, Arthur Gittleman
//This example is provided WITHOUT ANY WARRANTY either expressed or implied.

/* Draws the different fonts, trying all the 
 * styles, and using various point sizes.  Draws
 * all text relative to the applet's size.  Uses
 * the init method to compute quantities that 
 * don't change.
 */

import java.awt.*;
import java.applet.Applet;

public class Text extends Applet {
  private Font serifBold24;
  private Font sansSerifItalic14;
  private Font monoPlain18;
  private Font dialogBI20;
  private Font DIPlain18;
  private String serif;       //  The string we want to center
  private int serifStart;     //  The x position for the centered string
  private int serifWide;      //  The width of the centered string. 

  public void init() {
    serifBold24 = new Font("Serif",Font.BOLD,24);
    sansSerifItalic14 = new Font("SansSerif",Font.ITALIC,14);
    monoPlain18 = new Font("Monospaced",Font.PLAIN,18);
    dialogBI20 = new Font("Dialog",Font.BOLD+Font.ITALIC,20);
    DIPlain18 = new Font("DialogInput",Font.PLAIN,18);
    setFont(serifBold24);
    FontMetrics metrics = getFontMetrics(serifBold24);
    serif = serifBold24.getName(); 
    serifWide = metrics.stringWidth(serif);
  }
  public void paint(Graphics g) {
    Dimension d = getSize();
    int w = d.width;                 //  The width of the applet   
    int h = d.height;                //  The height of the applet  
    int serifStart = (w-serifWide)/2;
    int otherStart = w/4;             
    g.drawString(serif,serifStart,h/6);
    g.setFont(sansSerifItalic14);
    g.drawString(sansSerifItalic14.getName(),otherStart,2*h/6);
    g.setFont(monoPlain18); 
    g.drawString(monoPlain18.getName(),otherStart,3*h/6);
    g.setFont(dialogBI20);
    g.drawString(dialogBI20.getName(),otherStart,4*h/6);
    g.setFont(DIPlain18);
    g.drawString(DIPlain18.getName(),otherStart,5*h/6);
  }
}
   

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?