4 Replies - 187 Views - Last Post: 02 March 2012 - 05:00 PM Rate Topic: -----

#1 jamescox92  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 02-February 12

applet button error

Posted 02 March 2012 - 07:05 AM

i am unable to get the button to work so that when i type in lake into the text field it will then find the definition i have created for it to be displayed within the applet. what have i done wrong?


import java.awt.*; 
import java.applet.*; 
import java.awt.event.*;

public class buttont extends Applet implements ActionListener 
{

     Button okButton;  
     TextField nameField; 
     int  key_word=-1, pr_xCord, pr_yCord, pr_size=10, pr_definition=-1;
  

     public void init()  
     { 
  // Now we will use the FlowLayout 
          setLayout(new FlowLayout()); 
          okButton = new Button("information"); 
          okButton.addActionListener(this);
          nameField = new TextField("Type here Something",35); 
          
          add(okButton); 
          add(nameField); 
         

  // Attach actions to the components 
          okButton.addActionListener(this);  
         }

 // Here we will show the results of our actions 
         public void paint(Graphics g) 
         { 

        	 {
        	        g.setColor(Color.white);
        	        switch(pr_definition)
        	        {
        	            case(1):
        	                showStatus("lake");
        	                g.setColor(Color.red);
        	                g.fillRect(pr_xCord,pr_yCord,pr_size,pr_size);
        	                break;
        	            case(0):
        	                showStatus("playground");
        	                g.setColor(Color.blue);
        	                g.fillOval(pr_xCord,pr_yCord,pr_size,pr_size);
        	                break;
        	        }
        	         
        	    }
          g.drawString(nameField.getText(),20,100); 
     }


        public void actionPerformed(ActionEvent evt)  
         { 

             

                 
				evt.getSource();
                  nameField.setText("");
                  if(evt.equals("lake"))
                      pr_definition=0;
                  else
                      if(evt.equals("playground"))
                          pr_definiton=1;
                      else
                          showStatus("Can't understand " );
                   repaint();

          } 
     } 




Is This A Good Question/Topic? 0
  • +

Replies To: applet button error

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2657
  • View blog
  • Posts: 11,214
  • Joined: 20-September 08

Re: applet button error

Posted 02 March 2012 - 07:45 AM

Quote

if(evt.equals("lake"))


will never be true. An ActionEvent is not a String

String s = nameField.getText();
if("lake".equals(s))


is what you need
Was This Post Helpful? 0
  • +
  • -

#3 jamescox92  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 02-February 12

Re: applet button error

Posted 02 March 2012 - 07:59 AM

ive managed to change it to that however when i type in lake it just prints the string lake not the string i have created for it?
Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2657
  • View blog
  • Posts: 11,214
  • Joined: 20-September 08

Re: applet button error

Posted 02 March 2012 - 09:44 AM

Please say how this is meant to work - it's confusing
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: applet button error

Posted 02 March 2012 - 05:00 PM

Post your updated code
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1