我正在编写一个JTree,其中我正在调整英文和unicode混合的文本。 在使用Arial Unicode MS字体(unicode和monospaced)后,我看到的是,文本未对齐,因为等宽文本通常是对齐的。
我测试过的正确对齐的一种字体是" 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();
}
});
}
}
答案 0 :(得分:0)
您在JTree中的文本未对齐,因为“Arial Unicode MS”字体不是等宽字体。
请参阅Wikipedia article: 没有任何关于“Monospaced”的说法。 “MS”可能仅仅意味着“微软”。
只需使用“Monospaced”逻辑字体(始终存在于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);