public 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);
}
public static void main(String args[]){
Fonts2Panel fo=new Fonts2Panel();
fo.setVisible(true);
}
} 帮忙看看这段代码,怎么结果显示不出来?哪错了????
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);
}
public static void main(String args[]){
Fonts2Panel fo=new Fonts2Panel();
fo.setVisible(true);
}
} 帮忙看看这段代码,怎么结果显示不出来?哪错了????
f.getContextPane().add(fo);
f.show();
import java.awt.FontMetrics;
import java.awt.Graphics;import javax.swing.*;
public 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);
}
public static void main(String args[]){
Fonts2Panel fo=new Fonts2Panel();
fo.setVisible(true);
JFrame ff=new JFrame();
ff.getContentPane().add(fo);
ff.setSize(500,500);
ff.setVisible(true);
}
忘删了