返回列表 發帖

Font 類別

本帖最後由 tonyh 於 2019-9-7 14:57 編輯

試使用 setFont() 方法與 setForeground() 方法,設定各式各樣的字體與顏色。

[字體設定]
Font.PLAIN  無設定
Font.BOLD  粗體
Font.ITALIC  斜體
Font.BOLD+Font.ITALIC  粗體加斜體

[常見字型]
中文:標楷體、微軟正黑體、新細明體
英文:Times New Roman、Arial、Calibri

  1. import javax.swing.JFrame;
  2. import javax.swing.JLabel;
  3. import java.awt.GridLayout;
  4. import javax.swing.border.EtchedBorder;
  5. import java.awt.Color;
  6. import java.awt.Font;

  7. public class Ch119 {
  8.        
  9.         Font ft[]=new Font[6];
  10.         JLabel lb[]=new JLabel[6];
  11.         JFrame fm;

  12.         Ch119()
  13.         {
  14.                 ft[0]=new Font("標楷體", Font.BOLD, 50);
  15.                 ft[1]=new Font("微軟正黑體", Font.BOLD+Font.ITALIC, 40);
  16.                 ft[2]=new Font("新細明體", Font.ITALIC, 60);
  17.                 ft[3]=new Font("Times New Roman", Font.PLAIN, 40);
  18.                 ft[4]=new Font("Arial", Font.ITALIC, 60);
  19.                 ft[5]=new Font("BlacklightD", Font.PLAIN, 80);
  20.                
  21.                 for(int i=0; i<6; i++)
  22.                 {
  23.                     if(i<3)
  24.                             lb[i]=new JLabel("你好",JLabel.CENTER);
  25.                     else
  26.                             lb[i]=new JLabel("Hello",JLabel.CENTER);
  27.                     lb[i].setBorder(new EtchedBorder());
  28.                     lb[i].setFont(ft[i]);
  29.                 }
  30.                 lb[0].setForeground(Color.BLUE);
  31.                 lb[2].setForeground(Color.RED);
  32.                 lb[4].setForeground(Color.GRAY);
  33.                
  34.                 fm=new JFrame("Font 類別");
  35.                 fm.setBounds(100, 100, 600, 300);
  36.                 fm.setVisible(true);
  37.                 fm.setResizable(true);
  38.                 fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  39.                 fm.setLayout(new GridLayout(2,3));
  40.                 for(int i=0; i<6; i++)
  41.                         fm.add(lb[i]);
  42.         }
  43.        
  44.         public static void main(String[] args) {
  45.                 new Ch119();
  46.         }
  47. }
複製代碼

  1. import javax.swing.JFrame;
  2. import javax.swing.JLabel;
  3. import java.awt.GridLayout;
  4. import javax.swing.border.EtchedBorder;
  5. import java.awt.Color;
  6. import java.awt.Font;

  7. public class Ch106 {
  8.         
  9.         Font ft[]=new Font[6];
  10.         JLabel lb[]=new JLabel[6];
  11.         JFrame fm;

  12.         Ch106()
  13.         {
  14.                 ft[0]=new Font("標楷體", Font.BOLD, 50);
  15.                 ft[1]=new Font("微軟正黑體", Font.BOLD+Font.ITALIC, 40);
  16.                 ft[2]=new Font("新細明體", Font.ITALIC, 60);
  17.                 ft[3]=new Font("Times New Roman", Font.PLAIN, 40);
  18.                 ft[4]=new Font("Arial", Font.ITALIC, 60);
  19.                 ft[5]=new Font("BlacklightD", Font.PLAIN, 80);
  20.                
  21.                 for(int i=0; i<6; i++)
  22.                 {
  23.                     if(i<3)
  24.                             lb[i]=new JLabel("你好",JLabel.CENTER);
  25.                     else
  26.                             lb[i]=new JLabel("Hello",JLabel.CENTER);
  27.                     lb[i].setBorder(new EtchedBorder());
  28.                     lb[i].setFont(ft[i]);
  29.                 }
  30.                 lb[0].setForeground(Color.BLUE);
  31.                 lb[2].setForeground(Color.RED);
  32.                 lb[4].setForeground(Color.GRAY);
  33.                
  34.                 fm=new JFrame("Font 類別");
  35.                 fm.setBounds(100, 100, 600, 300);
  36.                 fm.setVisible(true);
  37.                 fm.setResizable(true);
  38.                 fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  39.                 fm.setLayout(new GridLayout(2,3));
  40.                 for(int i=0; i<6; i++)
  41.                         fm.add(lb[i]);
  42.         }
  43.         
  44.         public static void main(String[] args) {
  45.                 new Ch106();
  46.         }
  47. }
