gpt4 book ai didi

java - JTree Java代码,出现无法加载主类的错误,即使它有主类

转载 作者:行者123 更新时间:2023-12-01 12:16:42 26 4
gpt4 key购买 nike

问题 - 编写一个创建分层树数据结构的程序

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)

请帮我摆脱它。

最佳答案

重命名您的类名(例如 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();
}
}

关于java - JTree Java代码,出现无法加载主类的错误,即使它有主类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26955705/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com