Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Issues with JApplet and Text Fields

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Issues with JApplet and Text Fields

    Hi, I'm trying to create a game in which a random song title comes up and the user will have to enter the correct singer. My issue right now is I'm having difficulty making it so that the user's input is checked and then the text field is reset. I'm not even sure if this is possible, because no matter how hard I've looked I haven't found anything. Thanks for any help you can give me! Here's my code so far:

    package final_project;
     
    import java.applet.Applet;
    import java.awt.*;    // import the java.awt package
    import java.awt.event.*;  // import the java.awt.event package
    import java.util.Random;
     
    import javax.swing.JButton;
    import javax.swing.JTextArea;
     
    public class NameThatBand extends Applet implements ActionListener, MouseListener{
     
    	String song;
     
    	String[] songArray={"Let It Be","Where Is My Mind?", "Pet Sounds"};
     
    	JTextArea textArea;
    	TextField input;
    	Label prompt;
    	JButton button;
    	//The x and y coordinates of the last click:
    	int xpos;
    	int ypos;
    	//The coordinates of the rectangle that will need to be clicked:
    	int rect1xco,rect1yco,rect1width,rect1height;
    	//Test whether the click is within the applet area:
    	boolean mouseEntered;
    	//Test whether the click is within rect1:
    	boolean rect1Clicked;
     
    	int randomNum;
     
    	int score;
     
     
     
    	public void init(){
    		setSize(500,500);
    		prompt = new Label("Who Sings It?");
    		input = new TextField(5);
     
    		add(prompt);                                    // put prompt on applet
     
            add(input);                                     // put input TextField on applet
     
            input.addActionListener( this );                // "this" applet handles action events for TextField input
     
     
            //Rectangle coordinates:
            rect1xco = 0;
            rect1yco = 0;
            rect1width=1000;
            rect1height=5000;
     
            addMouseListener(this);
     
    	}
     
    	public void actionPerformed(ActionEvent e) {
     
    		input.equals((e.getActionCommand()));
    		String text = input.getText();
     
    		while(songArray[randomNum].equalsIgnoreCase("Where is my mind?")){
    			if (text.equalsIgnoreCase("The Pixies")){
    				score++;
    				input.setText("");
    				 repaint();
    				 actionPerformed(e);
    			}
    			else{
    				input.setText("");
    				repaint();
    				actionPerformed(e);
    			}
    		}
    		while(songArray[randomNum].equalsIgnoreCase("Pet Sounds")){
    			if (text.equalsIgnoreCase("The Beach Boys")){
    				score++;
    				input.setText("");
    				repaint();
    				actionPerformed(e);
    			}
    			else{
    				input.setText("");
    				repaint();
    				actionPerformed(e);
    			}
    		}
    		while(songArray[randomNum].equalsIgnoreCase("Let It Be")){
    			if (text.equalsIgnoreCase("The Beatles")){
    				score++;
    				input.setText("");
    				 repaint();
    				 actionPerformed(e);
    			}
    			else{
    				input.setText("");
    				repaint();
    				actionPerformed(e);
    			}
    		}
    	}
     
    	public void paint(Graphics g){
     
    		g.setColor(Color.white);
     
    		g.fillRect(rect1xco, rect1yco, rect1width, rect1height);
     
    		g.setColor(Color.black);
     
    		int minimum = 0;
    		int maximum = 3;
    		int randomNum = minimum +(int)(Math.random()*maximum);
     
    		if(rect1Clicked){
    			g.drawString(songArray[randomNum],20,140);
    		}
    		else {
    			g.drawString("To play, try to correctly guess the artist who sings the given song.", 20, 120);
    			g.drawString("Click anywhere to begin.", 20, 140);
    		}
     
    		if(mouseEntered){
    			repaint();
    		}
    		if(score==10){
    			g.drawString("Your Score = " + score, 20, 140);
    		}
    	}
     
    	public void mouseClicked(MouseEvent f){
    		xpos = f.getX();
    		ypos = f.getY();
     
    		if (xpos>rect1xco && xpos < rect1xco+rect1width && ypos >rect1yco &&
    				ypos<rect1yco+rect1height) rect1Clicked = true;
    		else
    			rect1Clicked=false;
    		repaint();
    	}
     
    	@Override
    	public void mouseEntered(MouseEvent e) {	}
     
    	@Override
    	public void mouseExited(MouseEvent e) {		}
     
    	@Override
    	public void mousePressed(MouseEvent e) {	}
     
    	@Override
    	public void mouseReleased(MouseEvent e) {	}
    	public static void main(String[] args){
     
    	}
    }


  2. #2
    Junior Member
    Join Date
    Apr 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Issues with JApplet and Text Fields

    Hi Do u Know U Did A Simple Mistake input.setText(""); so Always It Will Set To Empty I think So Just Try to give Some Values

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,343
    Thanks
    182
    Thanked 831 Times in 774 Posts
    Blog Entries
    5

    Default Re: Issues with JApplet and Text Fields

    My issue right now is I'm having difficulty making it so that the user's input is checked and then the text field is reset.
    I recommend boiling your code down to only the parts which involve your problem, post it as an SSCCE, and clearly define the behavior you want the behavior you have. Right now, at least to me, the problem/question is not clearly defined (define 'input is checked', define 'field is reset')

  4. #4
    Junior Member
    Join Date
    Apr 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Issues with JApplet and Text Fields

    Quote Originally Posted by copeg View Post
    I recommend boiling your code down to only the parts which involve your problem, post it as an SSCCE, and clearly define the behavior you want the behavior you have. Right now, at least to me, the problem/question is not clearly defined (define 'input is checked', define 'field is reset')
    Okay, the main problem is within my actionPerformed() method. Ideally, I'd like to make it so that when the user enters text into the TextField, which I call input, and hits enter, the text entered will be compared to a certain value based upon the random location in the array. So, if the random location in the array is the String "Pet Sounds", the user will need to enter "Beach Boys" in order to get a point. After that, I'd like the TextField to reset while another section of the array is called upon. So here's the code for my actionPerformed method:

    public void actionPerformed(ActionEvent e) {
     
    		input.equals((e.getActionCommand()));
    		String text = input.getText();
     
    		while(songArray[randomNum].equalsIgnoreCase("Where is my mind?")){
    			if (text.equalsIgnoreCase("The Pixies")){
    				score++;
    				input.setText("");
    				 repaint();
    				 actionPerformed(e);
    			}
    			else{
    				input.setText("");
    				repaint();
    				actionPerformed(e);
    			}
    		}
    		while(songArray[randomNum].equalsIgnoreCase("Pet Sounds")){
    			if (text.equalsIgnoreCase("The Beach Boys")){
    				score++;
    				input.setText("");
    				repaint();
    				actionPerformed(e);
    			}
    			else{
    				input.setText("");
    				repaint();
    				actionPerformed(e);
    			}
    		}
    		while(songArray[randomNum].equalsIgnoreCase("Let It Be")){
    			if (text.equalsIgnoreCase("The Beatles")){
    				score++;
    				input.setText("");
    				 repaint();
    				 actionPerformed(e);
    			}
    			else{
    				input.setText("");
    				repaint();
    				actionPerformed(e);
    			}
    		}
    	}

    Thanks for any help you can provide, and let me know if you have any questions!

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,343
    Thanks
    182
    Thanked 831 Times in 774 Posts
    Blog Entries
    5

    Default Re: Issues with JApplet and Text Fields

    A few comments:
    1) That is not an SSCCE, but suffices for the following comments
    2) The first line in which you evaluate the action command does nothing
    3) For each call to actionPerformed, you call the method again. Any reason why?
    4) Why are you calling repaint for every evaluation?
    5) Think about those while loops...are there cases in which their evaluation statements never evaluate to false (and hence be infinite)?

  6. #6
    Junior Member
    Join Date
    Apr 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Issues with JApplet and Text Fields

    First off, thanks for your comments!
    1) Yeah, truthfully I'm not really sure what I was supposed to give you, so I just tried making my problem a little more specific.
    2) Hmm. So would that line be something I should just delete or is there something I'm missing. Do I need something there to make it so that hitting the ENTER key does something?
    3) This was an attempt to reset the program so it would run again after one value was input, but obviously it doesn't work that way. I just didn't take it out like I should have.
    4) Same as number 3.
    5) Now that you mention it, it seems like one of my problems is that the randomNum does not change after the user inputs a String. So if the String in the array is "Pet Sounds", there's nothing that is changing that value. So I guess I need to make it so that when a user enters a String, the String is checked and then everything starts over with a new randomNum.

    If you can't tell I'm having a little trouble working with the applet. It's something somewhat new to me so I've been toying with it a lot and putting in things that probably make no sense. So I really appreciate your help.

    EDIT: I just changed the while loops to if elses. So now, the text box "resets" as in the user can enter a String into the TextField and when he/she hits ENTER, the text goes away and they have the ability to enter another String. So I think I made some progress here.
    Last edited by schiefer740; April 27th, 2012 at 05:07 PM.

  7. #7
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,343
    Thanks
    182
    Thanked 831 Times in 774 Posts
    Blog Entries
    5

    Default Re: Issues with JApplet and Text Fields

    Quote Originally Posted by schiefer740 View Post
    So I think I made some progress here.
    It sounds that way to me...glad to hear you are making progress. My biggest pieces of advice are to take things one step at a time and break the problem down to steps, even writing them on paper if you must to help you think more clearly and logically about a more complex problem; use System.out.println to help you understand the flow of the program (and debug); and read the tutorials at Oracle (for instance How to Write an Action Listener (The Java™ Tutorials > Creating a GUI With JFC/Swing > Writing Event Listeners) ). If you are still struggling, the post the updated code (see the link in my signature for how to make an SSCCE) with another question
    Last edited by copeg; April 27th, 2012 at 05:53 PM.

Similar Threads

  1. [SOLVED] Inserting scroll lists and text fields into tabbed panes
    By Ace Java 9000 in forum AWT / Java Swing
    Replies: 3
    Last Post: January 5th, 2012, 01:12 PM
  2. Problem parsing WHOIS date from text and capturing fields
    By jdev28 in forum Java Theory & Questions
    Replies: 5
    Last Post: July 16th, 2011, 03:27 PM
  3. Get dynamic Text fields values
    By John kee in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 24th, 2011, 06:15 AM
  4. repeat creation of text fields
    By kurt-hardy in forum AWT / Java Swing
    Replies: 1
    Last Post: December 6th, 2010, 11:05 AM
  5. Putting text fields in an array
    By Movies32 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 10th, 2010, 06:46 PM

Tags for this Thread