⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 maincanvas1.java.bak

📁 这是《Java 2 简明教程(第2版)》一书配套的源代码。
💻 BAK
字号:
import java.awt.*;
class MainCanvas1 extends Frame {
    MainCanvas cv = new MainCanvas();
    Choice choice = new Choice();

    MainCanvas1() {
        super("drawString Example");
        
        for (int i=4; i<60; i += 4) {
            choice.addItem(""+i);
        }
        choice.select(0);
        cv.setFontSize(4);
        cv.setSize(300, 100);
        add("Center", cv);
        add("South", choice);
        pack();
        show();
    }

    public boolean action(Event evt, Object what) {
        if (evt.target ==  choice) {
            cv.setFontSize(Integer.parseInt((String)what));
            return true;
        }
        return false;
    }

    static public void main(String[] args) {
        new MainCanvas1();
    }
}

class MainCanvas extends Canvas {
    void setFontSize(int size) {
        Font f = getFont();

        if (f == null) {
            f = new Font("TimesRoman", Font.PLAIN, size);
        } else {
            f = new Font(getFont().getName(), getFont().getStyle(), size);
        }
        setFont(f);
        repaint();
    }

    public void paint(Graphics g) {
        String s = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz";
        FontMetrics fontM = g.getFontMetrics();

        g.setColor(Color.white);
        g.fillRect(0, 0, fontM.stringWidth(s), fontM.getHeight());
        g.setColor(Color.black);
        g.drawString(s, 0, fontM.getAscent());
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -