Applet problem

Quit button doesnt respond, program keeps looping

Page 1 of 1

3 Replies - 1033 Views - Last Post: 06 April 2007 - 04:07 PM Rate Topic: -----

#1 gius78  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 05-April 07

Applet problem

Posted 05 April 2007 - 02:29 AM

Hi people really cant figure this out. My applet wont stop when I click the "Quit" button. The program wont stop loopin, I tryed using a sentinel but it doesnt work. Can any one help?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Garage2 extends JApplet implements ActionListener {
	
	//declare variables for labels, text fields and buttons
	JLabel welcome, prompt,amount;
	JTextField hoursTxt,price;
	JButton process, quit;
	
	public void init(){
		
		// initialize applet	
		Container c = getContentPane();//create an area for the applet
		c.setLayout (new FlowLayout());// set a layout for the objects
		
		//create objects to be on your applet
		welcome = new JLabel ("-Welcome to the City Central Car Park-");
		welcome.setFont(new Font("Arial", Font.BOLD, 14));
		prompt = new JLabel ("Please enter hours parked:");
		hoursTxt = new JTextField(5);
		amount = new JLabel ("Amount Due is:");
		price = new JTextField(7);
		process = new JButton("Process Payment");
		quit = new JButton("Quit");
		
		//add the objects to the applet 
		c.add(welcome);
		c.add(prompt);
		c.add(hoursTxt);
		c.add(amount);
		c.add(price);
		price.setEditable(false);
		c.add(process);
		c.add(quit);
		process.addActionListener(this);
		hoursTxt.addActionListener(this);
		quit.addActionListener(this);
		price.addActionListener(this);
	}
	public void actionPerformed(ActionEvent evt){
	
	   // end program if quit button is clicked
	   if(evt.getSource()== quit){
	   	  
		  System.exit(0);	
	   }
	   //process payment if process button is clicked
	   if(evt.getSource() == process){
		  calculate();
		  
	   } 	   
   	}
   	
   	public void calculate(){
   		
   		// pass the contents of the text field into a variable
   		String hoursStr = hoursTxt.getText();
   			
   		//convert from string to double
   		double hours = Double.parseDouble (hoursStr);
   		
   		//set variables for calculations
   		double overpay=0, fixedRate = 0,max = 15.5,min = 3,total=0;
  
   		while (hours < 0){
   	
   		   if (hours >= min){
   		  
   			  overpay = ((hours - 3)*2);
   			  fixedRate = overpay + 5;
   			  price.setText(" " + fixedRate +" ");
   			  hoursTxt.setText("");
   			  hoursTxt.requestFocus();
   			  
   		   }
   			  
   		   if (hours >= max){
   			  price.setText(" 30.00 ");
   			  hoursTxt.setText("");
   			  hoursTxt.requestFocus();
   			  fixedRate = 30.00;
   		   }
   		   
   		   if (hours < min){
   			  price.setText(" 5.00 ");
   			  hoursTxt.setText("");
   			  hoursTxt.requestFocus();
   			  fixedRate = 5.00;
   		   }
   			  
   		  customer++;
   		 //show results in the status bar
   		  showStatus ("Customers today: " + customer +"   Total: "+fixedRate+"  ");
   		   		
   		}
   		
	  } 
   	  
}

This post has been edited by William_Wilson: 05 April 2007 - 08:33 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Applet problem

#2 GWatt  Icon User is offline

  • member icon

Reputation: 269
  • View blog
  • Posts: 3,067
  • Joined: 01-December 05

Re: Applet problem

Posted 05 April 2007 - 06:28 AM

You can't really exit from an applet. What you can do is use the setEnabled() method for every component. You Can also reset the component's locations to be off the screen, and then paint over the entire applet area.
Was This Post Helpful? 0
  • +
  • -

#3 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 205
  • View blog
  • Posts: 4,807
  • Joined: 23-December 05

Re: Applet problem

Posted 05 April 2007 - 08:36 AM

there is also threading available for loops, run the loop in a thread and terminate the thread on pressing the button.
Was This Post Helpful? 0
  • +
  • -

#4 royroy7  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 06-April 07

Re: Applet problem

Posted 06 April 2007 - 04:07 PM

sound good
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1