Java编译错误:类AppletPrac是公开的,应在名为appletprac.java的文件中声明

发布于 2025-01-18 07:29:25 字数 2150 浏览 2 评论 0原文

编译 Java 程序时,出现以下错误:

Appletprac是公共的,应该在名为Appletprac.java的文件中声明

这是我的 Java 代码:

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

When compiling the Java program, I am getting this error:

class Appletprac is public, should be declared in a file named Appletprac.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();
}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

慵挽 2025-01-25 07:29:25

Java每个文件允许一个公共类,并且公共类名称应与文件名相同。对于您来说,您应该制作文件名appletprac.java,

您可以看到此链接为什么Java中的文件名与类名称相同?

写下这样的html文件:

test.html

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

将编译的.class文件放在同一文件夹中,然后输入appletViewer test.html在CMD中。

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

You can see this link Why are filenames in Java the same as the class name?

Write a html file like this:

test.html

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

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

巡山小妖精 2025-01-25 07:29:25

如果您的外部类具有 public 修饰符,则它应该位于具有相同类名和 .java 扩展名的文件中。这是一个简单的 java 约定,用于在文件系统中构造类和包。

If your outer class has a public modifier it should be in the file with the same class name and .java extention. This is a simple java convention to structure classes and packages in the filesystem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文