代码如下
/**
* @(#)CenterText.java
*
* CenterText application
*
* @author
* @version 1.00 2010/8/1
*/
import java.awt.*;
import javax.swing.*;
public class CenterText extends JFrame
{
public void paint( Graphics g )
{
String str = "Hello world!";
g.setFont( new Font( "Random" , Font.PLAIN ,24) );
FontMetrics metrics = g.getFontMetrics();
Dimension d = getSize() ;
g.setColor( getBackground() );
g.fillRect( 0, 0, d.width, d.height);
g.setColor(Color.black);
int x = ( d.width - metrics.stringWidth(str) );
int y = ( d.height + metrics.getHeight() );
g.drawString( str , x, y);
}
public static void main(String[] args)
{
CenterText ct = new CenterText();
ct.setSize(400,400);
ct.setVisible(true);
}
}
点解运行先得果窗口。唔字的呢。。。。搞咗成两个钟都稳唔出问题。。。囧~~~