java split source files
I am new to java and I have two classes (class calc and button) that I need to split into separate files and class calc uses class button. they both compile however when I run class A it fails. How can I do this?
This is the code for class 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();
}
}
this is the code for class button
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(){
}
}
when I compile I get no error messages but when I run the calc class I get this
Exception in thread "main" java.lang.NoClassDefFoundError: calc (wrong name: 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) at java.net.URLClassLoader$1.run(URLClassLoader.java:197) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:190) at java.lang.ClassLoader.loadClass(ClassLoader.java:307) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Go one directory higher (into the directory that contains "start" and "button") and do this:
java start.calc
By the way, it's a convention to let class names start with a capital letter.
精彩评论