Swing基本框架
package test;
import java.awt.*;
import java.awt.font.*;
import java.awt.geom.*;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
/**
* @version 1.33 2007-04-14
* @author Cay Horstmann
*/
public class FontTest {
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
Logger.getLogger(FontTest.class.getName()).log(Level.SEVERE, null, ex);
}
JFrame frame = new JFrame();
frame.setTitle("FontTest");
frame.setSize(800, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new JComponent() {
@Override
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
String message = "Hello, World!";
Font f = new Font("Serif", Font.BOLD, 36);
g2.setFont(f);
//g2.drawString("aaaa", 100, 100);
try {
f=Font.createFont(Font.TRUETYPE_FONT, new File("甜蜜记忆体.ttf"));
} catch (FontFormatException | IOException ex) {
Logger.getLogger(FontTest.class.getName()).log(Level.SEVERE, null, ex);
}
f=f.deriveFont(Font.BOLD,40F);
g2.setFont(f);
g2.drawString("伟大的时代", 100, 100);
}
});
frame.setVisible (true);
});
}
}