fontcolor.java

来自「《Java程序设计与应用》-张仕斌-源程序 《Java程序设计与应用》-张仕斌」· Java 代码 · 共 37 行

JAVA
37
字号
//FontColor.java
import java.awt.*;
import javax.swing.*;

public class FontColor extends JFrame {
	Color[] colors = null;
	String[] fontNames = null;
	
	public FontColor() {
		super("Font Color Test");
		colors = new Color[2];
		colors[0] = new Color(255,0,0); //红色
		colors[1] = new Color(0,0,255); //蓝色
		
		GraphicsEnvironment gl = GraphicsEnvironment.getLocalGraphicsEnvironment();
		fontNames = gl.getAvailableFontFamilyNames(); //取得所有可用字体名称
	}
	
	public void paint(Graphics g) {
		Font newFont = null;
		for (int i = 0; i < fontNames.length && i < 10 ; i++ ) {
			newFont = new Font(fontNames[i],Font.PLAIN,18); //生成字体对象
			g.setFont(newFont);           //设置当前字体
			g.setColor(colors[ i % 2]);   //设置当前颜色
			g.drawString(fontNames[i] , 10 , 50 + i * 21); 
		}
	}
	
	public static void main(String[]args) {
		FontColor fc = new FontColor();
		fc.setSize(500,400);
		fc.setDefaultCloseOperation(EXIT_ON_CLOSE);
		fc.setVisible(true);
	}
}
			
	

⌨️ 快捷键说明

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