Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2009
    Posts
    9
    Rep Power
    0

    Default Help with creating an applet

    I have no idea what I am doing wrong. All my applet is display the Welcome to java programming string I have in my paint method. Right now I just want to get a label put on a JFrame. Can anyone help please?

    Java Code:
    import java.awt.*;
    import java.applet.*;
    import javax.swing.JFrame;
    import javax.swing.JApplet;
    import javax.swing.JLabel;
    import java.awt.BorderLayout;
    
    public class SampleApplet extends JApplet {
    	
    	public void init() {
    	}
    
    	public static void main(String[] args) {
            //... Create an initialize the applet.
            JApplet theApplet = new SampleApplet();
            theApplet.init();         // Needed if overridden in applet
            //theApplet.start();        // Needed if overridden in applet
            
            //... Create a window (JFrame) and make applet the content pane.
            JFrame window = new JFrame("Sample Applet and Application");
            window.setContentPane(theApplet);
            window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            window.pack();              // Arrange the components.
            //System.out.println(theApplet.getSize());
            window.setVisible(true);    // Make the window visible.
            
            JLabel label1 = new JLabel("Test Label");
            label1.setBackground(Color.CYAN);
    	label1.setOpaque(true);
    	window.add(label1, BorderLayout.SOUTH);
        }
    
    	public SampleApplet() {
            add(new JLabel("This is both an Applet and Application!"));
    
        }
    	
    	public void paint(Graphics g) {
    		
    		g.drawString("Welcome to Java!!", 50, 60 );
    		
    	}
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I have no idea what I am doing wrong.
    You're trying to create an applet with incorrect assumptions, that's the problem. There are too many problems here to correct, and so I think you should just throw this code out and start afresh. Have a look at the Sun tutorial about applets and you'll see that many of your assumptions are incorrect (for instance trying to instantiate an applet object in the init method, trying to mix jframes with applets, trying to paint directly within the applet,....). It will give you sample code too.

    Good luck.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Heck, I already told you all this in your previous post, one that you never acknowledged, and based on your post here, one you never read. So why post here if you are just going to ignore all advice? Why should we help you in the future if we're just wasting our energies?

Similar Threads

  1. Calling another applet on click of button in one applet
    By niteshwar.bhardwaj in forum Java 2D
    Replies: 1
    Last Post: 02-19-2009, 01:54 PM
  2. Creating .Jar files
    By Deathmonger in forum New To Java
    Replies: 6
    Last Post: 06-23-2008, 12:59 AM
  3. Looking for team members for creating the applet game.
    By M77 in forum Reviews / Advertising
    Replies: 10
    Last Post: 05-28-2008, 09:25 AM
  4. Creating Calendar in Applet
    By wco5002 in forum New To Java
    Replies: 2
    Last Post: 04-09-2008, 05:21 AM
  5. Replies: 4
    Last Post: 06-08-2007, 07:15 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
  •