我試圖創建我的第一個JTree
,但是當我嘗試將我的節點添加到樹中時,所做的更改未生效,並且顯示的所有內容都是其中包含一些默認節點的樹(顏色,運動,食物)。JTree不顯示添加的節點
我的主面板中的代碼,其中樹所在:
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
public class Scenario1Panel extends JPanel
{
/**
*
*/
private static final long serialVersionUID = 1L;
public Scenario1Tree scenario1Tree;
public Scenario1Panel()
{
scenario1Tree = new Scenario1Tree();
add(scenario1Tree);
}
public static void main(String args[])
{
try
{
String lookAndFeel = UIManager.getSystemLookAndFeelClassName();
System.out.println(lookAndFeel);
UIManager.setLookAndFeel(lookAndFeel);
}
catch(Exception e)
{
System.out.println("couldn't get that LookAndFeel");
}
Scenario1Panel mgr = new Scenario1Panel();
JFrame frame = new JFrame();
frame.add(mgr);
frame.pack();
frame.setVisible(true);
}
}
我的樹代碼:
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.UIManager;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeSelectionModel;
public class Scenario1Tree extends JTree
{
/**
*
*/
private static final long serialVersionUID = 1L;
private JTree tree;
public Scenario1Tree()
{
DefaultMutableTreeNode top = new DefaultMutableTreeNode("Scenario1");
createNodes(top);
tree = new JTree(top);
//Create a tree that allows one selection at a time.
tree.getSelectionModel().setSelectionMode
(TreeSelectionModel.SINGLE_TREE_SELECTION);
//Create the scroll pane and add the tree to it.
JScrollPane treeView = new JScrollPane(tree);
}
private void createNodes(DefaultMutableTreeNode top) {
DefaultMutableTreeNode loginUser1 = null;
DefaultMutableTreeNode addNewUsers = null;
DefaultMutableTreeNode addNewUsersBreakout = null;
loginUser1 = new DefaultMutableTreeNode("Login User 1");
top.add(logintUser1);
addNewUsers = new DefaultMutableTreeNode("Add New Users");
top.add(addNewUsers);
addNewUsersBreakout = new DefaultMutableTreeNode("Add User2");
addNewUsers.add(addNewUsersBreakout);
addNewUsersBreakout = new DefaultMutableTreeNode("Add User3");
addNewUsers.add(addNewUsersBreakout);
addNewUsersBreakout = new DefaultMutableTreeNode("Add User4");
addNewUsers.add(addNewUsersBreakout);
addNewUsersBreakout = new DefaultMutableTreeNode("Add User5");
addNewUsers.add(addNewUsersBreakout);
}
public static void main(String args[])
{
try
{
String lookAndFeel = UIManager.getSystemLookAndFeelClassName();
System.out.println(lookAndFeel);
UIManager.setLookAndFeel(lookAndFeel);
}
catch(Exception e)
{
System.out.println("couldn't get that LookAndFeel");
}
Scenario1Tree tree = new Scenario1Tree();
JFrame frame = new JFrame();
frame.add(tree);
frame.pack();
frame.setVisible(true);
}
}
可能有人請解釋一下我的」米做錯了嗎?
'公共類Scenario1Tree擴展JTree'不要延長樹,除非增加新的功能或壓倒一切的舊。不要擴展'JPanel',只保留一個實例。 –
首先查看JTree演示/示例。 [YouTube](http://www.youtube.com/watch?v=aEr4XVMVdRE)或其他人。 [NetBeans IDE](http://www.netbeans.org)有一個很好的GUI編輯器。檢查代碼。也許您已經在使用NetBeans,因爲它使用了一個虛擬TreeModel作爲JTree構造函數參數,但也可以使用根節點調用構造函數。代碼現在必須改變很多。 –
問題是我從來沒有使用Java,所以這對我來說都是新手。我一直在試圖遵循這個例子:docs.oracle.com/javase/tutorial/uiswing/components/tree.html 我不需要使用擴展爲了使用類中的方法像add?如果我刪除「擴展樹」,則添加(Scenario1Tree)不再有效。 – Matt