Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-03-2008, 02:10 AM
Member
 
Join Date: Jan 2008
Posts: 18
jimJohnson is on a distinguished road
trouble with program
i am finishing a java program up and a little confused....I need a clear button and know that is with the action listener but already have an item listener and confused on how to put a second one with a deadline in a couple hours midnight to be exact

output should be
Initial screen:

Enter the average number of hours you are home per week
House Apartment dormitory

Select your type of housing to determine the best pet

Clear Fields Button

Screen with valid Input


Enter the average number of hours you are home per week
House Apartment dormitory

Select your type of housing to determine the best pet
According to your requirements I would recommend a ....

Clear Fields Button

Invalid screen
Enter the average number of hours you are home per week
House Apartment dormitory

Select your type of housing to determine the best pet
Enter a valid number of hours between 0 and 168
Clear Fields Button

Code:
//packages to import import java.awt.*; import java.applet.*; import java.awt.event.*; public class PetAdviceApplet extends Applet implements ItemListener { //declare variables and construct components int housing; double hours; String pet; Label promptLabel = new Label ("Enter the average number of hours you are home per week "); TextField hoursField = new TextField(15); CheckboxGroup housingGroup = new CheckboxGroup(); Checkbox houseBox = new Checkbox("House", false, housingGroup); Checkbox apartmentBox = new Checkbox("Apartment", false, housingGroup); Checkbox dormitoryBox = new Checkbox("Dormitory", false, housingGroup); Checkbox hiddenBox = new Checkbox("",true,housingGroup); Label housingLabel = new Label ("Select your type of housing to determine the best pet "); Label outputLabel = new Label (" "); Button clearButton = new Button("Clear Fields"); public void init() { setBackground(Color.red); setForeground(Color.black); add(promptLabel); add(hoursField); hoursField.requestFocus(); hoursField.setForeground(Color.black); add(houseBox); houseBox.addItemListener(this); add(apartmentBox); apartmentBox.addItemListener(this); add(dormitoryBox); dormitoryBox.addItemListener(this); add(housingLabel); add(outputLabel); } //end init() //This method is triggered by the user clicking an option button public void itemStateChanged(ItemEvent choice) { try { housing = getHousing(); hours = getHours() ; pet = getPet(hours, housing); output(pet); } //end try catch(NumberFormatException e) { outputLabel.setText("Enter a valid number for hours between 0 and 168 "); hiddenBox.setState(true); hoursField.setText(""); hoursField.requestFocus(); } //end catch } public double getHours() { double hours = Double.parseDouble(hoursField.getText()); if ((hours <= 0) || (hours >= 168)) throw new NumberFormatException(); return hours; } //end getHours() public int getHousing() { int housing = 0; if (houseBox.getState()) housing = 1; else if (apartmentBox.getState()) housing = 2; else if (dormitoryBox.getState()) housing = 3; return housing; } //end getHousing() public String getPet(double hours, int housing) { //declare method variables String pet = ""; switch(housing) { case 1: //pot-bellied pig if ((housing == 1) && (hours>=18)) { pet = "Pot-bellied pig"; } case 2: //dog if((housing == 1) && (hours>=10) && (hours < 18)) { pet = "Dog"; } case 3: //snake if ((housing == 1) && (hours < 10)) { pet = "Snake"; } case 4: //cat if ((housing == 2) && (hours >= 10)) { pet = "Cat"; } case 5: //hamster if ((housing == 2) && (hours < 10)) { pet = "Hamster"; } case 6: //fish if ((housing == 3) && (hours >= 6)) { pet = "Fish"; } case 7: //ant farm if ((housing == 3) && (hours < 6)) { pet = "Ant farm"; } break; }//end switch return pet; }//end getComm public void output(String pet) { outputLabel.setText ("According to your requirements, I would recommend a " + pet); validate(); } //end output }//end class

I hope I explained that clear enough...if not I will be back at 830 to explain it better
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 04-03-2008, 10:29 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,191
hardwired is on a distinguished road
Code:
// <applet code="PAA" width="400" height="400"></applet> import java.awt.*; import java.applet.*; import java.awt.event.*; public class PAA extends Applet implements ItemListener, ActionListener { //declare variables and construct components int housing; double hours; String pet; Label promptLabel = new Label ("Enter the average number " + "of hours you are home per week "); TextField hoursField = new TextField(15); CheckboxGroup housingGroup = new CheckboxGroup(); Checkbox houseBox = new Checkbox("House", false, housingGroup); Checkbox apartmentBox = new Checkbox("Apartment", false, housingGroup); Checkbox dormitoryBox = new Checkbox("Dormitory", false, housingGroup); Checkbox hiddenBox = new Checkbox("",true,housingGroup); Label housingLabel = new Label ("Select your type of housing " + "to determine the best pet "); Label outputLabel = new Label (" "); Button clearButton = new Button("Clear Fields"); public void init() { setBackground(Color.red); setForeground(Color.black); clearButton.addActionListener(this); add(promptLabel); add(hoursField); hoursField.requestFocus(); hoursField.setForeground(Color.black); add(houseBox); houseBox.addItemListener(this); add(apartmentBox); apartmentBox.addItemListener(this); add(dormitoryBox); dormitoryBox.addItemListener(this); add(housingLabel); add(outputLabel); add(clearButton); } //end init() //This method is triggered by the user clicking an option button public void itemStateChanged(ItemEvent choice) { try { housing = getHousing(); hours = getHours(); pet = getPet(hours, housing); output(pet); } //end try catch(NumberFormatException e) { outputLabel.setText("Enter a valid number for hours " + "between 0 and 168 "); hiddenBox.setState(true); hoursField.setText(""); hoursField.requestFocus(); } //end catch } public void actionPerformed(ActionEvent e) { hoursField.setText(""); hiddenBox.setState(true); outputLabel.setText(""); } public double getHours() { double hours = Double.parseDouble(hoursField.getText()); if ((hours <= 0) || (hours >= 168)) throw new NumberFormatException("hours must >= 0 and <= 168"); return hours; } //end getHours() public int getHousing() { int housing = 0; if (houseBox.getState()) housing = 1; else if (apartmentBox.getState()) housing = 2; else if (dormitoryBox.getState()) housing = 3; return housing; } //end getHousing() public String getPet(double hours, int housing) { //declare method variables String pet = ""; switch(housing) { case 1: // house if (hours>=18) { pet = "Pot-bellied pig"; } else if(hours>=10 && hours < 18) { pet = "Dog"; } else if (hours < 10) { pet = "Snake"; } break; case 2: // apartment if (hours >= 10) { pet = "Cat"; } else if (hours < 10) { pet = "Hamster"; } break; case 3: // dorm if (hours >= 6) { pet = "Fish"; } else if (hours < 6) { pet = "Ant farm"; } }//end switch return pet; }//end getComm public void output(String pet) { outputLabel.setText ("According to your requirements, " + "I would recommend a " + pet); validate(); } //end output }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
compiling trouble capacitator CLDC and MIDP 4 06-10-2008 11:12 PM
[SOLVED] trouble learnig swing monir6464 AWT / Swing 5 05-08-2008 07:01 AM
Compile Trouble adelgado0723 New To Java 5 04-21-2008 03:02 AM
Having trouble with array ice22 New To Java 3 11-13-2007 04:06 AM
JTree trouble Alantie Vala AWT / Swing 3 08-01-2007 12:12 AM


All times are GMT +3. The time now is 10:50 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org