gpt4 book ai didi

Java 编译错误 : class Appletprac is public, 应在名为 Appletprac.java 的文件中声明

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:05:14 24 4
gpt4 key购买 nike

当我编译 java 程序时出现此错误:class Appletprac is public, should be declared in a file named 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();
}
}

最佳答案

Java 允许每个文件一个公共(public)类,公共(public)类名必须与文件名相同。对于您来说,您应该将文件名命名为 Appletprac.java

你可以看到这个链接Why are filenames in Java the same as the class name?

写一个这样的html文件:

测试.html

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

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

关于Java 编译错误 : class Appletprac is public, 应在名为 Appletprac.java 的文件中声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22729678/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com