Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Adding Text Field for input

807607Dec 12 2006 — edited Dec 12 2006
/*
 * 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 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("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.
     */
    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*/
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
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 9 2007
Added on Dec 12 2006
3 comments
102 views