JavaAwtSwing笔记之 java.awt.Font静态属性所带的字体效果

254 阅读1分钟

查看Font静态属性自带的字体效果 , java1.6增加

Font的静态属性带了5中字体family的选择

  • Font.DIALOG
  • Font.DIALOG_INPUT
  • Font.MONOSPACED
  • Font.SANS_SERIF
  • Font.SERIF

源码👇

/*
     * Constants to be used for logical font family names.
     */

    /**
     * A String constant for the canonical family name of the
     * logical font "Dialog". It is useful in Font construction
     * to provide compile-time verification of the name.
     * @since 1.6
     */
    public static final String DIALOG = "Dialog";

    /**
     * A String constant for the canonical family name of the
     * logical font "DialogInput". It is useful in Font construction
     * to provide compile-time verification of the name.
     * @since 1.6
     */
    public static final String DIALOG_INPUT = "DialogInput";

    /**
     * A String constant for the canonical family name of the
     * logical font "SansSerif". It is useful in Font construction
     * to provide compile-time verification of the name.
     * @since 1.6
     */
    public static final String SANS_SERIF = "SansSerif";

    /**
     * A String constant for the canonical family name of the
     * logical font "Serif". It is useful in Font construction
     * to provide compile-time verification of the name.
     * @since 1.6
     */
    public static final String SERIF = "Serif";

    /**
     * A String constant for the canonical family name of the
     * logical font "Monospaced". It is useful in Font construction
     * to provide compile-time verification of the name.
     * @since 1.6
     */
    public static final String MONOSPACED = "Monospaced";

测试代码👇

package awt;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.LineBorder;

public class Font静态属性所带的字体测试2205301901 {
	
	static Frame frame = new Frame(Thread.currentThread().getStackTrace()[1].getClassName());
	static {
		frame.addWindowListener(new WindowAdapter() {@Override public void windowClosing(WindowEvent ev) {frame.dispose();System.exit(0);}});
		frame.setBounds(new Rectangle(100, 50, 1600, 900));
		frame.setLayout(new GridLayout(0, 1, 10, 10));
		
	}
	
	static void demoJlabelByFont(Font font, String str) {
		JLabel jLabel = new JLabel(str+"    汉字, English"); jLabel.setFont(font);
		jLabel.setBorder(new LineBorder(Color.BLUE, 3, true));
		jLabel.setHorizontalAlignment(JLabel.CENTER);
		frame.add(jLabel);
	}
	
	public static  void main(String...arguments) {
		Font font = null;
		font = new Font(Font.DIALOG, Font.PLAIN, 30); demoJlabelByFont(font, "font = new Font(Font.DIALOG, Font.PLAIN, 30);");
		font = new Font(Font.DIALOG_INPUT, Font.PLAIN, 30); demoJlabelByFont(font, "font = new Font(Font.DIALOG_INPUT, Font.PLAIN, 30);");
		font = new Font(Font.MONOSPACED, Font.PLAIN, 30); demoJlabelByFont(font, "font = new Font(Font.MONOSPACED, Font.PLAIN, 30);");
		font = new Font(Font.SANS_SERIF, Font.PLAIN, 30); demoJlabelByFont(font, "font = new Font(Font.SANS_SERIF, Font.PLAIN, 30);");
		font = new Font(Font.SERIF, Font.PLAIN, 30); demoJlabelByFont(font, "font = new Font(Font.SERIF, Font.PLAIN, 30);");
		
		frame.setVisible(true);
	}

}

在这里插入图片描述