Okay well I had to where every x amount of time it would compare, and a new word would put up. But ideally, I'd like to set the amount of time between phrase changes and compare the new words after ten seconds. I tried adding a new listener, but it never compares the to. It will just sit there until it's time to change the phrases and changes them.
package languagestudy;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
/**
*Sterling McLeod
*/
public class LanguageStudy {
public JFrame frame;
public JPanel panel;
public JTextField toTranslate;
public JTextField translated;
public ArrayList<String> phrases;
public Scanner scan;
public Random rng = new Random();
public Timer timer;
public int a;
public LanguageStudy(String language) throws IOException{
if(language.equalsIgnoreCase("spanish")) { //check what language and get phrases for appropriate language
String file = "C:/Users/Sterling/Documents/NetBeansProjects/LanguageStudy/Spanish.txt";
getPhrases(file);
}
}
public void createAndShowGUI() { //make two text fields
frame = new JFrame("LanguageStudy!");
panel = new JPanel();
toTranslate = new JTextField(40);
translated = new JTextField(40);
panel.add(toTranslate);
panel.add(translated);
panel.setVisible(true);
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
public void getPhrases(String theFile) throws IOException{
phrases = new ArrayList<String>();
File file = new File(theFile);
scan = new Scanner(file); //make a scanner for the file
scan.useDelimiter("\r?\n|\r");
int count = 0;
while(scan.hasNext()) { //get amount of phrases
phrases.add(scan.next());
count++;
}
}
public void go() { //starts program
System.out.println("How many seconds would you like the timer to be?");
Scanner scan2 = new Scanner(System.in);
int delay = scan2.nextInt() * 1000;
createAndShowGUI();
Timer changeTimer = new Timer(delay, new ChangeListener()); //set timer
//set the GUI before starting the timer
a = rng.nextInt(phrases.size()); //get a random number
toTranslate.setText(phrases.get(a)); //set text to the random phrase
JOptionPane.showMessageDialog(null, "A new phrase awaits you."); //notify user
translated.requestFocusInWindow();
//done setting up GUI
changeTimer.start();
}
class ChangeListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
translated.setText("");
a = rng.nextInt(phrases.size()); //get a random number
toTranslate.setText(phrases.get(a)); //set text to the random phrase
JOptionPane.showMessageDialog(null, "A new phrase awaits you."); //notify user
translated.requestFocusInWindow();
Timer compareTimer = new Timer(10000, new CompareListener());
compareTimer.start();
} //end actionPerformed
} //end changeListener
class CompareListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if(a % 2 == 0) { //if toTranslate was english
if(translated.getText().equalsIgnoreCase(phrases.get(a+1))) //if translated is equal to the next phrase (aka if correct foreign)
JOptionPane.showMessageDialog(null, "Correct!");
else {
String right = "Sorry the answer is \"" + phrases.get(a+1) + "\"";
JOptionPane.showMessageDialog(null, right);
} //end inner if-else
}
else if(a % 2 == 1) { //if toTranslate was foreign
if(translated.getText().equalsIgnoreCase(phrases.get(a-1))) { //if translated is equal to previous phrase (aka if correct english)
JOptionPane.showMessageDialog(null, "Correct!");
}
else {
String right = "Sorry the answer is \"" + phrases.get(a-1) + "\"";
JOptionPane.showMessageDialog(null, right);
} //end inner if-else
} //end if-else
} //end actionPerformed
} //end class compareListener
public static void main(String[] args) throws IOException{
LanguageStudy study = new LanguageStudy("spanish");
study.go();
}
}
What am I missing here?