複製代碼
EEEEEEEEEEEEELLLLLLLLLLLLLIIIIIIIIIIIII

TOP

  1. import javax.swing.JFrame;
  2. import javax.swing.JLabel;
  3. import java.awt.GridLayout;
  4. import javax.swing.border.EtchedBorder;
  5. import java.awt.Color;
  6. import java.awt.Font;

  7. public class Ch119 {
  8.         
  9.         Font ft[]=new Font[6];
  10.         JLabel lb[]=new JLabel[6];
  11.         JFrame fm;

  12.         Ch119()
  13.         {
  14.                 ft[0]=new Font("標楷體", Font.BOLD, 50);
  15.                 ft[1]=new Font("微軟正黑體", Font.BOLD+Font.ITALIC, 40);
  16.                 ft[2]=new Font("新細明體", Font.ITALIC, 60);
  17.                 ft[3]=new Font("Times New Roman", Font.PLAIN, 40);
  18.                 ft[4]=new Font("Arial", Font.ITALIC, 60);
  19.                 ft[5]=new Font("BlacklightD", Font.PLAIN, 80);
  20.                
  21.                 for(int i=0; i<6; i++)
  22.                 {
  23.                     if(i<3)
  24.                             lb[i]=new JLabel("你好",JLabel.CENTER);
  25.                     else
  26.                             lb[i]=new JLabel("Hello",JLabel.CENTER);
  27.                     lb[i].setBorder(new EtchedBorder());
  28.                     lb[i].setFont(ft[i]);
  29.                 }
  30.                 lb[0].setForeground(Color.BLUE);
  31.                 lb[2].setForeground(Color.RED);
  32.                 lb[4].setForeground(Color.GRAY);
  33.                
  34.                 fm=new JFrame("Font 類別");
  35.                 fm.setBounds(100, 100, 600, 300);
  36.                 fm.setVisible(true);
  37.                 fm.setResizable(true);
  38.                 fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  39.                 fm.setLayout(new GridLayout(2,3));
  40.                 for(int i=0; i<6; i++)
  41.                         fm.add(lb[i]);
  42.         }
  43.         
  44.         public static void main(String[] args) {
  45.                 new Ch119();
  46.         }
  47. }
複製代碼

TOP

  1. import javax.swing.JFrame;
  2. import javax.swing.JLabel;
  3. import java.awt.GridLayout;
  4. import javax.swing.border.EtchedBorder;
  5. import java.awt.Color;
  6. import java.awt.Font;

  7. public class Ch119 {
  8.         
  9.         Font ft[]=new Font[6];
  10.         JLabel lb[]=new JLabel[6];
  11.         JFrame fm;

  12.         Ch119()
  13.         {
  14.                 ft[0]=new Font("標楷體", Font.BOLD, 50);
  15.                 ft[1]=new Font("微軟正黑體", Font.BOLD+Font.ITALIC, 40);
  16.                 ft[2]=new Font("新細明體", Font.ITALIC, 60);
  17.                 ft[3]=new Font("Times New Roman", Font.PLAIN, 40);
  18.                 ft[4]=new Font("Arial", Font.ITALIC, 60);
  19.                 ft[5]=new Font("BlacklightD", Font.PLAIN, 80);
  20.                
  21.                 for(int i=0; i<6; i++)
  22.                 {
  23.                     if(i<3)
  24.                             lb[i]=new JLabel("你好",JLabel.CENTER);
  25.                     else
  26.                             lb[i]=new JLabel("Hello",JLabel.CENTER);
  27.                     lb[i].setBorder(new EtchedBorder());
  28.                     lb[i].setFont(ft[i]);
  29.                 }
  30.                 lb[0].setForeground(Color.BLUE);
  31.                 lb[2].setForeground(Color.RED);
  32.                 lb[4].setForeground(Color.GRAY);
  33.                
  34.                 fm=new JFrame("Font 類別");
  35.                 fm.setBounds(100, 100, 600, 300);
  36.                 fm.setVisible(true);
  37.                 fm.setResizable(true);
  38.                 fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  39.                 fm.setLayout(new GridLayout(2,3));
  40.                 for(int i=0; i<6; i++)
  41.                         fm.add(lb[i]);
  42.         }
  43.         
  44.         public static void main(String[] args) {
  45.                 new Ch119();
  46.         }
  47. }
