4

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();
}
}
Roman C
  • 49,761
  • 33
  • 66
  • 176
Tanmay
  • 59
  • 2
  • 10
  • 3
    Error is pretty clear to me. What is your file name ? – Suresh Atta Mar 29 '14 at 10:12
  • I think the error message already contains the solution: Did you name your file `Appletprac.java` ? – donfuxx Mar 29 '14 at 10:12
  • Ok got the solution i was saving the file by appletprac.java name. I have renamed it to Appletprac.java and now its compiling. But while running I am getting another error: Main method not found in class Appletprac, please define the main method as: public static void main(String[] args) or a JavaFX application class must extend javafx.aaplication.Application – Tanmay Mar 29 '14 at 10:18

2 Answers2

2

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.

Community
  • 1
  • 1
locoyou
  • 1,697
  • 1
  • 15
  • 19
  • Ok my program is now compiling but after running it I am getting this error: Main method not found in class Appletprac, please define the main method as: public static void main(String[] args) or a JavaFX application class must extend javafx.aaplication.Application – Tanmay Mar 29 '14 at 10:22
  • Did you run it as Java application? You should run it as Java Applet – locoyou Mar 29 '14 at 10:25
  • If you use eclipse, you can click run as applet. Else you can write a simple html file to run it – locoyou Mar 29 '14 at 10:27
  • Oh I was running it as a java application ! Thanks for your help ! :) I am running this program in command prompt. – Tanmay Mar 29 '14 at 10:30
  • Hey @locoyou I am getting another error: applet not initialized. Can you tell me what's wrong in my code..? – Tanmay Mar 29 '14 at 12:32
  • @Tanmay How did you compile the source code(javac or eclipse) and where did you run the applet(eclipse or cmd Appletviewer or browser)? – locoyou Mar 29 '14 at 12:37
  • @Tanmay I copied your code and dont encounter this problem. I wonder it's something wrong with your classpath or something else – locoyou Mar 29 '14 at 12:40
  • I am compiling from command prompt (javac) and running the applet from same command prompt using command (appletviewer Appletprac.java). – Tanmay Mar 29 '14 at 12:40
  • @Tanmay actually it's not the right way to use Appletviewer. You should write a html file and use it. I will add it in my post – locoyou Mar 29 '14 at 12:44
1

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.

Roman C
  • 49,761
  • 33
  • 66
  • 176