JTree 叶子中的 UTF-8 字符
UTF-8 characters in JTree leaves
我正在使用 JTree 在我的 GUI 上显示数据。有时这个数据是一个字符串,它是 UTF-8 格式的。这些字符串可能包含外国字符或表情符号。我的问题是字符串显示错误。
示例:
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeNode;
public class Demo {
public static void main(String[] args) {
DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
DefaultMutableTreeNode leaf = new DefaultMutableTreeNode("Leaf: ");
root.add(leaf);
JTree tree = new JTree(root);
JFrame frame = new JFrame("Demo");
frame.add(new JScrollPane(tree));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
输出:
- Root
|-- []eaf: []
是否可以使用 JTree 正确显示这些字符串?
对于未来的读者:
也许你也想检查一下。 Click
- 使用
tree2.setFont(new Font("DejaVu Sans", 0, 16));
时,您的代码运行良好。
- 我是 运行
JDK1.8.0_66
Windows 10 x64。
import java.awt.*;
import javax.swing.*;
import javax.swing.tree.*;
public class Demo2 {
public JComponent makeUI() {
//String s = "";
String s = "\uD83D\uDE01"; //\u1F601
DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
DefaultMutableTreeNode leaf = new DefaultMutableTreeNode("Leaf: " + s);
root.add(leaf);
JTree tree1 = new JTree(root);
tree1.setFont(new Font("Lucida Sans", 0, 16));
JTree tree2 = new JTree(root);
tree2.setFont(new Font("DejaVu Sans", 0, 16));
JPanel p = new JPanel(new GridLayout(2, 1));
p.add(new JScrollPane(tree1));
p.add(new JScrollPane(tree2));
return p;
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override public void run() {
createAndShowGUI();
}
});
}
public static void createAndShowGUI() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.getContentPane().add(new Demo2().makeUI());
f.setSize(320, 240);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
我正在使用 JTree 在我的 GUI 上显示数据。有时这个数据是一个字符串,它是 UTF-8 格式的。这些字符串可能包含外国字符或表情符号。我的问题是字符串显示错误。
示例:
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeNode;
public class Demo {
public static void main(String[] args) {
DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
DefaultMutableTreeNode leaf = new DefaultMutableTreeNode("Leaf: ");
root.add(leaf);
JTree tree = new JTree(root);
JFrame frame = new JFrame("Demo");
frame.add(new JScrollPane(tree));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
输出:
- Root
|-- []eaf: []
是否可以使用 JTree 正确显示这些字符串?
对于未来的读者:
也许你也想检查一下。 Click
- 使用
tree2.setFont(new Font("DejaVu Sans", 0, 16));
时,您的代码运行良好。- 我是 运行
JDK1.8.0_66
Windows 10 x64。
- 我是 运行
import java.awt.*;
import javax.swing.*;
import javax.swing.tree.*;
public class Demo2 {
public JComponent makeUI() {
//String s = "";
String s = "\uD83D\uDE01"; //\u1F601
DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
DefaultMutableTreeNode leaf = new DefaultMutableTreeNode("Leaf: " + s);
root.add(leaf);
JTree tree1 = new JTree(root);
tree1.setFont(new Font("Lucida Sans", 0, 16));
JTree tree2 = new JTree(root);
tree2.setFont(new Font("DejaVu Sans", 0, 16));
JPanel p = new JPanel(new GridLayout(2, 1));
p.add(new JScrollPane(tree1));
p.add(new JScrollPane(tree2));
return p;
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override public void run() {
createAndShowGUI();
}
});
}
public static void createAndShowGUI() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.getContentPane().add(new Demo2().makeUI());
f.setSize(320, 240);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}