2014-01-10 22 views
3

現在,我知道,在JButton文本中可以實現不同的字體系列。 Like belowJButton文本具有不同的CUSTOM字體

JButton button = new JButton("<html><font face=Arial>Hello </font><font face=Verdana>World</font></html>"); 

看起來像這樣。與Arial合作的「Hello」與Verdana合作的「World」。

enter image description here

但是,如果我想一個字有我一直在使用Font.createFont()方法創建的字體是什麼。我認爲,像這樣的東西本來可以奏效。

Font myFont = createMyFont(); 
JButton button = new JButton("<html><font face=MyFont>Hello </font>World</html>"); 

這個問題的意義在於,我創建一個多語種的軟件,它在一個單一的JButton兩種字體。

所以,我希望我的JButton是這樣的:

enter image description here

但是,它是這樣的:

enter image description here

+0

的Javadoc提到使用'Font.registerFont()'創建使用'Font.createFont()的字體後' –

+0

嘗試註冊的字體: 'GraphicsEnvironment中GE = GraphicsEnvironment.getLocalGraphicsEnvironment(); \t ge.registerFont(myFont);' –

+0

然後,我應該叫什麼名字?用它的文件名? – Akshat

回答

2

註冊自定義字體有:

GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment() 
ge.registerFont(myFont); 

After ,提供文件名,這樣的:

 URL fontUrl; 
     try { 
      fontUrl = new URL("http://www.webpagepublicity.com/" + 
        "free-fonts/a/Airacobra%20Condensed.ttf"); // dummy font 

      Font myFont = Font.createFont(Font.TRUETYPE_FONT, fontUrl.openStream()); 
      myFont = myFont.deriveFont(Font.PLAIN,20); 
      GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); 
      ge.registerFont(myFont); 


      button.setText("<html><font face='Airacobra Condensed'>Hello </font>World</html>"); 

     } catch (MalformedURLException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } catch (FontFormatException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

可以在Windows通過rightclicking上的字體文件,選擇屬性accquired - >詳細信息,有它的標題。例如:FontAwesome Regular。

enter image description here

相關問題