【问题标题】:Arial Unicode MS is not aligning in JtreeArial Unicode MS 未在 Jtree 中对齐
【发布时间】:2017-11-25 06:30:20
【问题描述】:

我正在编写一个 JTree,其中我正在对齐英语和 unicode 混合的文本。 我看到在使用 Arial Unicode MS 字体(它是 unicode 和等宽字体)后,文本没有对齐,因为等宽文本通常是对齐的。

我测试过的一种正确对齐的字体是“Consolas”,但由于它不是 Unicode,它不会显示不同语言的字符。

提到了一个示例代码以供参考: 我用过韩语:-)

package hello;

import java.awt.Font;

import javax.swing.JFrame;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.tree.DefaultMutableTreeNode;
public class Jtree_Test extends JFrame
{
    private static final long serialVersionUID = 1L;
    private JTree tree;
    public Jtree_Test()
    {
        //create the root node
        DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");

        //create the child nodes
        DefaultMutableTreeNode vegetableNode = new DefaultMutableTreeNode("Vegetables                           test1");
        DefaultMutableTreeNode fruitNode = new DefaultMutableTreeNode("test2                                 " + "안녕하세요, 당신은 어떠세요");
        //add the child nodes to the root node
        root.add(vegetableNode);
        root.add(fruitNode);

        //create the tree by passing in the root node
        tree = new JTree(root);
        tree.setFont(new Font("Arial Unicode MS", Font.PLAIN, 12));       
        getContentPane().add(tree);

        this.setSize(557, 349);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setTitle("JTree Example");       
        this.setVisible(true);
    }

    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Jtree_Test();
            }
        });
    }       
}

【问题讨论】:

  • this 是您希望看到的吗?
  • 在@AndrewThompson 的图片中,这种字体看起来根本不是等宽的(Vt 宽得多)。 “Arial Unicode MS”中的“MS”可能只是“Microsoft”,而不是“Monospaced”。
  • 不,安德鲁,这应该正确对齐。我有一张照片,但不知道如何在此处附上。
  • 您好,Thomas 感谢您的回复。是的,它不像你提到的那样是等宽的。每个字符都应具有相同的大小。我已经从官方网站 (microsoft.com/typography/fonts/font.aspx?FMID=1081) 下载并安装了 Arial Unicode MS 字体。
  • 嗨@AndrewThompson 感谢您提供方便的提示。 Thomas 使用了“Font.Monospaced”字体。此外,对于空格字符,它们只是被添加以在左右文本之间创建一个空白,以实现干净的 UI(如果文本在这里和那里看起来不太好),并且左右文本也是动态的,这意味着左边的文本总是英文 &字符数不同,正确的文本英语或其他语言和字符数不同。

标签: java swing unicode fonts jtree


【解决方案1】:

您在 JTree 中的文本没有对齐,因为“Arial Unicode MS”字体不是等宽字体。

Wikipedia article: 没有关于“等宽”的说法。 “MS”可能只是意味着“微软”。

只需使用“等宽”逻辑字体(Java 中始终存在), 它会很好(至少在我的 Windows 上):

    //create the root node
    DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");

    //create the child nodes
    DefaultMutableTreeNode vegetableNode = new DefaultMutableTreeNode(
            "Vegetables                           test1");
    DefaultMutableTreeNode fruitNode = new DefaultMutableTreeNode(
            "test2                                " + "안녕하세요, 당신은 어떠세요");
    //add the child nodes to the root node
    root.add(vegetableNode);
    root.add(fruitNode);

    //create the tree by passing in the root node
    tree = new JTree(root);
    tree.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 12));       
    getContentPane().add(tree);

【讨论】:

  • 嗨,Thomas 感谢您的回答,但请查看我的最后评论。 :-(
猜你喜欢
  • 2015-08-11
  • 1970-01-01
  • 2011-03-11
  • 2019-02-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多