Results 1 to 2 of 2
- 06-07-2009, 09:23 PM #1
[error] No enclosing instance of type RobotTest is accessible
I'm having a bit of trouble with this code, any ideas?
:confused:
Error (without ButtonFrame class):Java Code:package homenetwork.bkr.training; import java.awt.*; import java.awt.event.*; import java.awt.image.*; import javax.swing.*; public class RobotTest { public static void main (String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { //make frame with a button panel [B]ButtonFrame frame = new ButtonFrame();[/B] //Error: ButtonFrame cannot be resolved to a type frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); //attach a robot to the screen device GraphicsEnvironment environment = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsDevice screen = environment.getDefaultScreenDevice(); try { Robot robot = new Robot(screen); runTest(robot); } catch (AWTException e) { e.printStackTrace(); } } } ); } /** Runs a sample test procedure. * @param robot: the robot attached to the screen device. */ public static void runTest(Robot robot) { //simulate a space bar press robot.keyPress(' '); robot.keyRelease(' '); //TODO try using the key codes. what key codes should I use? //simulate a mouse click over the rightmost button robot.delay(2000); robot.mouseMove(200, 50); robot.mousePress(InputEvent.BUTTON1_MASK); robot.mouseRelease(InputEvent.BUTTON1_MASK); //capture the screen and show the resulting image robot.delay(2000); BufferedImage image = robot.createScreenCapture(new Rectangle(0,0,400,300)); [B]ImageFrame frame = new ImageFrame(image);[/B] //Error: No enclosing instance of type RobotTest is accessible. Must qualify the allocation with an enclosing instance of type RobotTest (e.g. x.new A() where x is an instance of RobotTest). frame.setVisible(true); } /** A frame to display a captured image */ @SuppressWarnings("serial") class ImageFrame extends JFrame { /** * @param image: the image to display */ public ImageFrame (Image image) { setTitle("Capture"); setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); JLabel label = new JLabel(new ImageIcon(image)); add(label); } public static final int DEFAULT_WIDTH = 450; public static final int DEFAULT_HEIGHT = 350; } } [B]ButtonFrame.java[/B] package homenetwork.bkr.training; import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; @SuppressWarnings("serial") public class ButtonFrame extends JFrame { public ButtonFrame() { setTitle("ButtonTest"); setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); //create buttons JButton yellowButton = new JButton("Yellow"); JButton blueButton = new JButton("Blue"); JButton redButton = new JButton("Red"); JPanel buttonPanel = new JPanel(); //creates panel (1) if (debug) System.out.println("DEBUG create: buttonPanel=" + buttonPanel.toString()); //add buttons to panel buttonPanel.add(yellowButton); buttonPanel.add(blueButton); buttonPanel.add(redButton); //add panel to frame add(buttonPanel); //create button Actions ColorAction yellowAction = new ColorAction(Color.yellow); ColorAction blueAction = new ColorAction(Color.blue); ColorAction redAction = new ColorAction(Color.red); //associate actions with buttons yellowButton.addActionListener(yellowAction); blueButton.addActionListener(blueAction); redButton.addActionListener(redAction); } public class ColorAction implements ActionListener { public ColorAction (Color c) { backgroundColor = c; } public void actionPerformed(ActionEvent event) { if (buttonPanel != null) buttonPanel.setBackground(backgroundColor); else if ((debug) && (buttonPanel != null)) System.out.println("DEBUG :" + buttonPanel.toString()); else if ((debug) && (buttonPanel == null)) System.out.println("DEBUG : buttonPanel==null"); //FIX here I should change the color I think //Color c = (Color) getValue("color"); if (debug) System.out.println("DEBUG: backgroundColor=" + this.backgroundColor.toString()); } private Color backgroundColor; } private static final int DEFAULT_WIDTH=300; private static final int DEFAULT_HEIGHT=70; protected JPanel buttonPanel; private boolean debug = true; }
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
ButtonFrame cannot be resolved to a type
ButtonFrame cannot be resolved to a type
at homenetwork.bkr.training.RobotTest.main(RobotTest. java:16)
Error
DEBUG create: buttonPanel=javax.swing.JPanel[,0,0,0x0,invalid,layout=java.awt.FlowLayout,alignm entX=0.0,alignmentY=0.0,border=,flags=9,maximumSiz e=,minimumSize=,preferredSize=]
Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problem:
No enclosing instance of type RobotTest is accessible. Must qualify the allocation with an enclosing instance of type RobotTest (e.g. x.new A() where x is an instance of RobotTest).
at homenetwork.bkr.training.RobotTest.runTest(RobotTe st.java:57)
at homenetwork.bkr.training.RobotTest$1.run(RobotTest .java:27)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
- 06-08-2009, 07:27 AM #2
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
Try making ImageFrame static. You cannot access a non-static inner class from within a static method. Also, if you are posting multiple classes, please place them within separate code tags. It makes it much easier to find different classes.
A sample post:
The FirstClass class:
The SecondClass class:Java Code:My first class
Java Code:My Second Class
If the above doesn't make sense to you, ignore it, but remember it - might be useful!
And if you just randomly taught yourself to program, well... you're just like me!
Similar Threads
-
Error: unexpected type
By silvia in forum New To JavaReplies: 3Last Post: 02-05-2010, 09:54 PM -
Error Message:Editor does not contain main type
By Deepa in forum New To JavaReplies: 9Last Post: 07-03-2009, 03:58 AM -
Error FIxed type attribute not equal
By phoenix_yadu in forum Advanced JavaReplies: 2Last Post: 04-23-2009, 04:36 PM -
Error Message:Editor does not contain a main type
By Deepa in forum New To JavaReplies: 0Last Post: 11-27-2008, 12:25 PM -
Making variables in a class accessible to all once changed
By int80 in forum New To JavaReplies: 13Last Post: 08-23-2008, 09:09 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks