Wednesday 2 March 2016

Java Components

We have used the terms component and container somewhat loosely to describe graphical elements of Java applications, but these terms are used in the names of actual classes in the java.awt package. Component is a base class from which all of Java’s GUI components are derived. It contains variables that represent the location, shape, general appearance, and status of the object as well as methods for basic painting and event handling. javax.swing.JComponent extends the base Component class and refines it for the Swing toolkit. The paintCompo nent() method we have been using in our example is inherited from the JComponent class. HelloComponent is a kind of JComponent and inherits all its public members, just as other GUI components do. The JButton class is also derived from JComponent and therefore shares this functionality. This means that the developer of the JButton class had methods such as paint Component() available with which to implement the behavior of the JButton object, just as we did when creating our example. What’s exciting is that we are perfectly free to further subclass components such as JButton and override their behavior to create our own special types of user-interface components. JButton and HelloComponent3 are, in this respect, equivalent types of things.

0 comments:

Post a Comment