Java问题

逛青楼的道长 2016-10-22 06:14:21
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);


}
}
为什么运行后没画出字符串的?
...全文
137 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

62,612

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