저는 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의 사본을로드하는 정확한 코드로도).
[sscce] (http://www.sscce.org)를 포함시킬 수 있습니까? 그 전까지는 [Swing Components에서 HTML을 사용하는 방법] (http://download.oracle.com/javase/tutorial/uiswing/components/html.html) 자습서를 읽어보십시오. – mre
'Font.createFont'에 문제가있을 확률이 높습니다.'Jlabel'의'setFont()'는 폰트를 설정하는 것을 보장합니다 - @mre가 암시 하듯이, 예제가 더 나은 대답을하는데 도움이 될 것입니다. –
내가 setText ("example") JLabel에로드 된 글꼴을 보여 주지만 setText (" example") 기본 Swing JLabel 글꼴이 사용되기 때문에 Font.createFont가 작동하는 것을 알고 있습니다. 이것은 카운트로 간주됩니까? –