Results 1 to 3 of 3
Thread: JTextField not visible?
- 05-20-2009, 12:41 AM #1
Member
- Join Date
- Feb 2009
- Posts
- 47
- Rep Power
- 0
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.
Java 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()); } }
Java 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(); } } }
-
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.Last edited by Fubarable; 05-20-2009 at 12:56 AM.
- 05-20-2009, 01:17 AM #3
Member
- Join Date
- Jan 2009
- Posts
- 18
- Rep Power
- 0
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
Similar Threads
-
Text are not visible in GUI
By VinTiger in forum New To JavaReplies: 5Last Post: 05-15-2009, 09:14 AM -
Tab or Table not visible
By madhuvanthi2312 in forum SWT / JFaceReplies: 1Last Post: 04-25-2009, 10:00 AM -
how to access jTextField of one JFrame1 from JFrame2 & Modify JTextField contents
By sumit1mca in forum AWT / SwingReplies: 1Last Post: 01-30-2009, 07:44 PM -
Address bar not visible
By Akashchopra521 in forum New To JavaReplies: 0Last Post: 12-03-2008, 08:13 AM -
constructor ... is not visible
By emceenugget in forum New To JavaReplies: 2Last Post: 09-27-2008, 06:12 AM
Bookmarks