Java问题
package test;
import javax.swing.*;
import java.awt.*;
public class TextCenterMessage extends JFrame{
public TextCenterMessage()
{
CenterMessage messagePanel = new CenterMessage();
add(messagePanel);
messagePanel.setBackground(Color.WHITE);
messagePanel.setFont(new Font("Californian FB",Font.BOLD,30));
}
public static void main(String[] args){
TextCenterMessage frame = new TextCenterMessage();
frame.setTitle("TextCenterMessage");
frame.setSize(300,150);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class CenterMessage extends JPanel{
protected void PaintComponent(Graphics g)
{
super.paintComponent(g);
FontMetrics fm = g.getFontMetrics();
int stringWidth = fm.stringWidth("Welcome to java");
int stringAscent = fm.getAscent();
int xCoordinate = getWidth() / 2 - stringWidth / 2;
int yCoordinate = getHeight() / 2 + stringAscent / 2;
g.drawString("Welcome to java", xCoordinate, yCoordinate);
}
}
为什么运行后没画出字符串的?