Swing編程基本框架

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);
        });
    }

    }




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章