Adding Text Field for input

/*

* File: CyberPet.java

* Author: Java, Java, Java

* Description: This class represents a CyberPet that can

* eat and sleep on command. This version incorporates

* a public getState() method to report the pet's state.

*/

publicclass CyberPet

{

privateboolean isEating =true;// CyberPet's state

privateboolean isSleeping =false;

privateboolean isThinking =false;

private String name ="no name";// CyberPet's name

public CyberPet (String str)// Constructor method

{

name = str;

}

publicvoid setName (String str)// Access method

{

name = str;

}// setName()

public String getName()

{

return name;// Return CyberPet's name

}// getName()

publicvoid eat()// Start eating

{

isEating =true;// Change the state

isSleeping =false;

isThinking =false;

return;

}// eat()

publicvoid sleep()// Start sleeping

{

isSleeping =true;// Change the state

isEating =false;

isThinking =false;

return;

}// sleep()

publicvoid think()

{

isThinking =true;

isSleeping =false;

isEating =false;

return;

}

public String getState ()

{

if (isEating)

return"Eating";// Exit the method

if (isSleeping)

return"Sleeping";// Exit the method

if (isThinking)

return"Thinking";

return"Error in State";// Exit the method

}// getState()

public String toString()

{

return name +" is " + getState();

}

}// CyberPet

/*

* Description: This apply provides a graphical user

* interface to the CyberPet class. The interface consists

* of two Buttons that can be clicked to tell the CyberPet

* to eat or drink, and a TextField which reports the

* CyberPet's state.

*

* The interface is initialized in the init() method and

* user actions are handled in the actionPerformed() method.

*/

import java.applet.*;

import java.awt.*;

import java.awt.event.*;

//STARTING COMMENT LINE ENCLOSING CLASS

publicclass CyberPetAppletextends Appletimplements ActionListener

{

// Declare instance variables

private CyberPet pet1;// The CyberPet

private Label nameLabel;// A Label

private TextField stateField;// A TextField

private Button eatButton, sleepButton, thinkButton;// Two Buttons

//* The init() method instantiates the instance variables, including both the

//* CyberPet (pet1) and the GUI elements that are displayed on the applet.

publicvoid init()

{

pet1 =new CyberPet("Jack");// CyberPet

// Create the GUI components

nameLabel =new Label("Hi! My name is " + pet1.getName() +

" and currently I am : ");

stateField =new TextField(12);

eatButton =new Button("Eat!");// Buttons

eatButton.addActionListener(this);// Assign the listener for Eat

sleepButton =new Button("Sleep!");

sleepButton.addActionListener(this);

thinkButton =new Button ("Think!");

thinkButton.addActionListener(this);

// Initialize the TextField

stateField.setText(pet1.getState());

stateField.setEditable(false);

// Add the components to the applet.

add(nameLabel);

add(stateField);

add(eatButton);

add(sleepButton);

add(thinkButton);

setSize(300,150);// Set the applet's size to 300 x 150 pixels

}// init

/*

* The actionPerformed() method is called whenever

* one of the buttons is pressed.

*/

publicvoid actionPerformed( ActionEvent e)

{

if (e.getSource() == eatButton)

pet1.eat();

elseif (e.getSource() == sleepButton)

pet1.sleep();

elseif (e.getSource() == thinkButton)

pet1.think();

stateField.setText(pet1.getState());

}//actionPerformed

}

//ENDING COMMENT LINE ENCLOSING CLASS*/

so far i did this in lab i have to mod this code for another text field and than have what is entered in the 2nd text field be the "pets" name

im not really sure how to do this because most of this code we got as skeleton code

so any pointers in the right direction would be great

Thanks,

Tom

[9064 byte] By [tk3445a] at [2007-11-26 12:20:14]
# 1

You already have a textfield called stateField, so add another textfield called something like petnameField. You'll basically do the same things with it as you did with the stateField, so go through the code and look at what you do with it. Once you figure out what each line does to the stateField, then do the same to the petnameField.

