Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. Java Applet Help

Java Applet Help

Scheduled Pinned Locked Moved Java
javahelpquestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 10694570
    wrote on last edited by
    #1

    I am having trouble in only the last line of code. (I think!) The first label prompts the user for their name. If they click the button, then it will change the second label to read "Hello, (username)!" Can anyone give me some pointers?

    import java.applet.Applet;
    import java.awt.Button;
    import java.awt.Color;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JLabel;
    import javax.swing.JTextField;

    public class Greeting extends Applet implements ActionListener {

    JLabel labelName = new JLabel("Please enter your name.");
    JTextField textField = new JTextField(20);
    Button button = new Button("GREET");
    JLabel labelResult = new JLabel("Result goes here.");
    
    String userResult = "";
    
    public void init() {
    	setSize(500, 500);
    	setBackground(Color.yellow);
    	add(labelName);
    	add(textField);
    	add(button);
    	add(labelResult);
    
    	button.addActionListener(this);
    }
    
    public String getText() {
    	return userResult;
    }
    
    public void setName(String result){
    	userResult = result;
    }
    
    public void actionPerformed(ActionEvent event) {
    	if (event.getSource() == button) {
    		labelResult.setText("Hello, " + userResult);
    	}
    
    }
    

    }

    L 1 Reply Last reply
    0
    • U User 10694570

      I am having trouble in only the last line of code. (I think!) The first label prompts the user for their name. If they click the button, then it will change the second label to read "Hello, (username)!" Can anyone give me some pointers?

      import java.applet.Applet;
      import java.awt.Button;
      import java.awt.Color;
      import java.awt.event.ActionEvent;
      import java.awt.event.ActionListener;
      import javax.swing.JLabel;
      import javax.swing.JTextField;

      public class Greeting extends Applet implements ActionListener {

      JLabel labelName = new JLabel("Please enter your name.");
      JTextField textField = new JTextField(20);
      Button button = new Button("GREET");
      JLabel labelResult = new JLabel("Result goes here.");
      
      String userResult = "";
      
      public void init() {
      	setSize(500, 500);
      	setBackground(Color.yellow);
      	add(labelName);
      	add(textField);
      	add(button);
      	add(labelResult);
      
      	button.addActionListener(this);
      }
      
      public String getText() {
      	return userResult;
      }
      
      public void setName(String result){
      	userResult = result;
      }
      
      public void actionPerformed(ActionEvent event) {
      	if (event.getSource() == button) {
      		labelResult.setText("Hello, " + userResult);
      	}
      
      }
      

      }

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      You have not included any code to get the user's name from textField. And your getter and setter are mismatched. You need something like:

      public String getName() {
          return userResult;
      }
      
      public void setName(String result){
          userResult = result;
      }
      
      public void actionPerformed(ActionEvent event) {
          if (event.getSource() == button) {
              setName(textField.toString());
              labelResult.setText("Hello, " + getName());
          }
      
      }
      
      A 1 Reply Last reply
      0
      • L Lost User

        You have not included any code to get the user's name from textField. And your getter and setter are mismatched. You need something like:

        public String getName() {
            return userResult;
        }
        
        public void setName(String result){
            userResult = result;
        }
        
        public void actionPerformed(ActionEvent event) {
            if (event.getSource() == button) {
                setName(textField.toString());
                labelResult.setText("Hello, " + getName());
            }
        
        }
        
        A Offline
        A Offline
        anaQata
        wrote on last edited by
        #3

        someone told me applets are dead? :^)

        L 1 Reply Last reply
        0
        • A anaQata

          someone told me applets are dead? :^)

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Someone told me that Leslie was alive.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups