2011-09-01 3 views
10

저는 OS X에서 Windows로 스윙 앱을 포팅하기 시작했습니다. JLabel으로 문제가 발생했습니다.JLabel html 텍스트가 setFont를 무시합니다

setFont으로 지정된 글꼴은 라벨 텍스트가 HTML 인 경우 무시됩니다 (Mac에서는 발생하지 않음). HTML 형식은 복잡한 디스플레이에서 가독성을 높이기 위해 매우 유용합니다.

정상적인 상황에서 HTML 태그에 글꼴을 지정 하겠지만 사용중인 글꼴은 런타임시 Font.createFont을 사용하여 JAR 밖으로 ttf로로드됩니다. 로드 된 글꼴의 이름을 글꼴 태그에서 사용하려고했지만 작동하지 않습니다.

로드 된 awt.Font을 Windows에서 html-ified JLabel과 함께 사용할 수있는 방법이 있습니까?

다음은 예입니다. 내 응용 프로그램의 글꼴을 공유 할 수 없습니다,하지만 난 그냥이 하나 (순수 TTF)과 같은 행동을 실행 발생합니다

http://www.dafont.com/sophomore-yearbook.font

import java.awt.Font; 
import java.io.File; 
import javax.swing.*; 

public class LabelTestFrame extends JFrame { 

     public LabelTestFrame() throws Exception { 
       boolean useHtml = true; 
       String fontPath = "C:\\test\\test_font.ttf"; 
       JLabel testLabel = new JLabel(); 
       Font testFont = Font.createFont(Font.TRUETYPE_FONT, new File(fontPath)).deriveFont(18f); 
       testLabel.setFont(testFont); 
       if (useHtml) testLabel.setText("<html>Some HTML'd text</html>"); 
       else testLabel.setText("Some plaintext"); 
       getContentPane().add(testLabel); 
       setSize(300,300); 
     } 

     public static void main(String[] args) { 
       SwingUtilities.invokeLater(new Runnable() { 
         @Override 
         public void run() { 
           try {new LabelTestFrame().setVisible(true);} 
           catch (Exception e) {e.printStackTrace();} 
         } 
       }); 
     } 

} 

편집 : 흥미롭게도, 내가 중 하나를 사용하는 경우 JRE의 lib/fonts 폴더에있는 ttf (이 경우 Lucida 글꼴 중 하나가 test_java.ttf로 이름이 바뀜)이 스 니펫은 boolean on 및 off와 동일한 결과를 생성합니다.

public LabelTestFrame() throws Exception { 
    boolean useHtml = false; 
    String fontPath = "C:\\test\\test_java.ttf"; 
    JLabel testLabel = new JLabel(); 
    Font testFont = Font.createFont(Font.TRUETYPE_FONT, new File(fontPath)).deriveFont(18f); 
    testLabel.setFont(testFont); 
    if (useHtml) testLabel.setText("<html><b>Some HTML'd text</b></html>"); 
    else testLabel.setText("Some plaintext"); 
    getContentPane().add(testLabel); 
    setSize(300,300); 
} 

public static void main(String[] args) { 
    SwingUtilities.invokeLater(new Runnable() { 
     @Override 
     public void run() { 
      try {new LabelTestFrame().setVisible(true);} 
      catch (Exception e) {e.printStackTrace();} 
     } 
    }); 
} 

편집 2 : JLabel의 글꼴 기본 설정을위한 여기서 설명하는 방법은 정확히 같은 문제가 (일반 텍스트가 잘 보여줍니다 html'd 텍스트는하지 않습니다) : Changing default JLabel font

편집 3 : 제가했습니다 dafont의 무작위 글꼴조차도 시스템에 설치되어있는 경우 작동합니다 (파일에서 [now installed] ttf의 사본을로드하는 정확한 코드로도).

+0

[sscce] (http://www.sscce.org)를 포함시킬 수 있습니까? 그 전까지는 [Swing Components에서 HTML을 사용하는 방법] (http://download.oracle.com/javase/tutorial/uiswing/components/html.html) 자습서를 읽어보십시오. – mre

+0

'Font.createFont'에 문제가있을 확률이 높습니다.'Jlabel'의'setFont()'는 폰트를 설정하는 것을 보장합니다 - @mre가 암시 하듯이, 예제가 더 나은 대답을하는데 도움이 될 것입니다. –

+0

내가 setText ("example") JLabel에로드 된 글꼴을 보여 주지만 setText (" example") 기본 Swing JLabel 글꼴이 사용되기 때문에 Font.createFont가 작동하는 것을 알고 있습니다. 이것은 카운트로 간주됩니까? –

답변

12

를 본 무엇 registerFont()

런타임시 JRE에 .ttf을 복사 할 수 있다면 Googling하는 동안이 작은 보석을 발견했습니다. 그것은 정확히 무엇을 해야하는지 않습니다. 런타임에 글꼴을로드 할 수 Font.createFont를 사용하는 경우, 단지 수행

GraphicsEnvironment.getLocalGraphicsEnvironment().registerFont(myCreatedFont)

는 JRE에 등록 할 수 있습니다.

이렇게하면 글꼴이 HTML 텍스트와 Windows의 일반 텍스트에 나타날 수 있습니다!

+0

+1 ['createFont)'] (http://download.oracle.com/javase/6/docs/api/java/awt/Font.html#createFont%28int,%20java.io.InputStream%29)에는이 점이 언급되어 있지만 약간 막연한. – trashgod

+0

Java 6 javadoc에서만 언급합니다. 1.4의 javadoc을 먼저 보여줘서 Google을 망 쳤어! –

+0

Google에서 Snoracle Shuffle을 비난 할 수는 없습니다! :-) – trashgod

4

은 참고로 여기

비교함으로써

enter image description here

여기 우분투 (10)상의 디스플레이의 MAC OS의 X.에 오픈 JDK 6

enter image description here

import java.awt.Font; 
import java.awt.GridLayout; 
import java.io.File; 
import javax.swing.*; 

public class LabelTestFrame extends JFrame { 

    public LabelTestFrame() throws Exception { 
     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     this.setLayout(new GridLayout(0, 1)); 
     String fontPath = "SophomoreYearbook.ttf"; 
     Font testFont = Font.createFont(
      Font.TRUETYPE_FONT, new File(fontPath)).deriveFont(18f); 
     JLabel label1 = new JLabel("<html>Some HTML'd text</html>"); 
     label1.setFont(testFont); 
     this.add(label1); 
     JLabel label2 = new JLabel("Some plaintext"); 
     this.add(label2); 
     this.pack(); 
     this.setLocationRelativeTo(null); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       try { 
        new LabelTestFrame().setVisible(true); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 
} 
+0

예, OS X은 예상대로 작동합니다. –

+0

나는 쉬운 측면 비교가 빛을 발할 수 있기를 바랬다. _any_'.ttf' 파일이 Windows에서 작동합니까? – trashgod

+0

예, 내장 (이미 JRE가 설치되어 있음) Lucida ttf는 저에게 옳은 일을합니다. 리눅스가 당신을 위해 속보를 일으키고있는 것처럼 보입니다 (Sun Java 또는 OpenJDK입니까?). –