Java的编译错误:类Appletprac是公共的,应在名为Appletprac.java文件中声明 [英] Java Compile Error: class Appletprac is public, should be declared in a file named Appletprac.java
问题描述
当我编译java程序,我收到此错误:类 Appletprac
是公共的,应在名为文件中声明 Appletprac.java
下面是我的Java code:
进口java.applet中的*。
进口java.awt中的*。 //图形类
进口的javax.swing *。
java.awt.event中导入*。
/ *<小程序code =Appletprac.classWIDTH =500HEIGHT =500> < /小程序> * /
公共类Appletprac扩展JApplet的实现的ActionListener
{
JButton的确定;
一个JRadioButton Font_Style1,Font_Style2,Font_Style3;
ButtonGroup的BG;
JCheckBox的Font_Family_Name;
JTextField的JT;
INT I;
字符串s =;
公共无效的init()
{
确定=新的JButton(OK);
Font_Family_Name =新JCheckBox的(衬线);
Font_Style1 =新一个JRadioButton(素);
Font_Style2 =新一个JRadioButton(大胆);
Font_Style3 =新一个JRadioButton(BOLDITALIC);
BG =新的ButtonGroup();
JT =新的JTextField(20);
this.setLayout(新的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(本);
Font_Style1.addActionListener(本);
Font_Style2.addActionListener(本);
Font_Style3.addActionListener(本);
}
公共无效的start()
{}
公共无效停止()
{}
公共无效漆(图形G)
{
g.clearRect(50,50,500,300);
g.draw3DRect(50,50,500,300,FALSE);
g.setFont(新字体(S,I,30));
g.setColor(Color.BLUE);
g.drawString(jt.getText(),100,100);}
公共无效的actionPerformed(ActionEvent的五)
{
如果(e.getSource()== Font_Style1)
I = Font.PLAIN;
如果(e.getSource()== Font_Style2)
I = Font.BOLD;
如果(e.getSource()== Font_Style3)
{
I = Font.ITALIC;
INT J = Font.BOLD;
I = I + J;
}
如果(e.getSource()== Font_Family_Name || e.getSource()== OK)
{
如果(Font_Family_Name.isSelected())
S =衬线;
其他
S =高保;
}
重绘();
}
}
Java的允许每个文件包含一个公共类和公共类的名称应与文件名相同。对你来说,你应该做的文件名Appletprac.java
您可以看到此链接<一个href=\"http://stackoverflow.com/questions/2134784/why-filename-in-java-should-be-same-as-class-name\">Why文件名在java中应该是相同的类名?
编写一个HTML文件:
的test.html
&LT; HTML和GT;
&LT;小程序
code = Appletprac.class
宽度= 200
高度= 100〕
&LT; /小程序&GT;
&LT; / HTML&GT;
把编译后的.class文件在同一文件夹,输入的appletviewer test.html的
在cmd。
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
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 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 filename in java should be same as 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.
这篇关于Java的编译错误:类Appletprac是公共的,应在名为Appletprac.java文件中声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!