The JFrameclass itself has only a few methods for changing how frames look. Of course,through the magic of inheritance, most of the methods for working with the size and position of a frame come from the various superclasses of JFrame. Here are some of the most important methods:
illustrates the inheritance hierarchy for the JFrameclass.
Inheritance hierarchy for the frame and component classes in AWT and Swing
TIP: The API notes for this section give what we think are the most important methods for giving frames the proper look and feel. Some of these methods are defined in the JFrameclass. Others come from the various superclasses of JFrame. At some point, you may need to search the API docs to see if there are methods for some special purpose. Unfortunately, that is a bit tedious to do with inherited methods. For example, the to Front method is applicable to objects of type JFrame, but because it is simply inherited from the Window class, the JFramedocumentation doesn’t explain it. If you feel that there should be a method to do something and it isn’t explained in the documentation for the class you are working with, try looking at the API documentation for the methods of the superclassesof that class. The top of each API page has hyperlinks to the superclasses, and inherited methods are listed below the method summary for the new and overridden methods.
As the API notes indicate, the Component class (which is the ancestor of all GUI objects) and the Window class (which is the superclass of the Frame class) are where you need to look to find the methods to resize and reshape frames. For example, the setLocationmethod in the Component class is one way to reposition a component. If you make the call
setLocation(x, y) the top-left corner is located x pixels across and y pixels down, where (0, 0) is the top -left corner of the screen. Similarly, the set Bounds method in Component lets you resize and relocate a component (in particular, a JFrame) in one step, as
Alternatively, you can give the windowing system control on window placement. If you call
before displaying the window, the windowing system picks the location (but not the size), typically with a slight offset from the last window.
NOTE: For a frame, the coordinates of the setLocationand setBoundsare taken relative to the whole screen. As you will see in Chapter 9, for other components inside a container, the measurements are taken relative to the container.
Many methods of component classes come in getter /setter pairs, such as the following methods of the Frame class:
Such a getter /setter pair is called a property. A property has a name and a type. The name is obtained by changing the first letter after the get or set to lowercase. For example, the Frame class has a property with name title and type String. Conceptually, title is a property of the frame. When we set the property, we expect that the title changes on the user’s screen. When we get the property, we expect that we get back the value that we set.
We do not know (or care) how the Frame class implements this property. Perhaps it simply uses its peer frame to store the title. Perhaps it has an instance field private String title; // not required for property
If the class does have a matching instance field, we don’t know (or care) how the getter and setter methods are implemented. Perhaps they just read and write the instance field. Perhaps they do more, such as notifying the windowing system whenever the title changes.
There is one exception to the get /set convention: For properties of type boolean, the getter starts with is. For example, the following two methods define the location By Platform property:
NOTE: Many programming languages, in particular, Visual Basic and C#, have built-in supportfor properties. It is possible that a future version of Java will also have a language constructfor properties.
Determining a Good Frame Size
Remember: if you don’t explicitly size a frame, all frames will default to being 0 by 0 pixels. To keep our example programs simple, we resize the frames to a size that we hope works acceptably on most displays. However, in a professional application, you should check the resolution of the user’s screen and write code that resizes the frames accordingly: a window that looks nice on a laptop screen will look like a postage stamp on a high -resolution screen.
To find out the screen size, use the following steps. Call the static get Default Toolkit method of the Toolkit class to get the Toolkit object. (The Toolkit class is a dumping groundfor a variety of methods that interface with the native windowing system.) Then call the get Screen Sizemethod, which returns the screen size as a Dimension object. A Dimension object
simultaneously stores a width and a height, in public (!) instance variables width and height. Here is the code:
We use 50% of these values for the frame size, and tell the windowing system to position the frame:
We also supply an icon. Because the representation of images is also system dependent, we again use the toolkit to load an image. Then, we set the image as the icon for the frame:
Depending on your operating system, you can see the icon in various places. For example, in Windows, the icon is displayed in the top-left corner of the window, and you can see it in the list of active tasks when you press ALT+TAB. Here are a few additional tips for dealing with frames:
Core Java Related Interview Questions
|J2EE Interview Questions||Core Java Interview Questions|
|JDBC Interview Questions||JSP Interview Questions|
|Android Interview Questions||JavaServer Faces (JSF) Interview Questions|
|Java collections framework Interview Questions||Java 8 Interview Questions|
|Java Collections Interview Questions||Java Exception Handling Interview Questions|
|Java Concurrency Interview Questions||Java Serialization Interview Questions|
|Java Programmer Interview Questions||Java Inheritance Interview Questions|
|Java IO Interview Questions||Object Oriented Programming in PHP Interview Questions|
Core Java Tutorial
An Introduction To Java
The Java Programming Environment
Fundamental Programming Structures In Java
Objects And Classes
Interfaces And Inner Classes
User Interface Components With Swing
Deploying Applications And Applets
Exceptions, Logging, Assertions, And Debugging
All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.