我是java新手,我有两个类(类calc和按钮),我需要拆分成单独的文件,类calc使用类按钮。然而,当我运行A类时它们都会编译失败。我怎么能这样做?
这是类calc的代码
package start;
import javax.swing.*;
import java.awt.event.*;
import java.awt.Container;
import java.awt.Insets;
import java.awt.Dimension;
import button.*;
public class calc {
private button calc_buttons;
private static void creategui(){
JFrame window = new JFrame("hello dynamic window");
buttons.createbuttons();
buttons.setOpaque(false);
window.setContentPane(buttons);
window.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
window.pack();
window.setVisible(true);
}
static public void main(String[] args){
creategui();
}
}
这是类按钮的代码
package button;
import javax.swing.*;
import java.awt.event.*;
import java.awt.Container;
import java.awt.Insets;
import java.awt.Dimension;
public class button /*extends JPanel implements ActionListener*/{
protected static JButton button1, button2, button3, button4, button5, button6, button7;
protected static JButton button8, button9, button_fact, button_exp, button_sqrt, button_abs;
protected static JButton button_sin, button_cos, button_tan, button_percent, button_rcl;
protected static JButton button_MC, button_e, button_pi, button_Mplus, button_Mminus;
protected static JButton button_C, button_AC, button_MS, button_sign;
protected static JButton button_plus, button_minus, button_div, button_mult, button_equal;
static public void calc_button(){
}
}
当我编译时,我没有得到任何错误消息,但是当我运行calc类时,我得到了这个
线程“main”中的异常java.lang.NoClassDefFoundError:calc(错误的名称:start / calc) at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClassCond(ClassLoader.java:632) at java.lang.ClassLoader.defineClass(ClassLoader.java:616) at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141) at java.net.URLClassLoader.defineClass(URLClassLoader.java:283) at java.net.URLClassLoader.access $ 000(URLClassLoader.java:58) 在java.net.URLClassLoader $ 1.run(URLClassLoader.java:197) at java.security.AccessController.doPrivileged(Native Method) 在java.net.URLClassLoader.findClass(URLClassLoader.java:190) at java.lang.ClassLoader.loadClass(ClassLoader.java:307) 在sun.misc.Launcher $ AppClassLoader.loadClass(Launcher.java:301) 在java.lang.ClassLoader.loadClass(ClassLoader.java:248)
答案 0 :(得分:2)
更高一个目录(进入包含“start”和“button”的目录)并执行以下操作:
java start.calc
顺便说一下,让类名以大写字母开头是一种约定。