我正在尝试使用 JTree。这是我的示例测试类:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.util.HashMap;
import java.util.Set;
import java.util.Vector;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import jsparser.Function;
import jsparser.HTMLElement;
import jsparser.JSArray;
import jsparser.JSInt;
import jsparser.JSObject;
import jsparser.JSString;
import jsparser.JSValue;
import jsparser.Null;
/**
*
* @author Alex
*/
public class ObjectTreeTest {
private static JSObject prepareTree() {
JSObject obj = new JSObject();
obj.set("x", new JSInt(1));
obj.set("y", new JSInt(1));
JSArray users = new JSArray();
obj.set("users", users);
JSObject user = new JSObject();
obj.set("login", new JSString("Alex654"));
obj.set("password", new JSString("123456"));
JSObject info = new JSObject();
info.set("city", new JSString("Moscow"));
info.set("job", new JSString("Developer"));
info.set("firstName", new JSString("Alex"));
info.set("lastName", new JSString("Popov"));
obj.set("bio", info);
users.push(user);
JSObject user2 = new JSObject();
obj.set("login", new JSString("admin"));
obj.set("password", new JSString("test"));
obj.set("bio", Null.getInstance());
users.push(user2);
return obj;
}
public static class JSValueWrapper {
public JSValueWrapper(JSValue val) {
this.val = val;
}
public JSValue getValue() {
return val;
}
@Override
public String toString() {
if (val instanceof Function) {
return "{ Function }";
}
else if (val instanceof JSArray) {
return "Array";
}
else if (val instanceof HTMLElement) {
return "HTMLElement[" + ((HTMLElement)val).node.tagName + "]";
}
else if (val instanceof JSObject) {
return "{ Object }";
}
return val.toString();
}
private JSValue val;
}
public static void processChildren(DefaultMutableTreeNode node) {
JSValue val = ((JSValueWrapper)node.getUserObject()).getValue();
if (val instanceof JSArray) {
Vector<JSValue> items = ((JSArray)val).getItems();
for (int i = 0; i < items.size(); i++) {
DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(new JSValueWrapper(items.get(i)));
node.add(newNode);
processChildren(newNode);
}
} else if (!(val instanceof Function) && val instanceof JSObject) {
HashMap<String, JSValue> props = ((JSObject)val).getProperties();
Set<String> keys = props.keySet();
for (String key: keys) {
DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(new JSValueWrapper(props.get(key)));
node.add(newNode);
if (!key.equals("__proto__")) {
processChildren(newNode);
}
}
}
}
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(
UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {}
final JSObject root = prepareTree();
if (root == null) return;
final JFrame frame = new JFrame("Object Inspector");
JPanel cp = new JPanel();
cp.setBorder(BorderFactory.createEmptyBorder(9, 10, 9, 10));
frame.setContentPane(cp);
cp.setLayout(new BorderLayout());
DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode(new JSValueWrapper(root));
processChildren(rootNode);
DefaultTreeModel treeModel = new DefaultTreeModel(rootNode, true);
final JTree tree = new JTree(treeModel);
final JPanel contentpane = new JPanel();
contentpane.setBackground(Color.WHITE);
contentpane.setOpaque(true);
contentpane.setLayout(new BoxLayout(contentpane, BoxLayout.PAGE_AXIS));
contentpane.add(tree);
//contentpane.setBounds(0, 0, 490, 380);
final int width = 490, height = 418;
final JScrollPane scrollpane = new JScrollPane(contentpane);
scrollpane.setOpaque(false);
scrollpane.getInsets();
cp.add(scrollpane);
scrollpane.setBackground(Color.WHITE);
scrollpane.setOpaque(true);
scrollpane.setPreferredSize(new Dimension(width, height));
//setSize(518, 420);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
tree.setPreferredSize(new Dimension(tree.getParent().getWidth(), tree.getParent().getHeight()));
}
});
}
}
我希望得到一棵漂亮的树,就像在我每天使用的 NetBeans IDE 中一样。
但是我明白了:
这到底有什么问题?还应该注意,当我的标签更长时(没有“包装器”内部类),叶子中的输出标签宽了 2-3 倍,但标签的宽度也大大减少了。