📄 fontdemo.java
字号:
import javax.swing.*;import java.awt.Font;import java.awt.FontMetrics;import java.awt.Color;import java.awt.Graphics;public class FontDemo extends JFrame { public FontDemo() { super("显示字体示例"); setSize(240, 200); this.getContentPane().add(new FontPanel()); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main(String[] args) { FontDemo fd = new FontDemo(); fd.setVisible(true); }}class FontPanel extends JPanel { public void paint(Graphics g) { super.paint(g); int x = 10, y = 20; String strFirst = "Welcome to"; String strSecond = " Java"; String strThird = "World"; String strNextLine = "Font example"; this.setBackground(Color.white); g.setColor(Color.black); g.setFont(new Font("宋体", Font.PLAIN, 20)); g.drawString(strFirst, x, y); FontMetrics fm = g.getFontMetrics(); x += fm.stringWidth(strFirst); g.setFont(new Font("Times New Roman",Font.PLAIN, 20)); g.setColor(Color.red); g.drawString(strSecond, x, y); g.setColor(Color.black); g.setFont(new Font("宋体", Font.PLAIN, 20)); x += fm.stringWidth(strSecond); g.drawString(strThird, x, y); y += fm.getHeight(); x = 10; g.drawString(strNextLine, x, y); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -