Results 1 to 7 of 7
  1. #1
    anithababu is offline Member
    Join Date
    Jan 2008
    Posts
    4
    Rep Power
    0

    Default A great doubt in Java Applet,will u solve it!!

    I have created an applet where I have created 3 buttons for the following actions:
    1.A drawn line should appear! After clicking each button which is at different coordination.
    BUT ONLY THE APPLET STARTS BUT NOTHING IS DISPLAYED!!,ONLY BLANK WINDOW.
    Please just RUN THE SOURCE CODE which I have tried out!! So that you get a clear idea of my problem


    MY PROGRAM:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;

    public class MyButton extends JApplet implements ActionListener
    {

    int i;
    JButton one,two,three;

    Container con;
    public void init()
    {
    one=new JButton("one");

    two=new JButton("two");

    three=new JButton("three");
    con=getContentPane();
    con.setLayout(new FlowLayout());
    con.add(one);

    con.add(two);

    con.add(three);
    one.addActionListener(this);
    two.addActionListener(this);

    three.addActionListener(this);
    }


    public void actionPerformed(ActionEvent e)
    {
    if (e.getSource()==one)
    {
    i=1;
    repaint();
    }
    else
    if (e.getSource()==two)
    {
    i=2;
    repaint();
    }
    if (e.getSource()==three)
    {
    i=3;
    repaint();
    }
    }
    public void paint(Graphics g)
    {
    if(i==1){
    g.drawLine(180,40,20,100);
    }


    else if(i==2)
    {
    g.drawLine(140,20,30,100);
    }
    if(i==3)
    {
    g.drawLine(120,25,20,100);
    }

    }
    /*<APPLET CODE="MyButton.class" HEIGHT=200 WIDTH=200>
    </APPLET>
    */
    }





    IF YOU FOUND ANY BUG IN THIS PROGRAM,PLEASE EDIT IT!!
    THANKS!!!

  2. #2
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    9

    Default

    I'm not too familiar with applets because I choose not to use them, however I did run your code which compiled without errors. Three buttons appeared which when clicked each drew a line skewed to the left.

    Please use code tags when posting your source. Where is this "great doubt" you have? You have "great doubt" over an applet? I'm confused.. this must be a phrase I'm not familiar with or are there a growing group of folks from one location that use similar handles and always experience "doubt". Have confidence in your code writing.
    Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
    Want to voice your opinion on your IDE/Editor of choice? Vote now!
    Got a little Capt'n in you? (drink responsibly)

  3. #3
    anithababu is offline Member
    Join Date
    Jan 2008
    Posts
    4
    Rep Power
    0

    Default

    Thank u sir,but when i try to run in applet window after compiling using the command appletviewer MyButton.java i see only blank applet window with the msg applet started at the bottom of the applet window.

  4. #4
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    9

    Default

    I'm not familiar with applets, as I previously stated. So hopefully someone with better experience will assist you. I will just add that the buttons appear - only when you hover over their area with your mouse.... so maybe it's a focusing issue? Or something to do with having them show by default? Have you looked over the official docs? I also notice you don't call start() even though it's automatically called after init() you don't seem to have an implementation for it...

    EDIT: also, you should apply this to your html page if that's where you are planning to deploy it. I just now put it on a simple html page and it functioned as it should have, with the buttons appearing upon page opening.
    Last edited by CaptainMorgan; 01-14-2008 at 08:38 PM.
    Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
    Want to voice your opinion on your IDE/Editor of choice? Vote now!
    Got a little Capt'n in you? (drink responsibly)

  5. #5
    sabbirhs is offline Member
    Join Date
    Jan 2008
    Posts
    6
    Rep Power
    0

    Default

    What is the version of your JDK. javax do not work with some special version.

    Try to focuse on it.
    Take care.

  6. #6
    anithababu is offline Member
    Join Date
    Jan 2008
    Posts
    4
    Rep Power
    0

    Default

    Thank u sir it works! I used older version,now i changed.

  7. #7
    sabbirhs is offline Member
    Join Date
    Jan 2008
    Posts
    6
    Rep Power
    0

Similar Threads

  1. Great Demand For IT Professionals.
    By avularamesh4u in forum Jobs Discussion
    Replies: 2
    Last Post: 12-30-2008, 06:05 PM
  2. Help mi solve my error
    By Deon in forum New To Java
    Replies: 3
    Last Post: 01-11-2008, 06:26 AM
  3. great book for teenager to learn Java?
    By 21rouge in forum New To Java
    Replies: 8
    Last Post: 12-03-2007, 06:23 AM
  4. Cannot solve the coding problem of my assignment
    By elimmom in forum New To Java
    Replies: 3
    Last Post: 08-13-2007, 12:33 PM
  5. Replies: 3
    Last Post: 07-30-2007, 08:11 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
  •