• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

How to extend JFrame class?  RSS feed

 
Greenhorn
Posts: 13
C++ Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to extend my JFrame class but I keep getting an error saying my class isn't abstract.
This is my code so far:
 
Marshal
Posts: 60130
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't extend GUI display classes if you can help it. There are a few instances where you do need to override methods, e.g. JPanel#paintComponent(), but most classes don't need subclassing. You are probably trying to put an abstract method into your subclass if you are getting that compiler error; maybe you wrote something like public void foo(...); { ... } with more semicolons than you are supposed to have.
Don't write this sort of thing:-For the GBC class, look here.
Write this sort of thing:-
 
Campbell Ritchie
Marshal
Posts: 60130
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried your code and got a completely different error message, which is to do with which part of the code is inside your inner class and which part of the code is outside the inner class.
 
Rancher
Posts: 2832
96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are more serious errors.

First one is this. You have an inner class

but if you look at that class, you do not implement an ActionListener at all! You do have a method 'actionPerformed', but that is unrelated to this class.

Another problem is that you have a constructor of your class 'Swing' that is part of your outerclass 'JVactionalRental'.

And I notice an error that occurs quite frequently:
You have a class A that extends B, but in your constructor you define yest another B. For instance:

And finally for now:
if you add a component (a panel or a button) to a frame, then it is added to the center panel of the contentPane. If you add another component to your frame, then it replaces whatever the center of that contentPane contained. So take care of that!
And you need to set a FlowLayout once, not every time. To make the process a little more clear, use:
 
Blaine Jackson
Greenhorn
Posts: 13
C++ Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I managed to work through my problem for the most part, by starting from scratch, and came up with this:

But I'm still getting an error that says "is not abstract and does not override abstract method" and I got a new one that says "non-static variable this cannot be referenced from a static context". I tried making the first class a static class and it fixed that error but created more in return. Can you please help me with this?
 
Piet Souris
Rancher
Posts: 2832
96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Blaine,

this looks much better!

Still some errors:

1) the method to override is called 'actionPerformed', with a capital P. If you are using an IDE, it will warn you that you are not overriding anything.

2) There is a '}' at the end of your code, that has no counterpart. Your IDE (if you use any) will indicate that for you.

3) And lastly:
line 92:

should be

With these improvements, your code does rn, but I doubt if you will be very pleased with the way your frame looks! But that for some other time    
 
Campbell Ritchie
Marshal
Posts: 60130
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:There are more serious errors. . . . . You have an inner class . . . You do have a method 'actionPerformed', but that is unrelated to this class. . . . a constructor of your class 'Swing' that is part of your outerclass . . .

Maybe all those errors are caused by failure to indent the code consistently, so you can't readily see which class you are in.
When I tried the code I didn't get any error messages about the actionPerformed method. But having that method in the wrong place would cause an error message about

class XYZ is not abstract and doesn't override method pqr.

 
Campbell Ritchie
Marshal
Posts: 60130
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:. . . the method to override is called 'actionPerformed', with a capital P. . . .

But OP is using the @Override annotation, which should highlight that error. In view of the other errors you mentioned, I wonder whether we are seeing exactly the same code; even the tiniest differences if the code is not copied with ctrl‑C—ctrl‑V can cause us to get completely different compiler errors.
 
Piet Souris
Rancher
Posts: 2832
96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I only copied OP's second version into my IDE. I got several errors, the most important one being that you cannot access an instance variable from a static method, or words of similar meaning. Thar error disappeared when I removed an excessive '}'  near the end. I guess that after that the @Override error became clear. But I don' t think that OP is using an IDE.

But I agree: it was a little confusing. But after repairing these small mistakes, the code ran nicely.

I hope OP will let us know if he/she got it running as well.
 
Campbell Ritchie
Marshal
Posts: 60130
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:. . . . I got several errors, the most important one being that you cannot access an instance variable from a static method . . .

I got suspicious because I was getting different error messages.
 
Piet Souris
Rancher
Posts: 2832
96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I redid the whole process, copied OP's second code again inyo the IDE, and these errors I got:

1) JVacationFrame is not abstract and does not override abstract method 'actionPerformed' et cetera
2) at the @Override: method does not override or implement a method from a superclass
3) JVacationFrame v = new JVacationFrame;    should be 'JVacationFrame v = new JVacationFrame();
3) last line: the '}' is superfluous

No sign anymore of calling an instance member from a static context, I may have dreamt that part...

So that took just a couple of seconds to repeair, after which the code ran fine.
 
Blaine Jackson
Greenhorn
Posts: 13
C++ Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone, I managed to get the code to work, with this code:

But now I'm trying to get the "price" Jbutton to display whats in the finaltotal textfield when pressed. I tried parsing but it still gave me the error "int cannot be converted to String".
 
Blaine Jackson
Greenhorn
Posts: 13
C++ Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nevermind, I got it to work without the use of a JButton.
Here is the code I used:

Thanks for the help everyone!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!