【发布时间】:2013-10-27 19:05:13
【问题描述】:
似乎 JTextPane 和 JTextArea 以不同的方式呈现字体。它几乎不引人注意,但我仍然想知道它为什么会在那里。
我已经设置了一个 SSCCE,但你看不到它。最好的方法是使用 JTextArea 运行一个程序,然后将代码更改为 JTextPane 并再次运行它。它们应该以某种方式重叠,当您从一个窗口切换到另一个窗口时(使用 alt+ 选项卡),可以看到差异。您必须提供自己的 *.ttf 文件。
这是为什么呢?有没有办法强制 JTextPane 以与 JTextArea 相同的方式呈现文本?
SSCCE:
import java.awt.Font;
import java.awt.FontFormatException;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.io.IOException;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextPane;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.UnsupportedLookAndFeelException;
public class Main
{
public static void main(String[] args)
{
for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels())
{
if ("Nimbus".equals(info.getName()))
{
try
{
UIManager.setLookAndFeel(info.getClassName());
}
catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e)
{
System.out.println("No Nimbus!");
}
break;
}
}
JFrame a = new JFrame("Test");
a.setSize(600, 900);
a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
a.getContentPane().setLayout(new BoxLayout(a.getContentPane(), BoxLayout.Y_AXIS));
Font d = null;
try
{
d = Font.createFont(Font.TRUETYPE_FONT, Main.class.getResourceAsStream("calibri_bold.ttf"));
d = d.deriveFont(23f);
}
catch (FontFormatException | IOException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
final JTextPane b = new JTextPane();
b.setBorder(new JTextArea().getBorder());
b.setFont(d);
b.addFocusListener(new FocusListener() {
@Override
public void focusGained(FocusEvent arg0) {
b.repaint();
}
@Override
public void focusLost(FocusEvent arg0) {
b.repaint();
}
});
b.setText("It seems that JTextPane and JTextArea are rendering fonts differently. It is barely noticeable, but I still want to know why is it there. I have set up a SSCCE, but you can't really see it. Best method would be to run a program with JTextArea, then change the code to JTextPane and run it again. They should overlap in a way when you change from one window to the other (with alt+ tab) the difference can be seen. Why is that? Is there a way to force the JTextPane to render the text the same way as JTextArea does?");
a.getContentPane().add(b);
final JTextArea c = new JTextArea();
c.setFont(d);
c.setText("It seems that JTextPane and JTextArea are rendering fonts differently. It is barely noticeable, but I still want to know why is it there. I have set up a SSCCE, but you can't really see it. Best method would be to run a program with JTextArea, then change the code to JTextPane and run it again. They should overlap in a way when you change from one window to the other (with alt+ tab) the difference can be seen. Why is that? Is there a way to force the JTextPane to render the text the same way as JTextArea does?");
c.setLineWrap(true);
c.setWrapStyleWord(true);
a.getContentPane().add(c);
a.setVisible(true);
}
}
【问题讨论】:
-
You will have to provide your own *.ttf file.- 当您使用默认字体时会发生什么?您的 .ttf 文件有问题吗? -
为什么会有问题?这是一个文件,它们都应该显示相同的...
-
就在我的脑海中,有一些字体的渲染提示。 IE JTextPane 可能会使用带有浮点度量的 Graphics2D,而 JTextArea 可能不会。
-
Why would there be a problem with it?- 它应该为任何字体呈现相同的内容,那么为什么坚持我们使用自定义 .ttf 文件来使 SSCCE 复杂化呢?我一个人没有访问权限,所以我无法运行您的 SSCCE。提出问题的目的是在问题中仅包含相关信息。您还需要自己解决一些基本问题。
标签: java swing jtextarea jtextpane nimbus