I want my program to display a text field. The text will be a random spanish word or phrase. Then I enter in the correct english translation. I want it to wait for 5 seconds after I enter text. Then set the text to a new word, wait for my translation, wait 5 seconds, etc etc until I close the program.
import java.util.ArrayList; import java.util.Random; public class Library { public ArrayList<String> spanish = new ArrayList<String>(); //make a spanish list public ArrayList<String> english = new ArrayList<String>(); //make an english list static Random random = new Random(); //make a random object private int index; public void setIndex(int index) { this.index = index; } public int getIndex() { return index; } //method to add all Strings to the lists public void makeLists() { spanish.add("Como te llamas?"); english.add("What is your name?"); spanish.add("Como se llama?"); english.add("What is his/her name?"); spanish.add("Por favor"); english.add("Please"); } //method to grab a word or phrase from the spanish list public String getOne() { int x = random.nextInt(spanish.size()); setIndex(x); return spanish.get(getIndex()); } }import javax.swing.*; import java.io.*; import java.util.Random; public class Player extends Thread{ static JTextField text = new JTextField(); //make the text field static Library library = new Library(); //make a library object static Random random = new Random(); //make a random object public static void main(String args[]) { Player player = new Player(); //make a player library.makeLists(); //make the lists player.start(); //start } public void run() { try { text.setVisible(true); //make the field visible boolean loop = true; while(loop) { //loop forever text.setText(library.getOne() + "\n"); //set the text to a random spanish phrase InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); if(((br.readLine().equalsIgnoreCase(library.english.get(library.getIndex()))))) { text.setText("Right!"); //if i enter in the correct english translation, i'm right } else text.setText("Wrong. The answer is " + library.english.get(library.getIndex())); //if i'm wrong, display the right answer Thread.sleep(5000); //wait for 5 seconds } } catch(Exception ex) { ex.printStackTrace(); } } }
Whenever I run the program...nothing happens. Well I'm assuming SOMETHING happens, but no text field comes up on my screen. My screen just remains the same and eventually I click the red terminate button (in Eclipse) when I give up on waiting. Can anyone tell me what's wrong or point me in some direction please?? Thanks.