box.java

来自「corejava的一部分源码,与corejiavasource中的有区别。」· Java 代码 · 共 37 行

JAVA
37
字号
import java.awt.*;

class Box extends Panel {
	String title;

	public Box(Component c, String title) {
		this.title = title;		
		setLayout(new BorderLayout());
		add(c, "Center");
	}
	public Insets getInsets() {
		Graphics 	g 	= getGraphics();
		FontMetrics fm 	= g.getFontMetrics();

		g.dispose();
		return new Insets(fm.getHeight(),2,2,2);
	}
	public void paint(Graphics g) {
		Dimension 	sz 	= getSize();
		FontMetrics fm 	= g.getFontMetrics();
		int 		h 	= fm.getHeight();

		g.setColor(SystemColor.controlShadow);
		g.drawRect(0,h/2,sz.width-2,sz.height-2-h/2);
		g.setColor(SystemColor.controlLtHighlight);
		g.drawRect(1,h/2+1,sz.width-2,sz.height-2-h/2);

		g.setColor(getBackground());
		g.clearRect(sz.width/2 - fm.stringWidth(title)/2 - 2,
					0, fm.stringWidth(title)+4, fm.getHeight());

		g.setColor(getForeground());
		g.drawString(title,sz.width/2 - fm.stringWidth(title)/2,
					fm.getAscent());
	}
}

⌨️ 快捷键说明

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