See the compiler error, all OK. Remove the notation and continue till the time I change the method name, signature etc. JFrame is involved in some memory leaks on some platforms, so if you extend it and include large objects in your sub-class then these objects will never be recovered by garbage collection.
See stackoverflow. Supplement Having mentioned you do search SO, here is a tip. Check out the posts of the top 15 providers of answers in the Swing top users. Some don't often or ever provide self contained examples like a few of us commonly do and don't look to those examples necessarily for OO design as opposed to just technique , but whatever code they provide, or advice they give, should be highly considered.
Community Bot 1 1 1 silver badge. Andrew Thompson Andrew Thompson k 39 39 gold badges silver badges bronze badges. The main gripe I have with extending JFrame is, you're not actually adding any new features or functionality to it, which could be effectively re-used beyond using setVisible The other issue I have with extending JFrame is people then promptly override paint , which is really, really bad. MadProgrammer MadProgrammer k 21 21 gold badges silver badges bronze badges.
The first approach is better. Reimeus Reimeus k 13 13 gold badges silver badges bronze badges. Go for the first approach. It does not matter. Can you give a short example of one such reason? I did. Read it again. Of course the choice has ramifications, but none of them make one preferable over the other. If there's a fork in the road, and both roads end up in the same place, it may not matter which one you take, but they will be different.
The devil is in the details. It has objects, and thats it other than the simple types. Javascript uses prototypal inheritance, which means it has a way of efficiently defining objects and the methods on those objects this is the topic for another question; you can search SO as there are already answers. You can use composition, however, to give it the functionality. Indeed, this is one of the reasons composition is preferred by some over inheritance; there are no limits on combining functionality but this isn't the only reason.
So there are your simple examples for both inheritance and composition. However, this is not the end of the story. I said before that Javascript does not support multiple inheritance, and in a sense it doesn't, because you can't base the prototype of an object off the prototypes of multiple objects; i. This has implications for the instanceof operator. However, this doesn't really matter, because just because you can't redefine the constructor of an object twice, you can still add any methods you want to the prototype of an object.
So just because you can't do the above example, you can still add anything you want to C. Many frameworks support this and make it easy. I do a lot of Sproutcore work; with that framework you can do. Here I defined functionality in object literals A and B , and then added the functionality of both to C , so every instance of C has methods 1, 2, and 3. In this particular case, the extend method provided by the framework does all the heavy lifting of setting up the prototypes of the objects.
And it is almost always safer to make public method calls on an object rather than muck with an object's internals. I wish I could say the same and am impressed. I find that the larger and more complex the project, the greater the chance that this sort of thing happens to me. And so while the risk of seeing unwanted side effects from unnecessarily extending a class is low for small demo programs, and some may argue, why worry about it for larger projects, but for me I feel that it's a good idea to keep using good habits with small programs so I will automatically do them with big projects.
Hang around this forum and you'll see many questions deriving from just this problem. Hmm ok, I think I'll be going with the non-extending version then because there doesn't seem to be a special reason for extending JFrame.
Also like this I save the possibility to extend the class when it's really needed. Thanks for discussing! I enter all input into a textfile beforehand and then I read the file with java. Eventually I find out it would be nice to transform the program into a GUI to make it more user friendly or generally speaking more "modern". At this point unfortunately I always get stuck because I don't know how to start.
There's so many ways to put Contents into Containers i. My next question would have been where to put the elements of the container: create an own class for each button I want to add or create a class which contains a panel and then add the buttons into the panel from inside that class and so on Usually I end up creating only one class and writing tons of static methods which I simply call from main.
I can tell myself that this can't be good design. Since I rarely have to do this for a JFrame, I'll rarely want to extend it. If your class extends JFrame this will be hard to do. You need to tell Swing that the components are translucent, so it does also paint the background when repainting the changed components. For this, your components have to return false from the isOpaque method this can be achieved by setOpaque false when no subclass overwrites isOpaque to do something else.
With this, it should work without any manual repainting of everything, as the other answers proposed. I already did this once.
0コメント