两个代码几乎完全一样,就是大小写改变了。但一个能运行,另一个不能运行,请高手指点一下。按照习惯类应该大写,但小写难道就不能运行了吗?
第一次没问题
import java.awt.*;
import javax.swing.*;
public class BoxLayoutFrame extends JFrame{
BoxLayoutTest panel=new BoxLayoutTest();
public BoxLayoutFrame(){
this.getContentPane().add(panel);
this.setSize(500,220);
this.setTitle("BoxLayoutDemo");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.show();
}
public static void main(String args[]){
BoxLayoutFrame frame=new BoxLayoutFrame();
}
}
class BoxLayoutTest extends JPanel{
BoxLayoutTest(){
setLayout(new BoxLayout(this,BoxLayout.X_AXIS));//也可以设置成为Y_AXIS
TextField textField=new TextField();
TextArea textArea=new TextArea(4,20);
JButton button=new JButton("Click me");
add(new JLabel("TextField"));
add(textField);
add(new JLabel("TextArea"));
add(textArea);
add(new JLabel("Button"));
add(button);
}
}第二个小写不能运行
import java.awt.*;
import javax.swing.*;
public class boxLayoutFrame extends JFrame{
boxLayoutTest panel=new boxLayoutTest();
public boxLayoutFrame(){
this.getContentPane().add(panel);
this.setSize(500,220);
this.setTitle("BoxLayoutDemo");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.show();
}
public static void main(String args[]){
boxLayoutFrame frame=new boxLayoutFrame();
}
}
class boxLayoutTest extends JPanel{
boxLayoutTest(){
setLayout(new boxLayout(this,boxLayout.X_AXIS));//也可以设置Y_AXIS
TextField textField=new TextField();
TextArea textArea=new TextArea(4,20);
JButton button=new JButton("Click me");
add(new JLabel("TextField"));
add(textField);
add(new JLabel("TextArea"));
add(textArea);
add(new JLabel("Button"));
add(button);
}
}
第一次没问题
import java.awt.*;
import javax.swing.*;
public class BoxLayoutFrame extends JFrame{
BoxLayoutTest panel=new BoxLayoutTest();
public BoxLayoutFrame(){
this.getContentPane().add(panel);
this.setSize(500,220);
this.setTitle("BoxLayoutDemo");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.show();
}
public static void main(String args[]){
BoxLayoutFrame frame=new BoxLayoutFrame();
}
}
class BoxLayoutTest extends JPanel{
BoxLayoutTest(){
setLayout(new BoxLayout(this,BoxLayout.X_AXIS));//也可以设置成为Y_AXIS
TextField textField=new TextField();
TextArea textArea=new TextArea(4,20);
JButton button=new JButton("Click me");
add(new JLabel("TextField"));
add(textField);
add(new JLabel("TextArea"));
add(textArea);
add(new JLabel("Button"));
add(button);
}
}第二个小写不能运行
import java.awt.*;
import javax.swing.*;
public class boxLayoutFrame extends JFrame{
boxLayoutTest panel=new boxLayoutTest();
public boxLayoutFrame(){
this.getContentPane().add(panel);
this.setSize(500,220);
this.setTitle("BoxLayoutDemo");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.show();
}
public static void main(String args[]){
boxLayoutFrame frame=new boxLayoutFrame();
}
}
class boxLayoutTest extends JPanel{
boxLayoutTest(){
setLayout(new boxLayout(this,boxLayout.X_AXIS));//也可以设置Y_AXIS
TextField textField=new TextField();
TextArea textArea=new TextArea(4,20);
JButton button=new JButton("Click me");
add(new JLabel("TextField"));
add(textField);
add(new JLabel("TextArea"));
add(textArea);
add(new JLabel("Button"));
add(button);
}
}
import javax.swing.*; public class boxLayoutFrame extends JFrame{ boxLayoutTest panel=new boxLayoutTest(); public boxLayoutFrame(){
this.getContentPane().add(panel);
this.setSize(500,220);
this.setTitle("BoxLayoutDemo");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.show();
}
public static void main(String args[]){
boxLayoutFrame frame=new boxLayoutFrame();
}
} class boxLayoutTest extends JPanel{
boxLayoutTest(){ setLayout(new BoxLayout(this,BoxLayout.X_AXIS));//也可以设置Y_AXIS
TextField textField=new TextField();
TextArea textArea=new TextArea(4,20);
JButton button=new JButton("Click me");
add(new JLabel("TextField"));
add(textField);
add(new JLabel("TextArea"));
add(textArea);
add(new JLabel("Button"));
add(button);
}
}
Exception in thread "main" java.lang.NoClassDefFoundError:boxLayoutFrame"虽然我知道和Classpath有关,但我感觉我设置的很对,因为我运行其他代码就没问题
import java.awt.*;
import javax.swing.*;
public class boxLayoutFrame extends JFrame{
boxLayoutTest panel=new boxLayoutTest();
public boxLayoutFrame(){
this.getContentPane().add(panel);
this.setSize(500,220);
this.setTitle("BoxLayoutDemo");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.show();
}
public static void main(String args[]){
boxLayoutFrame frame=new boxLayoutFrame();
}
}
class boxLayoutTest extends JPanel{
boxLayoutTest(){
setLayout(new boxLayout(this,boxLayout.X_AXIS));//也可以设置Y_AXIS !!!!------>把boxLayout改成BoxLayout就可以了
TextField textField=new TextField();
TextArea textArea=new TextArea(4,20);
JButton button=new JButton("Click me");
add(new JLabel("TextField"));
add(textField);
add(new JLabel("TextArea"));
add(textArea);
add(new JLabel("Button"));
add(button);
}
}
Exception in thread "main" java.lang.NoClassDefFoundError:boxLayoutFrame"虽然我知道和Classpath有关,但我感觉我设置的很对,因为我运行其他代码就没问题我是从你报的错误上看出来的,错误说:无类定义发现错。也就是说,你原先的BoxLayoutFrame.class你没有删除,那么,在你修改了类名之后,用javac生成的boxLayoutFrame.class在Windows看来和BoxLayoutFrame.class是同一个文件,所以,它就覆盖了BoxLayoutFrame.class,并且继续使用BoxLayoutFrame.class这个名字。而在Java看来:boxLayoutFrame和BoxLayoutFrame是两个完全不同的名字,于是报“java.lang.NoClassDefFoundError:boxLayoutFrame”。你只需要把原来生成的class删除了,然后重新javac,再java。就应该能解决这个问题了。前几天太忙了,一直没时间回答。
boxLayoutFrame.java:18: 找不到符号
符号: 类 boxLayout
位置: 类 boxLayoutTest
setLayout(new boxLayout(this,boxLayout.X_AXIS));//也可以设置成为Y_AXIS
^
boxLayoutFrame.java:18: 找不到符号
符号: 变量 boxLayout
位置: 类 boxLayoutTest
setLayout(new boxLayout(this,boxLayout.X_AXIS));//也可以设置成为Y_AXIS
谁能帮忙解决一下,我快被折磨晕了。