texto html @JLabel ignora setFont
Acabei de começar a portar meu aplicativo Swing do OS X para o Windows e as coisas são dolorosas comJLabel
s.
Eu notei que a fonte especificada parasetFont
é ignorado se o texto do rótulo for HTML (isso não acontece no Mac). A formatação HTML é EXTREMAMENTE útil para facilitar a leitura em telas complicada
Em circunstâncias normais, eu especificaria a fonte em uma tag HTML, mas a fonte que estou usando é carregada em tempo de execução usandoFont.createFont
com um ttf fora do JAR. Tentei usar o nome da fonte carregada na tag font, mas isso não funciono
Existe alguma maneira de usar umawt.Font
com um html-ifiedJLabel
no Windows?
Aqui está um exemplo. Não consigo compartilhar a fonte do meu aplicativo, mas apenas o executei (um TTF puro) e o mesmo comportamento acontece:
http: //www.dafont.com/sophomore-yearbook.fon
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();}
}
});
}
}
EDIT: curiosamente, se eu usar um dos ttf's da pasta lib / fonts do JRE (neste caso, uma das fontes Lucida aqui renomeadas para test_java.ttf), esse trecho produzirá resultados idênticos com o booleano ativado e desativad
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();}
}
});
}
EDIT 2: O método descrito aqui para definir a fonte JLabel padrão tem exatamente o mesmo problema (o texto sem formatação mostra detalhes, o texto em HTML não):Alterando a fonte JLabel padrão
EDIT 3: Notei que mesmo fontes aleatórias do dafont funcionarão se estiverem instaladas no sistema (mesmo com esse código exato, onde eu carreguei uma cópia do [agora instalado] ttf de um arquivo).