solong.java

来自「21天学通JAVA2(第三版)(专业参考版)」· Java 代码 · 共 35 行

JAVA
35
字号
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SoLong extends JFrame {
    public SoLong() {
        super("So Long");
        setSize(425, 150);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        SoLongPane sl = new SoLongPane();
        Container content = getContentPane();
        content.add(sl);
        setVisible(true);
    }

    public static void main(String[] arguments) {
        SoLong frame = new SoLong();
    }

}

class SoLongPane extends JPanel {
    public void paintComponent(Graphics comp) {
        Graphics comp2D = (Graphics2D)comp;
        Font f = new Font("Monospaced", Font.BOLD, 18);
        FontMetrics fm = getFontMetrics(f);
        comp2D.setFont(f);
        String s = "So long, and thanks for all the fish.";
        int x = (getSize().width - fm.stringWidth(s)) / 2;
        int y = getSize().height / 2;
        comp2D.drawString(s, x, y);
    }
}

⌨️ 快捷键说明

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