爲什麼setFont不能正常工作?我想繪製消息(字體粗體,大小爲20),並在其下面帶有小藍色數字的乘法表。如果setFont不按這個順序工作
//this is a separate class
public class Start {
public static void main(String[] args){
GUI gui = new GUI();
}
}
public class GUI extends JFrame{
public GUI(){
add(new DrawTable());
setTitle("Multiplication table");
setSize(240, 280);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setResizable(false);
}
}
class DrawTable extends JPanel{
public void paintComponent(Graphics g){
super.paintComponent(g);
this.setBackground(Color.WHITE);
g.setColor(Color.BLUE);
g.setFont(new Font("Times", Font.PLAIN, 11));
for(int i = 1, j = 110; i < 10; i++, j += 15){
g.drawString("" + i, 20, j);
}
for(int i = 1, j = 50; i < 10; i++, j += 20){
g.drawString("" + i, j, 80);
}
for(int i = 1, j = 110; i < 10; i++, j += 15){
for(int k = 1, l = 50; k < 10 ; k++, l += 20){
if((i * k) < 10){
g.drawString("" + i *k , l, j);
}else{
g.drawString("" + i * k, l - 6, j);
}
}
}
//those are the lines im talking about
setFont(new Font("font1", Font.BOLD, 20));
FontMetrics fm = g.getFontMetrics();
int x = (getWidth()/2) - (fm.stringWidth("Multiplication table"))/2;
g.drawString("Multiplication table", x, 50);
}
}
這僅適用於我把這些四行super.paintComponent方法(g)根據,然後 該消息是黑色,粗體和20以及數字小和藍色,但如果我把4條 線像這裏一樣,它的全部是小而藍,爲什麼?
什麼是「font1」的方式?你的系統是否有這種字體? –