hunter9000a at 2007-7-7 15:10:02 > top of Java-index,Archived Forums,Socket Programming...
# 2
ok i have got it to disply the text box how do i make it so what is entered is displayed as the name?
tk3445a at 2007-7-7 15:10:02 > top of Java-index,Archived Forums,Socket Programming...
# 3

/*

* File: CyberPet.java

* Author: Java, Java, Java

* Description: This class represents a CyberPet that can

* eat and sleep on command. This version incorporates

* a public getState() method to report the pet's state.

*/

public class CyberPet

{

private boolean isEating = true;// CyberPet's state

private boolean isSleeping = false;

private boolean isThinking = false;

private String name = "no name";// CyberPet's name

public CyberPet (String str)// Constructor method

{

name = str;

}

public void setName (String str)// Access method

{

name = str;

} // setName()

public String getName()

{

return name;// Return CyberPet's name

} // getName()

public void eat() // Start eating

{

isEating = true;// Change the state

isSleeping = false;

isThinking = false;

return;

} // eat()

public void sleep()// Start sleeping

{

isSleeping = true;// Change the state

isEating = false;

isThinking = false;

return;

} // sleep()

public void think()

{

isThinking = true;

isSleeping = false;

isEating = false;

return;

}

public String getState ()

{

if (isEating)

return "Eating";// Exit the method

if (isSleeping)

return "Sleeping";// Exit the method

if (isThinking)

return "Thinking";

return "Error in State"; // Exit the method

} // getState()

public String toString()

{

return name + " is " + getState();

}

} // CyberPet

/*

* Description: This apply provides a graphical user

* interface to the CyberPet class. The interface consists

* of two Buttons that can be clicked to tell the CyberPet

* to eat or drink, and a TextField which reports the

* CyberPet's state.

*

* The interface is initialized in the init() method and

* user actions are handled in the actionPerformed() method.

*/

import java.applet.*;

import java.awt.*;

import java.awt.event.*;

//STARTING COMMENT LINE ENCLOSING CLASS

public class CyberPetApplet extends Applet implements ActionListener

{

// Declare instance variables

private CyberPet pet1;// The CyberPet

private Label nameLabel;// A Label

private TextField stateField;// A TextField

private TextField petNameField;

private Button eatButton, sleepButton, thinkButton;// Two Buttons

//* The init() method instantiates the instance variables, including both the

//* CyberPet (pet1) and the GUI elements that are displayed on the applet.

public void init()

{

pet1 = new CyberPet();// CyberPet

// Create the GUI components

nameLabel = new Label("Hi! My name is " + pet1.getName() +

" and currently I am : ");

stateField = new TextField(12);

petNameField = new TextField(20);

eatButton = new Button("Eat!");// Buttons

eatButton.addActionListener(this); // Assign the listener for Eat

sleepButton = new Button("Sleep!");

sleepButton.addActionListener(this);

thinkButton = new Button ("Think!");

thinkButton.addActionListener(this);

// Initialize the TextField

stateField.setText(pet1.getState());

stateField.setEditable(false);

petNameField.setText((" "));

petNameField.setEditable(true);

petNameField.addActionListener((this));

// Add the components to the applet.

add(nameLabel);

add(stateField);

add(petNameField);

add(eatButton);

add(sleepButton);

add(thinkButton);

setSize(300,150); // Set the applet's size to 300 x 150 pixels

} // init

/*

* The actionPerformed() method is called whenever

* one of the buttons is pressed.

*/

public void actionPerformed( ActionEvent e)

{

if (e.getSource() == eatButton)

pet1.eat();

else if (e.getSource() == sleepButton)

pet1.sleep();

else if (e.getSource() == thinkButton)

pet1.think();

stateField.setText(pet1.getState());

}//actionPerformed

}

//ENDING COMMENT LINE ENCLOSING CLASS*/

here is my new code i just dont know what to put under pet1 = new Cyberpet() i have to put somthing in the () but im not quite sure how to call it from the 2nd text box

PLEASE HELP!!!

Thanks,

Tom

tk3445a at 2007-7-7 15:10:02 > top of Java-index,Archived Forums,Socket Programming...