Java Problems

hockeygoalie5

In Runtime
Messages
299
Location
United States
Okay, I've programmed a Java application in Eclipse. It was working fine but now the JFrame won't show. After investigating, I found two errors that Eclipse wasn't giving me by compiling it manually.

Code:
AutoTyper.java:16: cannot find symbol
symbol  : class SmartRobot
location: class org.smartrobot.AutoTyper
                SmartRobot robot = new SmartRobot();
                ^

AutoTyper.java:16: cannot find symbol
symbol  : class SmartRobot
location: class org.smartrobot.AutoTyper
                SmartRobot robot = new SmartRobot();
                                       ^
SmartRobot is a class in the same directory and package as AutoTyper.
 
Yes. What I gather is that for some reason, just on that line, it cannot find 's'?
Nah, it definitely means SmartRobot as a whole (which is what it means by symbol) it just points to the first letter of it. Compiler errors aren't always the easiest things to follow but you get used to them after a while.

I'd really need to see the project to help further - if the SmartRobot class has no errors, is being compiled ok and is located in the same package and folder as the referring class (or the appropriate import statement is used) I don't see any reason why it'll come up with that error.
 
Code:
AutoTyper.java:16: cannot find symbol
symbol  : class SmartRobot
location: class org.smartrobot.AutoTyper
                SmartRobot robot = new SmartRobot();
                ^

AutoTyper.java:16: cannot find symbol
symbol  : class SmartRobot
location: class org.smartrobot.AutoTyper
                SmartRobot robot = new SmartRobot();
                                       ^
SmartRobot is a class in the same directory and package as AutoTyper.
Did you import the package?
 
Okay, I removed that line and it did compile perfect and fine, but the frame still isn't showing! Yes, I set visible to true.

AutoTyper
Code:
package org.smartrobot;

import java.awt.AWTException;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;

public class AutoTyper {
    protected static boolean On = false;
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setSize(250, 100);
        frame.setLayout(new FlowLayout());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JTextField field = new JTextField();
        JButton start = new JButton("Start");
        JButton stop = new JButton ("Stop");
        field.setColumns(20);
        start.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                On = true;
            }
        });
        stop.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                On = false;
            }
        });
        for(int s = 0; s < 1;) {
            if(On) {
                for (int i = 0; i < 35000;) {
                    i++;
                    if(i == 35000) {
                        i = 0;
                    }
                }
            }
        }
            frame.add(field);
            frame.add(start);
            frame.add(stop);
            frame.setVisible(true);
    }
}
You may notice it doesn't do what its name suggests, I'm not done yet.

SmartRobot
Code:
package org.smartrobot;

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;


public class SmartRobot extends Robot {

 public SmartRobot() throws AWTException {
  super();
 }

 public void keyType(int keyCode) {
  keyPress(keyCode);
  delay(50);
  keyRelease(keyCode);
 }
 
 public void keyType(int keyCode, int keyCodeModifier) {
  keyPress(keyCodeModifier);
  keyPress(keyCode);
  delay(50);
  keyRelease(keyCode);
  keyRelease(keyCodeModifier);
 }

 
 public void type(String text) {
  String textUpper = text.toUpperCase();

  for (int i=0; i<text.length(); ++i) {
   typeChar(textUpper.charAt(i));
  }  
 }
 
 private void typeChar(char c) {
  boolean shift = true;
  int keyCode;
  
  switch (c) {
  case '~':
   keyCode = (int)'`';
   break;
  case '!':
   keyCode = (int)'1';
   break;
  case '@':
   keyCode = (int)'2';
   break;
  case '#':
   keyCode = (int)'3';
   break;
  case '$':
   keyCode = (int)'4';
   break;
  case '%':
   keyCode = (int)'5';
   break;
  case '^':
   keyCode = (int)'6';
   break;
  case '&':
   keyCode = (int)'7';
   break;
  case '*':
   keyCode = (int)'8';
   break;
  case '(':
   keyCode = (int)'9';
   break;
  case ')':
   keyCode = (int)'0';
   break;
  case ':':
   keyCode = (int)';';
   break;
  case '_':
   keyCode = (int)'-';
   break;
  case '+':
   keyCode = (int)'=';
   break;
  case '|':
   keyCode = (int)'\\';
   break;
//  case '"':

//   keyCode = (int)'\'';

//   break;

  case '?':
   keyCode = (int)'/';
   break;
  case '{':
   keyCode = (int)'[';
   break;
  case '}':
   keyCode = (int)']';
   break;
  case '<':
   keyCode = (int)',';
   break;
  case '>':
   keyCode = (int)'.';
   break;
  default:
   keyCode = (int)c;
   shift = false;
  }
  if (shift)
   keyType(keyCode, KeyEvent.VK_SHIFT);
  else
   keyType(keyCode);
 }
 
 @SuppressWarnings("unused")
private int charToKeyCode(char c) {
  switch (c) {
  case ':':
   return ';';
  }
  return (int)c;
 }
}

It was working fine before, with the same code.
 
It was working fine before, with the same code.
I don't see how it was!

The problem is here:
Code:
for(int s = 0; s < 1;) {
    if(On) {
        for (int i = 0; i < 35000;) {
            i++;
            if(i == 35000) {
                i = 0;
            }
        }
    }
}
frame.add(field);
frame.add(start);
frame.add(stop);
frame.setVisible(true);

What you've got going there is an infinite loop (though you've done it in a bit of an odd way, you don't need either statement there - for(;; ) will do the same thing. while(true) is a better way of doing it though, far better to read.)

Since you never break out of that loop, all the statements below it (including the setVisible() method call) never get executed, thus the frame is never shown.
 
Code:
for(int s = 0; s < 1;) {
    if(On) {
        for (int i = 0; i < 35000;) {
            i++;
            if(i == 35000) {
                i = 0;
            }
        }
    }
}
What's the above code doing at present, or rather what do you want it to do?
It's just doing some pointless counting at present.

Generally speaking, infinite loops are a bit of a code smell - if you've got one something's probably not right!
 
Back
Top Bottom