【发布时间】:2014-03-29 10:10:53
【问题描述】:
当我编译 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();
}
}
【问题讨论】:
-
错误对我来说很清楚。你的文件名是什么?
-
我认为错误信息中已经包含了解决方法:Did you name your file
Appletprac.java? -
好的,我通过 appletprac.java 名称保存文件。我已将其重命名为 Appletprac.java,现在正在编译。但是在运行时出现另一个错误:在 Appletprac 类中找不到主方法,请将主方法定义为:public static void main(String[] args) 或 JavaFX 应用程序类必须扩展 javafx.aaplication.Application
标签: java class compilation applet declaration