Results 1 to 2 of 2
  1. #1
    Join Date
    Jan 2009
    Posts
    10
    Rep Power
    0

    Default "The public type AppletFrame must be defined in its own file" what is it?

    XML Code:
    //Create a child frame window from within an applet
    package Applets;
    import java.awt.*;
    import java.applet.*;
    import java.awt.event.*;
    /*
     * <applet code="SampleFrame" width=300 height=50>
     * </applet>
     * */
    class SampleFrame extends Frame{
    	SampleFrame(String title){
    		super(title);
    		//create an object to handle window events
    		MyWindowAdapter adapter=new MyWindowAdapter(this);
    		//register it to receive those events
    		addWindowListener(adapter);
    	}
    	public void paint(Graphics g){
    		g.drawString("This is in Frame window",10,40);
    	}
    }
    class MyWindowAdapter extends WindowAdapter{
    	SampleFrame sampleFrame;
    	public MyWindowAdapter(SampleFrame sampleFrame){
    		this.sampleFrame=sampleFrame;
    	}
    	public void windowClosing(WindowEvent we){
    		sampleFrame.setVisible(false);
    	}
    }
    //create Frame window
    
    public class AppletFrame extends Applet{
    	Frame f;
    	public void init(){
    		f=new SampleFrame("A Frame Window");
    		
    		f.setSize(250,250);
    		f.setVisible(true);
    	}
    	public void start(){
    		f.setVisible(true);
    	}
    	public void stop(){
    		f.setVisible(false);
    	}
    	public void paint(Graphics g){
    		g.drawString("This is A Applet Window",20,20);
    	}
    }
    i am executing this program in "eclipse"..
    Error is...

    XML Code:
    java.lang.Error: Unresolved compilation problem: 
    	The public type AppletFrame must be defined in its own file
    
    	at Applets.AppletFrame.<init>(SampleFrame.java:33)
    	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    	at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    	at java.lang.reflect.Constructor.newInstance(Unknown Source)
    	at java.lang.Class.newInstance0(Unknown Source)
    	at java.lang.Class.newInstance(Unknown Source)
    	at sun.applet.AppletPanel.createApplet(Unknown Source)
    	at sun.applet.AppletPanel.runLoader(Unknown Source)
    	at sun.applet.AppletPanel.run(Unknown Source)
    	at java.lang.Thread.run(Unknown Source)

    Thanks Sir.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    what is it?
    Put this class in a separate file or remove the public modifier from its declaration, ie, change this
    Java Code:
    public class AppletFrame extends Applet{
    to this
    Java Code:
    class AppletFrame extends Applet{
    In general, only one outer class in any given file may have the public modifier and its name must be the same name as the fileName.

    To run an applet from the prompt (like an application) try this approach:
    Java Code:
    public class Pseudo extends JApplet {
        public void init() {
            // everything as usual for your applet
        }
    
        /** Add this convenience method. */
        public static void main(String[] args) {
            JApplet applet = Pseudo();
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(applet);
            f.setSize(300,50);
            f.setLocation(100,100);
            applet.init();
            // if you have overidden the applet [i]start[/i] method
            applet.start();
            f.setVisible(true);
        }
    }

Similar Threads

  1. Replies: 6
    Last Post: 02-10-2011, 09:55 AM
  2. Java, Military Format using "/" and "%" Operator!!
    By sk8rsam77 in forum New To Java
    Replies: 11
    Last Post: 02-26-2010, 03:03 AM
  3. Count lines cointaining "word" in input file
    By gwithey in forum New To Java
    Replies: 5
    Last Post: 04-02-2009, 05:23 AM
  4. Replies: 2
    Last Post: 01-24-2009, 06:56 PM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •