?? fontdemo.java
字號:
import javax.swing.*;
import java.awt.Font;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.FontMetrics;
public class FontDemo extends JFrame {
public FontDemo() {
super("顯示字體示例");
setSize(240,200);
getContentPane().add(new FontPanel());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
FontDemo frame = new FontDemo();
frame.show();
}
}
class FontPanel extends JPanel{
public void paint(Graphics g){
super.paint(g);
int x=10;
int y=20;
//要顯示的字體
String strFirst="歡迎到";
String strSecond="Java";
String strThird="的世界";
String strNextLine="字體示例";
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);
//為字符串strSecond設置新字體和顏色
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 + -