Java 编译错误:类 Appletprac 是公共的,应在名为 Appletprac.java 的文件中声明 [英] Java Compile Error: class Appletprac is public, should be declared in a file named Appletprac.java

查看:16
本文介绍了Java 编译错误:类 Appletprac 是公共的,应在名为 Appletprac.java 的文件中声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我编译 java 程序时,我收到此错误:class Appletprac is public,应该在名为 Appletprac.java 的文件中声明

When I am compiling the java program I am getting this error: class Appletprac is public, should be declared in a file named Appletprac.java

这是我的java代码:

Here is my java code:

import java.applet.*;
import java.awt.*;        // Graphics Class
import javax.swing.*;
import java.awt.event.*;
/*<applet code="Appletprac.class" width="500" height="500"> </applet>*/
public class Appletprac extends JApplet implements ActionListener
{
JButton OK;
JRadioButton Font_Style1,Font_Style2,Font_Style3;
ButtonGroup bg;
JCheckBox Font_Family_Name;
JTextField jt;
int i;
         String s="";
public void init()
{
    OK=new JButton("OK");       
    Font_Family_Name=new JCheckBox("Serif");
    Font_Style1=new JRadioButton("Plain");
    Font_Style2=new JRadioButton("Bold");   
    Font_Style3=new JRadioButton("BoldItalic");
    bg=new ButtonGroup();
    jt=new JTextField(20);
    this.setLayout(new FlowLayout());
    bg.add(Font_Style1);
    bg.add(Font_Style2);
    bg.add(Font_Style3);    
                      this.add(jt);
    this.add(OK);
    this.add(Font_Family_Name);
    this.add(Font_Style1);
    this.add(Font_Style2);
    this.add(Font_Style3); 
    OK.addActionListener(this);
    Font_Style1.addActionListener(this);    
    Font_Style2.addActionListener(this);
    Font_Style3.addActionListener(this);
}
public void start()
{}
public void stop()
{}  
public void paint(Graphics g)
{
    g.clearRect(50,50,500,300);
    g.draw3DRect(50,50,500,300,false);
    g.setFont(new Font(s,i,30));
    g.setColor(Color.BLUE);
    g.drawString(jt.getText(),100,100);

}
public void actionPerformed(ActionEvent e)
{
    if(e.getSource()==Font_Style1)
        i=Font.PLAIN;
    if(e.getSource()==Font_Style2)
                  i=Font.BOLD;
    if(e.getSource()==Font_Style3)
    {
                  i=Font.ITALIC;
                  int j=Font.BOLD;
                  i=i+j;
    }       
    if(e.getSource()==Font_Family_Name || e.getSource()==OK)
    {
        if(Font_Family_Name.isSelected())
            s="Serif";
        else
                      s="Tall paul";
    }       
    repaint();
}
}

推荐答案

Java 允许每个文件有一个公共类,公共类名应与文件名相同.对于您,您应该将文件名 Appletprac.java

Java allow one public class per file, and the public class name should be the same with the file name. For you , you should make the filename Appletprac.java

你可以看到这个链接 为什么文件名在Java和类名一样吗?

像这样写一个html文件:

Write a html file like this:

test.html

<html>
<applet 
   code = Appletprac.class
   width = 200
   height = 100>
</applet>
</html>

将编译好的.class文件放在同一个文件夹中,在cmd中输入appletviewer test.html.

Put the compiled .class file in the same folder, and input appletviewer test.html in the cmd.

这篇关于Java 编译错误:类 Appletprac 是公共的,应在名为 Appletprac.java 的文件中声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