两个代码几乎完全一样,就是大小写改变了。但一个能运行,另一个不能运行,请高手指点一下。按照习惯类应该大写,但小写难道就不能运行了吗?
第一次没问题
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);
}
}

解决方案 »

  1.   

    你要把文件名也改成boxLayoutFrame.java
      

  2.   

    1 楼正解. BTW, 类名首字母大写, 应该养成这样的习惯.
      

  3.   

    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); 

      

  4.   

    能通过编译,但不能运行,一运行就出现“
    Exception in thread "main" java.lang.NoClassDefFoundError:boxLayoutFrame"虽然我知道和Classpath有关,但我感觉我设置的很对,因为我运行其他代码就没问题
      

  5.   


    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); 


      

  6.   

    我看应该是Windows的问题。Windows在保存文件时是不区分大小写的。所以,如果你的文件名仅仅是修改的话,是无法修改过来的,必须要删除原来大写的文件,然后再来创建小写的文件。同样,class文件也必须删除,否则,新生成的class也会不改变大小写的。这是Windows系统与Java之间的一个区别。
      

  7.   

    能通过编译,但不能运行,一运行就出现“ 
    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。就应该能解决这个问题了。前几天太忙了,一直没时间回答。
      

  8.   

    10楼说的有道理,但是编译时又出现错误:
    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
    谁能帮忙解决一下,我快被折磨晕了。