What are the pros and cons of extending a JFrame rather than create a new JFrame?
For example:
public class Test extends JFrame {
setVisible(true);
}
or
public class Test {
JFrame test = new JFrame():
test.setVisible(true);
}
The class myFrame extends the class JFrame . The paint() method of a JFrame object is called by the Java system (not by you) to finish the display. Most of the graphic is done; paint() just finishes it. If you override the paint() method, you can display your own components in the frame.
Avoid extending JFrame as it ties your GUI to being, well a JFrame. If instead you concentrate on creating JPanels instead, then you have the freedom to use these JPanels anywhere needed -- in a JFrame, or JDialog, or JApplet, or inside of another JPanel, or swapped with other JPanels via a CardLayout.
You can't do it. Jpanel and jframe are both classes and java doesn't support multiple inheritance of classes. Only one class and many interfaces. Don't extend frame or other top level containers.
You can't put one JFrame inside another. Change your design so that the Game window content is on a JPanel and the Menu is on another JPanel .
You should not extend a class, unless you want to actually extend its functionality, in the example you've shown, you should use option 2, as option 1 is an abuse of the extend
ing feature.
In other words - as long as the answer to the question is Test a JFrame
? is NO, it should not extend JFrame
.
pros of not extending JFrame (or any Swing component for that matter):
int getX()
and int getY()
methods. Try it and you'll see some not so funny abnormal behaviors.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With