package GuiGaoJi;import java.awt.*;
import javax.swing.*;
class Fonts2Panel extends JPanel {
public void paintComponent(Graphics g) {
String myquote="Happiness is an attitude.";
Font f=new Font("Times New Roman",Font.BOLD+Font.ITALIC,24);
g.setFont(f);
FontMetrics fm=getFontMetrics(f);
int x=(getSize().width - fm.stringWidth(myquote))/2;
int y=getSize().height/2;
g.drawString(myquote,x,y);
}
的主方法怎么写,应该重新实例什么对象??怎么让他输出来相应字体的字!
import javax.swing.*;
class Fonts2Panel extends JPanel {
public void paintComponent(Graphics g) {
String myquote="Happiness is an attitude.";
Font f=new Font("Times New Roman",Font.BOLD+Font.ITALIC,24);
g.setFont(f);
FontMetrics fm=getFontMetrics(f);
int x=(getSize().width - fm.stringWidth(myquote))/2;
int y=getSize().height/2;
g.drawString(myquote,x,y);
}
的主方法怎么写,应该重新实例什么对象??怎么让他输出来相应字体的字!
Fonts2Panel fo=new Fonts2Panel();
fo.setVisible(true);
}是的,可以这样写吗?