複製代碼

TOP

  1. import javax.swing.JFrame;
  2. import javax.swing.JLabel;
  3. import java.awt.GridLayout;
  4. import javax.swing.border.EtchedBorder;
  5. import java.awt.Color;
  6. import java.awt.Font;

  7. public class Ch110 {
  8.         
  9.         Font ft[]=new Font[6];
  10.         JLabel lb[]=new JLabel[6];
  11.         JFrame fm;

  12.         Ch110()
  13.         {
  14.                 ft[0]=new Font("標楷體", Font.BOLD, 50);
  15.                 ft[1]=new Font("微軟正黑體", Font.BOLD+Font.ITALIC, 40);
  16.                 ft[2]=new Font("新細明體", Font.ITALIC, 60);
  17.                 ft[3]=new Font("Times New Roman", Font.PLAIN, 40);
  18.                 ft[4]=new Font("Arial", Font.ITALIC, 60);
  19.                 ft[5]=new Font("BlacklightD", Font.PLAIN, 80);
  20.                
  21.                 for(int i=0; i<6; i++)
  22.                 {
  23.                     if(i<3)
  24.                             lb[i]=new JLabel("你好",JLabel.CENTER);
  25.                     else
  26.                             lb[i]=new JLabel("Hello",JLabel.CENTER);
  27.                     lb[i].setBorder(new EtchedBorder());
  28.                     lb[i].setFont(ft[i]);
  29.                 }
  30.                 lb[0].setForeground(Color.BLUE);
  31.                 lb[2].setForeground(Color.RED);
  32.                 lb[4].setForeground(Color.GRAY);
  33.                
  34.                 fm=new JFrame("Font 類別");
  35.                 fm.setBounds(100, 100, 600, 300);
  36.                 fm.setVisible(true);
  37.                 fm.setResizable(true);
  38.                 fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  39.                 fm.setLayout(new GridLayout(2,3));
  40.                 for(int i=0; i<6; i++)
  41.                         fm.add(lb[i]);
  42.         }
  43.         
  44.         public static void main(String[] args) {
  45.                 new Ch110();
  46.         }
  47. }
複製代碼

TOP

  1. import javax.swing.JFrame;
  2. import javax.swing.JLabel;
  3. import java.awt.GridLayout;
  4. import javax.swing.border.EtchedBorder;
  5. import java.awt.Color;
  6. import java.awt.Font;

  7. public class Ch119 {
  8.         
  9.         Font ft[]=new Font[6];
  10.         JLabel lb[]=new JLabel[6];
  11.         JFrame fm;

  12.         Ch119()
  13.         {
  14.                 ft[0]=new Font("標楷體", Font.BOLD, 50);
  15.                 ft[1]=new Font("微軟正黑體", Font.BOLD+Font.ITALIC, 40);
  16.                 ft[2]=new Font("新細明體", Font.ITALIC, 60);
  17.                 ft[3]=new Font("Times New Roman", Font.PLAIN, 40);
  18.                 ft[4]=new Font("Arial", Font.ITALIC, 60);
  19.                 ft[5]=new Font("BlacklightD", Font.PLAIN, 80);
  20.                
  21.                 for(int i=0; i<6; i++)
  22.                 {
  23.                     if(i<3)
  24.                             lb[i]=new JLabel("你好",JLabel.CENTER);
  25.                     else
  26.                             lb[i]=new JLabel("Hello",JLabel.CENTER);
  27.                     lb[i].setBorder(new EtchedBorder());
  28.                     lb[i].setFont(ft[i]);
  29.                 }
  30.                 lb[0].setForeground(Color.BLUE);
  31.                 lb[2].setForeground(Color.RED);
  32.                 lb[4].setForeground(Color.GRAY);
  33.                
  34.                 fm=new JFrame("Font 類別");
  35.                 fm.setBounds(100, 100, 600, 300);
  36.                 fm.setVisible(true);
  37.                 fm.setResizable(true);
  38.                 fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  39.                 fm.setLayout(new GridLayout(2,3));
  40.                 for(int i=0; i<6; i++)
  41.                         fm.add(lb[i]);
  42.         }
  43.         
  44.         public static void main(String[] args) {
  45.                 new Ch119();
  46.         }
  47. }
複製代碼

TOP

返回列表