0

First of all, I would like you to know that I'm new in Java world; so please forgive me if my question is basic.

I'm working with a team, and we are trying to create a browser using Java.
To begin with, we are watching some tutorials, and all of them begin with a class that extends JFrame. What does this JFrame do?

Veligkekas G.
  • 43
  • 1
  • 10
  • 3
    http://docs.oracle.com/javase/7/docs/api/javax/swing/JFrame.html – Manu Nov 14 '15 at 22:10
  • 1
    To really dumb down this answer, a JFrame is a window. – Cup of Java Nov 14 '15 at 22:11
  • 1
    You should not extend a JFrame. A JFrame is just used to hold other Swing components. Read the section from the Swing tutorial on [How to Make Frames](http://docs.oracle.com/javase/tutorial/uiswing/components/frame.html) for more information and working examples. – camickr Nov 14 '15 at 22:12
  • @camickr why not extend JFrame? – Yassin Hajaj Nov 14 '15 at 22:13
  • @YassinHajaj Generally speaking, you're not adding any new functionality to the class which can't be done by simple creating a new instance of it. Also, it locks you into a single use case, making your code less flexible and reducing the reusability of the code – MadProgrammer Nov 14 '15 at 23:20
  • Generally speaking, a JFrame is known as a top level container, it's the "window" which mangoes the window borders and controls and allows the rest of your application to be presented to the user – MadProgrammer Nov 14 '15 at 23:21

1 Answers1

2

This could easily be answered in a 10 second Google search... But to answer your question anyway, a JFrame is an extension of java.awt.Frame, which displays a graphical window to the user, in which you can house components and graphics on.

These components range from JButton's JLabel's all the way to Menu bars, etc.

Also, extending a JFrame is never a good idea. It works fine, but for the best code readability, and usage, do not do it. There are plenty of reasons why, and they are explained thoroughly here:
Why shouldn't you extend JFrame and other components?

Extract:

Generally speaking, extending the component tends to be done strictly to use the component. This severely limits your options in unnecessary ways in terms of design, so that your classes can't extend different classes, you can't hide the JFrame's methods causing it to be more difficult to maintain and easier to trigger unexpected bugs when using the class.

More can be found below in the links provided:

JavaDoc for JFrame
How to use a JFrame

Community
  • 1
  • 1