-
JTextField not visible?
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.
Code:
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());
}
}
Code:
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.
-
I assume that you're placing this JTextField into a JFrame or something similar right? Where's the code for this? If you're just playing with a JTextField and not placing it anywhere that has the ability to show it, you will see nothing.
Bottom line here: I think that you will benefit greatly by going through the Sun Swing tutorial and learn how to create functioning GUI programs. http://java.sun.com/docs/books/tutor...nts/index.html
Best of luck.
-
Hey,
Fuba's right there but looking at your code, in class player it seems that bluej doesnt associate your closing bracket at the end of your if statment (line 31) but instead associates it with the bracket on line 35 after your else statment...
Maybe there's a reason behind this i'm not familiar with? Could anyone clarify?
Very perculiar indeed and got me curious heh.
But yes, get onto java.sun and check out JFrames etc.
Another alternative would be JOptionPanes to get the user input :)
-Jvr