Why we use jpanel




















It has two sub-divisions, namely menu bar and content pane. The components of JFrame are known as contents and most of the contents are found in the content pane.

Besides, to add content in the JFrame, one needs to put it in the content pane. JFrame employs a method of windows listener that starts working whenever a person carries out operations like activating, closing, opening, minimizing or maximizing a window.

It also employs a mouse listener so that the frame can react to the actions of the mouse. A JFrame can place inside itself multiple frames and JPanels but all of them depend on the mainframe for their existence. A large number of functions can be created for the JFrame by using not only the methods of Listeners but also the methods of get, set and add methods. In Java an object-oriented programming language there are different types of classes which serve as the framework or design for the creation of objects or a specific data structure, retaining the instance variables of the objects and consequently, maintaining their state and application of the behavioural patterns of the objects.

What is JPanel? What is JFrame? But the parent of the former is javax. While the latter is the child of java. Frame class. In terms of weight, JFrame is heavy and is employed as a top-level window. JFrame is a window used for creating independent GUI applications. While Jpanel is a space where one can put together a group of complex components or operations. Being a window, JFrame contains a title bar. While Jpanel does not contain a title bar.

Note that this class implements the WindowListener interface: we can specifically say it does, or as illlustrated above, not say it: Java knows that any subclass of a class that implements an interface also implements that interface, because at worst it just inherits all of the needed methods.

Given this class, we can write addWindowListener new Terminator ; to give the JFrame the new behavior that we want. Notice that we are defining this subclass just to construct one instance of it, to pass to the addWindowListener method. This is exactly what anonymous classes are useful for: to supply a more compact but complex syntax for constructing one object from a class that is never needed again. Recall that it involves constructing an object from an anonymous nameless class. It says to Java, construct an object from an anonymous subclass whose superclass is WindowAdapter ; the anonymous subclass extends WindowAdapter , inheriting all its methods and overriding only the windowClosing method.

Note that we have discussed two different concepts here. The concept of an adapter class which implements an interface with stub methods. We can use such classes directly or more likely construct subclasses of them and then use objects constructed from these subclasses instead.

The key to an adapter class is that it already implements an interface, with default method meanings; we can easily create a subclass from it, overriding a method or two, to specify different behavior for some action s.

The concept of an anonymous class based on an adapter class. When we need to construct just one object from a subclass, we don't really even need to name the subclass. Instead, we can use special Java syntax to construct an object from a subclass of a named superclass by specifying the superclass name and the overridden methods.

We will see adapter classes used frequently for other kinds of listeners buttons, mouse, keyboard when we study the Controller part of the MVC pattern. It is a general technique that we can use in other places in Java programs too. Examine the folder named basic jframe in the View Demonstrations download.

It shows a short program small View class and tiny Application class that illustrates how to use javax. The console controls this window appearing and disappearing. The window's location changes each time it disappears. When the window is closed, the entire program terminates including the loop in main.

You can experiment with the methods called to attempt to get other forms of interesting behavior, including special behavior for other methods specified by the WindowAdapter.

Typically, each sublcass will define some instance variables and a constructor to initialize them , and overrides the inherited paintComponent method with one that is special to the subclass. To override the inherited paintComponent method, we must write a void method with one parameter: a reference to an object of type Graphics called the Graphics context: see the next section for a short discussion of this class , which contains all the methods for drawing figures, images, and text see its Javadoc for details.

The Java runtime system automatically calls this paintComponent method whenever the size of its JFrame is changed, whenever other windows are moved to uncover part of the JFrame , and whenever the JFrame is deiconified after being iconified subclasses of the WindowAdapter can add special behavior for such events as well. In all cases, the paintComponent method is called to redisplay the contents of the JPanel , now that the amount of it that is visible has been "changed".

The Java runtime automatically supplies the paintComponent method with the right Graphics context we never have to worry about that. If we want to explicitly call the paintComponent method, say call it repeatedly to force Java to animate some actions in a JPanel , we cannot call it directly, because we cannot access its Graphics context. Instead, we must call the void , parameterless method repaint either on a specific JPanel or on its JFrame , which automatically calls it on each JPanel in its content pane.

When a JPanel paints itself, all the figures, images, and text are written into the screen: some are visible, some are not because they go beyond the window's boundary, or is covered by another window. The hidden information is GONE. If the JPanel needs to redisplay any part of itself, it can do so only by calling paintComponent again which typically displays it all. For sophisticated applications, overloaded versions of the paintComponent method might be able to determine what part of its image to repaint, but in simple applications, it just repaints everything.

So, if we need to display a bunch of objects in a JPanel , we must store information about each of these objects say in a collection class , and then the paintComponent method should iterate through this collection, displaying each object in the JPanel.

Whenever the paintComponent method of a JPanel is called whether by the Java system or the program itself , it is supplied with a reference to an object from the Graphics class called its graphics context. It does not store anything drawn in the JPanel. The paintComponent method general uses methods from the Graphics class to draw lines, rectangles, ovals, images, and text.

In the process, it can change some of these states. In the next few sections, we will describe how to display figures, images, text, and buttons in such a panel. Finally, we should always call super. In this section we will examine some simple methods for drawing figures in a graphics context. First of all, whenever we specify coordinates, the graphics context will translate them so that 0,0 is the upper-left hand corner of the JPanel.

Parts of drawings may go beyond the screen. This is not an error: such parts just won't be seen. Here are prototypes for the simplest drawing methods in the Graphics class.

It is opaque by default, but you can change its background color. You can also customize its components using the Layout Managers. It is a window with characteristics of its own.

It has a border, title bar, and button components. Its physical attributes, like size, color, fonts, etc. JFrame has basically two sub-areas, the content pane and the menu bar, but most of the controls are found in the content pane area. In JFrame, you can also put buttons, labels, and check boxes. JFrame is a window commonly used for stand-alone applications, like a warning window, or a notification window, that you would usually see pop out on your screen. It uses a method of windows listener that executes whenever you close, open, maximize, minimize or activate a window.

Frames can also have inner frames, but they are totally dependent on the main frame. There are so many actions you can make for your frame, not only using the listeners, but moreover, using the add, get, and set methods.

JPanel serves as a general purpose container, while JFrame is a window commonly used for stand-alone applications, like a warning window, or a notification window. JPanel represents an area used for more complex operations or applications. In JPanel, one panel can hold many operations, while in JFrame, it can have inner frames for a different purpose. Cite APA 7 ,.



0コメント

  • 1000 / 1000