简体   繁体   English

在java中运行框架程序时出错

[英]error in running frame program in java

I compiled the program it has no error but while running I get an error:我编译了程序,它没有错误,但在运行时出现错误:

could not find or load frame.java找不到或无法加载 frame.java

I saved program name as frame.java and run as java frame.java我将程序名称保存为frame.java并作为java frame.java运行

import java.awt.*;
import java.awt.event.*;
public class frame extends Frame {

    Label l1, l2, l3, l4, l5, l6;

    frame() {
        super("my frame title");
        setLayout(new FlowLayout());
        l1 = new Label("name");
        l2 = new Label("fathers name");
        l3 = new Label("add");
        l4 = new Label("sex");
        l5 = new Label("course");
        l6 = new Label("hobbies");

        add(l1);
        add(l2);
        add(l3);
        add(l4);
        add(l5);
        add(l6);

        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                dispose();
            }
        }
        );
    }

    public static void main(String a[]) {
        frame o = new frame();
        o.setSize(500, 500);
        o.setVisible(true);
    }
}

A Java source file is compiled into a file of {name}.class.一个Java 源文件被编译成一个{name}.class 文件。

But when running the class file, you don't need to supply the .class prefix, for example...但是在运行类文件时,您不需要提供.class前缀,例如...

java frame

Another problem you might have is that there is an expectation that a class should belong to a package and you may have difficulty finding/running a class within the default package, for example...您可能遇到的另一个问题是,期望一个类应该属于一个包,并且您可能很难在默认包中找到/运行一个类,例如......

package test;
import java.awt.*;
import java.awt.event.*;
public class frame extends Frame {

    Label l1, l2, l3, l4, l5, l6;

    frame() {
        super("my frame title");
        setLayout(new FlowLayout());
        l1 = new Label("name");
        l2 = new Label("fathers name");
        l3 = new Label("add");
        l4 = new Label("sex");
        l5 = new Label("course");
        l6 = new Label("hobbies");

        add(l1);
        add(l2);
        add(l3);
        add(l4);
        add(l5);
        add(l6);

        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                dispose();
            }
        }
        );
    }

    public static void main(String a[]) {
        frame o = new frame();
        o.setSize(500, 500);
        o.setVisible(true);
    }
}

The source file will now need to be saved in the test directory and compiled using something like...源文件现在需要保存在test目录中并使用类似...

javac test\frame.java

And run using something like并使用类似的东西运行

java test.frame

Assuming your are executing from the parent directory of the test directory假设您是从test目录的父目录执行

See Creating and Using Packages for more details.有关更多详细信息,请参阅创建和使用包

I'd also recommend that:我还建议:

  • You use an IDE to start with, as it will make the process simpler您首先使用 IDE,因为它会使过程更简单
  • You consider using Swing or JavaFX instead of AWT based class, as AWT doesn't have the same community support as Swing and JavaFX, which make it easier to solve problems you might have in the future您考虑使用 Swing 或 JavaFX 而不是基于 AWT 的类,因为 AWT 没有与 Swing 和 JavaFX 相同的社区支持,这可以更轻松地解决您将来可能遇到的问题
  • You have a read through Code Conventions for the Java TM Programming Language , it will make it easier for people to read your code and for you to read others您已经通读 了 Java TM 编程语言的代码约定,它将使人们更容易阅读您的代码,您也可以阅读其他人的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM