问题 - 编写一个创建分层树数据结构的程序
Science -> BSc IT -> NS -> ST -> ASP.NET -> ADV JAVA -> LINUX
Answer- |
\ /
-
import javax.swing.*;
import java.awt.*;
import javax.swing.tree.*;
class JTree extends JFrame
{
JTree t;
DefaultMutableTreeNode sci, bscit, s1,s2,s3,s4,s5;
public JTree()
{
Container c=getContentPane();
c.setLayout(new FlowLayout());
sci=new DefaultMutableTreeNode("Science");
bscit=new DefaultMutableTreeNode("BSc IT");
s1=new DefaultMutableTreeNode("NS");
s2=new DefaultMutableTreeNode("ST");
s3=new DefaultMutableTreeNode("ASP.NET");
s4=new DefaultMutableTreeNode("ADV JAVA");
s5=new DefaultMutableTreeNode("LINUX");
sci.add(bscit);
bscit.add(s1);
bscit.add(s2);
bscit.add(s3);
bscit.add(s4);
bscit.add(s5);
c.add(t);
pack();
setVisible(true);
}
public static void main(String args[]) //Error at here of Can not load main class
{
JTree obj=new JTree();
}
}
JTree的(运行)
run:
java.lang.NoClassDefFoundError: jtree/JTree (wrong name: JTree)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)
Exception in thread "main" Java Result: 1
BUILD SUCCESSFUL (total time: 2 seconds)
请帮助我摆脱它。
答案 0 :(得分:1)
重命名你的类名(例如Main)并在你的类中创建JTree字段,然后在主类的构造函数中创建它的实例:
import javax.swing.*;
import java.awt.*;
import javax.swing.tree.*;
class Main extends JFrame {
JTree t;
DefaultMutableTreeNode sci, bscit, s1, s2, s3, s4, s5;
public Main() {
Container c = getContentPane();
c.setLayout(new FlowLayout());
sci = new DefaultMutableTreeNode("Science");
bscit = new DefaultMutableTreeNode("BSc IT");
s1 = new DefaultMutableTreeNode("NS");
s2 = new DefaultMutableTreeNode("ST");
s3 = new DefaultMutableTreeNode("ASP.NET");
s4 = new DefaultMutableTreeNode("ADV JAVA");
s5 = new DefaultMutableTreeNode("LINUX");
sci.add(bscit);
bscit.add(s1);
bscit.add(s2);
bscit.add(s3);
bscit.add(s4);
bscit.add(s5);
t = new JTree(sci);
c.add(t);
pack();
setVisible(true);
}
public static void main(String args[])
{
Main obj = new Main();
}
}