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){ } }