Hi, I have a simple program with a an image, jtextbox, and a jlabel... For some reason the image and label show fine, but despite doing what some people said would work (As you might see... ) the textbox only works some of the time (And when i resize the frame.) Also, I would like the cursor to automatically be in the textbox (with requestFocus()) but that isnt working either...
Here is the code. I highlighted the first working section and the nonworking section
import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.awt.image.*; import java.net.*; import java.util.*; import java.io.*; public class OurPanel extends JPanel { public static BufferedImage myImage; public static Graphics g; public static int amount = 1; public static boolean assist = false; public static double time = 2; public static JLabel label; public static boolean done = false; public static boolean ok = false; public OurPanel() throws Exception { myImage = new BufferedImage(500, 500, BufferedImage.TYPE_INT_RGB); g = myImage.getGraphics(); label = new JLabel(""); label.setFont(new Font("Serif",0,50)); add(label); } public void start() throws Exception { String[] array= new String[576]; Scanner in = new Scanner(new File("a.txt")); ArrayList<Pair> joe = new ArrayList<Pair>(); int r = 0; while(in.hasNextLine()) { if(r>575) break; String current = in.nextLine(); current=current.trim(); String[] joesephus = current.split("!"); r++; int a = 0; a = 0; array[r-1]=current; if(current.equals("null")) continue; if(current.length()>3) joe.add(new Pair(joesephus[0].substring(0,2),joesephus[0].substring(3,joesephus[0].length()), joesephus.length>1?joesephus[1]:null)); } while(true) { ArrayList<Pair> out = new ArrayList<Pair>(); for(int i = 0;i<amount;i++) { out.add(joe.get((int)(Math.random()*joe.size()))); System.out.println(out.get(out.size()-1)); if(out.get(out.size()-1).pair==null||out.get(out.size()-1).pair.equals("")||!(new File("Images//" + out.get(out.size()-1).pair + ".jpg").exists())) { i--; out.remove(out.size()-1); } } String answer =""; for(int i = 0;i<amount;i++) { done=false; boolean l = false; [B] if(out.get(i).url!=null&&out.get(i).url.trim()!="") { JLabel s; File stevehensing = new File("Images//" + out.get(i).pair + ".jpg"); if(stevehensing.exists()) { ok = false; java.awt.Image image = javax.imageio.ImageIO.read(stevehensing); s = new JLabel(new ImageIcon(image)); add(s); label.setText(out.get(i).word + (assist?" (" + out.get(i).pair.trim() + ")" :"")); }[/B] } setRequestFocusEnabled(true); answer=answer.toLowerCase(); Scanner input = new Scanner(System.in); removeAll(); [B] final JTextField box = new JTextField("", 10); box.validate(); box.setVisible(true); add(box); [/B] [B] Thread t = null; t = new Thread( new Runnable() { private int a; private boolean yes = false; public void run() { while (true) { box.setVisible(!box.isVisible()||yes); try { if(a>1) yes=true; Thread.sleep(100); a++; } catch (InterruptedException e) { /* Handle exception */ } } } }); box.setVisible(true); t.start(); box.addKeyListener( new KeyAdapter() { public void keyPressed(KeyEvent e) { System.out.println("TESTES" + KeyEvent.getKeyText(e.getKeyChar())); if (e.getKeyChar() == KeyEvent.VK_ENTER) { done=true; } } }); label.setText(""); add(label); repaint(); setVisible(true); box.requestFocus(); while(!done) { } [/B] if(box.getText().toLowerCase().equals(answer)) label.setText("Correct!"); else label.setText("Incorrect, it is " + answer.toUpperCase()); remove(box); Thread.sleep(2000); } } private class Listener implements ActionListener { public void actionPerformed(ActionEvent e) { done=true; } } private class Listener2 implements ActionListener { public void actionPerformed(ActionEvent e) { ok = true; } } private static class Pair { public String pair = ""; public String word = ""; public String url = ""; public Pair(String pair2, String word2, String url2) { pair=pair2; word=word2; url=url2; } public String toString() { return pair + " " + word; } } public static void main(String[] args) throws Exception { final JFrame frame = new JFrame("Flash Cards"); frame.setSize(1200,800); frame.setLocation(100, 50); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); OurPanel joe = new OurPanel(); frame.setContentPane(joe); frame.setVisible(true); Thread t = new Thread(new Runnable(){public void run(){ frame.setSize(200,801); try{ Thread.sleep(300); } catch(Exception j) { } frame.setSize(1200,800); } } ); t.start(); joe.start(); } }
Thanks in advance for the help!
I will try to answer any questions taht you have. I realize i name my variables in a confusing way