• Post Reply Bookmark Topic Watch Topic
  • New Topic

please help java swing practice

 
Fritz Guerilus
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I'm rusty with Java, so I'm practicing to improve my skills.
I found this code in the book JAVA FUNDAMENTALS Volume 1 Chapter 7
The code is to create a simple Jframe, but it’s a little advance from what I'm used to seeing.

It's missing an instance of the class TestGui to compile and run, but I'm not sure how to put in what's missing.

The code invokes the static method EventQueue.invokeLater which passes an anonymous inner class into the method argument.
The anonymous inner class is a thread

When I compile the code I get the following error:


Exception in thread "main" java.lang.Error: Unresolved compilation problem:
No enclosing instance of type TestGui is accessible. Must qualify the allocation with an enclosing instance of type TestGui (e.g. x.new A() where x is an instance of TestGui).

at TestGui.main(TestGui.java:18)


I understand what the error is saying but I don't know how to fix it.
Can someone help explain what's missing and how I should add it.
Thank You
-Fritz
BTW: The comments are mine, and not from the book

 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first question coming to mind is : does SimpleFrame needs to be an inner class of TestGui ? If not, take it out of it. If for some reason you need it to be an inner class, then you need a TestGui instance as well. Something like new TestGui().new SimpleFrame(); will do.
 
Tom Reilly
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alternatively you can change SimpleFrame to be a static class:This makes it a top level class. But when referring to it outside the class, you must specify the containing class. It's up to you to determine the better design for your situation.
 
Fritz Guerilus
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I looked at your replies and took a second look at the code example in the book and realized I made a typing mistake.

I accidently forgot to put in the bracket to close the public class.

Thus making the SimpleFrame class into a inner class when it was only supposed to be a member class and complicated things.

The code runs fine.

But thanks for your suggestions.
 
Campbell Ritchie
Marshal
Posts: 52587
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We usually discuss GUIs on a different forum. Moving thread.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!