| Inheritance and GUI's | |||||||||||
| Intro to Inheritance and GUI's | Examples of Inheritance GUI's | ||||||||||
| GUI Example | |||||||||||
| Home | |||||||||||
| To create JFrames that do what we want, we create a new class that inherits from the JFrame.
Take a look at the inheritance hierarchy of the javax.swing.JFrame class in the standard runtime API documentation. This hierarchy indicates that JFrame is a Frame and Frame is a Window and Window is a Container and Container is a Component and finally Component is an Object (just like all the other classes are in Java since the Object class is the root class from which they all descend). A more general way to look at this is to say a child class can be used any place any of its ancestor classes can be used. This means that JFrame is a Frame and a Window and a Container and a Component and an Object. If we take a look at the inheritance hierarchy of the javax.swing.JFrame class, it says that a JFrame is a Frame, a Frame is a Window, a Window is a Container, a Container is a Component, and a Component is an Object. By applying the definition of inheritance given at the top, we can say that: A JFrame is a frame, window, container, component, and an object all in one. We can compare inheritance to this: We make our own Jframe, called MyJFrame public class MyJFrame extends JFrame { MyJFrame (String title){� title, size� addWindowListener(new WindowCloser()); setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); } class WindowCloser extends WindowAdapter { public void windowClosing (WindowEvenet event) { int choice = JOptionPane.showConfirmDialog (MyJFrame.this,�Do you really want to quit:, �Confirm quit�,JOptionPane.YES_NO_OPTION); if (choice == JOptionPane.YES_NO_OPTION) System.exit(0); } } } Result |
|||||||||||