Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By doWhile

Thread: problem with the button "exit"

  1. #1
    mike28 is offline Member
    Join Date
    Oct 2011
    Posts
    20
    Rep Power
    0

    Default problem with the button "exit"

    the program suppose to invoke the following URL Google When exit is selected, all the other buttons are working fine but when I click on "exit" nothing happens.

    please help :)))




    Java Code:
    import java.awt.*;
    import java.lang.*;
    import java.applet.*;
    import java.net.*;
    import java.net.MalformedURLException;
    import java.applet.Applet.*;
    import java.io.IOException;
    
    public class Bounce extends Applet {
    
        public Stage      stage;
    	URL url;
        GridBagLayout     layout;
        Button            addButton  = new Button( "add ball" );
        Button            fastButton = new Button( "faster" );
        Button            slowButton = new Button( "slower" );
    
    
        public void init() {
    	setBackground( Color.white );
    	layout = new GridBagLayout();
            setLayout( layout );
    	int width = Integer.parseInt( getParameter( "width" ).trim() );
    	int height = Integer.parseInt( getParameter( "height" ).trim() );
            stage = new Stage( width-100,height-50 );
    	constrain( stage,this,layout,1,0,1,6,
    		   GridBagConstraints.NONE,
    		   GridBagConstraints.CENTER,
    		   0,0 );
    	constrain( addButton,this,layout,0,0,1,1,
    		   GridBagConstraints.BOTH,
    		   GridBagConstraints.CENTER,
    		   0.3,0 );
    	constrain( fastButton,this,layout,0,1,1,1,
    		   GridBagConstraints.BOTH,
    		   GridBagConstraints.CENTER,
    		   0.3,0 );
    	constrain( slowButton,this,layout,0,2,1,1,
    		   GridBagConstraints.BOTH,
    		   GridBagConstraints.CENTER,
    		   0.3,0 );
        } // end of init()
    
    
        public void start() {
            stage.start();
        } // end of start()
    
    
        public void stop() {
            stage.stop();
        } // end of stop()
    
        public void destroy() {
        } // end of destroy()
    
        public boolean action( Event evt, Object arg ) {
    	if ( evt.id == Event.ACTION_EVENT ) {
    	    if ( evt.target == addButton ) {
    		stage.addBall();
    		stage.start();
    		return true;
    	    }
    	    else if ( evt.target == fastButton ) {
    		stage.faster();
    		stage.start();
    		return true;
    	    }
    	    else if ( evt.target == slowButton ) {
    		stage.slower();
    		stage.start();
    		return true;
    	    }
    	}
    	return false;
        } // end of action()
    
        public static void constrain(
    				 Component     component,
    				 Container     container,
    				 GridBagLayout layout,
    				 int           gridx,
    				 int           gridy,
    				 int           gridwidth,
    				 int           gridheight,
    				 int           fill,
    				 int           anchor,
    				 double        weightx,
    				 double        weighty
    				 ) {
    
    	GridBagConstraints gbc = new GridBagConstraints();
    
    	gbc.gridx      = gridx;
    	gbc.gridy      = gridy;
    	gbc.gridwidth  = gridwidth;
    	gbc.gridheight = gridheight;
    	gbc.fill       = fill;
    	gbc.anchor     = anchor;
    	gbc.weightx    = weightx;
    	gbc.weighty    = weighty;
    
    	layout.setConstraints( component,gbc );
    
    	container.add( component );
    
        } // end of constrain()
    
    
    
    } // end of Bounce class
    Java Code:
    import java.awt.*;
    import java.lang.*;
    import java.applet.*;
    import java.net.*;
    import java.applet.Applet.*;
    import java.io.IOException;
    
    public class Bounce2 extends Bounce1
    {
    	Button            startButton  = new Button( "start" );
    	Button            exitButton  = new Button( "exit" );
    
    	public void init()
    	{
    		super.init();
    		constrain( startButton,this,layout,0,4,1,1,
    				   GridBagConstraints.BOTH,
    				   GridBagConstraints.CENTER,
    		   0.3,0 );
    		constrain( exitButton,this,layout,0,5,1,1,
    				   GridBagConstraints.BOTH,
    				   GridBagConstraints.CENTER,
    		   0.3,0 );
     	}
    
        public boolean action( Event evt, Object arg )
        {
    		super.action( evt, arg );
    	    if( evt.target == startButton )
    	    {
    			stage.start();
    			return true;
    		}
    		else if( evt.target == exitButton )
    		{
    			this.exit();
    
    			return true;
    		}
    		return false;
    	}
    	public void exit(){
    			try {
    			  URL url = new URL( "http://www.google.com" );
    			  getAppletContext().showDocument( url );
    			}
    			catch( MalformedURLException murlx ) {}
    			catch( IOException iox ) {}
    	}
    }

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    6

    Default Re: problem with the button "exit"

    I don't know where you are getting tutorials or instruction from, but I would highly recommend finding a different source - the action method has been deprecated since java v1.1 ! In other words, don't use it - use an ActionListener. And for future reference, please post an SSCCE

  3. #3
    mike28 is offline Member
    Join Date
    Oct 2011
    Posts
    20
    Rep Power
    0

    Default Re: problem with the button "exit"

    if ill use ActionListener it will fix my "exit" button?

  4. #4
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    6

    Default Re: problem with the button "exit"

    Quote Originally Posted by mike28 View Post
    if ill use ActionListener it will fix my "exit" button?
    You can try to 'fix' your 'exit' button relying on poor and deprecated code (why not add some println in there to debug the flow of your program), or you can write up to date code that is more reliable, easier to debug, and something which many of the contributors here - including myself - are more familiar with. Take your pick.
    Fubarable likes this.

Similar Threads

  1. Not detecting "exit" in client
    By noahssite in forum Networking
    Replies: 1
    Last Post: 08-18-2011, 11:24 PM
  2. Replies: 6
    Last Post: 05-18-2010, 05:11 PM
  3. problem with argument list and precedence "(" and ")"
    By helpisontheway in forum Advanced Java
    Replies: 6
    Last Post: 12-24-2009, 07:50 AM
  4. How to exit the program by typing "exit"?
    By Laythe in forum New To Java
    Replies: 6
    Last Post: 08-19-2009, 08:32 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
  •