Introduction
Handling images in an application is a common problem for many beginning Java programmers. The standard way to access images in a Java application is by using thegetResource()
method.
This tutorial shows you how to
use the IDE's GUI Builder to generate the code to include images (and other resources)
in your application. In addition, you will learn how
to customize the way the IDE generates image handling code.The application that results from this tutorial will be a simple JFrame that contains one JLabel that displays a single image.
Creating the Application
- Choose File > New Project.
- In the New Project wizard, select Java > Java Application and click Next.
- For Project Name, type
ImageDisplayApp
. - Clear the Create Main Class checkbox.
- Click Finish.
Creating the Application Form
In this section, you create the JFrame form and add a JLabel to the form.To create the JFrame form:
- In the Projects window, expand the ImageDisplayApp node.
- Right-click the Source Packages node and choose New > JFrame Form.
- For Class Name, type
ImageDisplay
. - For Package Name, type
org.me.myimageapp
. - Click Finish.
- In the Palette, select the Label component and drag it to the JFrame.
Adding a Package for the Image
When you use images or other resources in an application, typically you create a separate Java package for the resources. On your local filesystem, a package corresponds with a folder.To create a package for the image:
- In the Projects window, right-click the
org.me.myimageapp
node and choose New > Java Package.
- In the Package Name text field of the New Java Package wizard, replace
newpackage
withresources
so that the new package is calledorg.me.myimageapp.resources
. - Click Finish.
Source Packages
folder.Displaying the Image on the Label
In this application, the image will be embedded within a JLabel component.To add the image to the label:
- In the GUI Designer, select the label that you have added to your form.
- In the Properties window, click the Properties category and scroll to the Icon property.
- Click the ellipsis (...) button.
The icon property editor is displayed. - In the icon property dialog box, click Import to Project.
- In the file chooser navigate to any image that is on your system that you want to use. Then click Next.
- In the Select target folder page of the wizard, select the
newpackage
folder and click Finish. - Click OK to close the icon property dialog box.
- Copies the image to your project. Therefore, when you build and distribute the application, the image is included in the distributable JAR file.
- Generates code in the ImageDisplay class to access the image.
- Displays your image on the label in the Design view of your form.
- In the Properties window, select the
text
property and deletejLabel1
. That value was generated by the GUI Builder as display text for the label. However, you are using the label to display an image rather than text, so that text is not needed. - Drag the button to the center of the form.
- In the GUI Designer, click the Source button. (Choose View > Source Editor Toolbar from the main menu if the Source button is hidden.)
- Scroll down to the line that says Generated Code.
- Click the plus sign (+) to the left of the Generated Code line to display the code that the GUI Designer has generated.
jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/me/myimageapp/newpackage/image.png"))); // NOI18NSince you have used the property editor for
jLabel1
's Icon
property,
the IDE has generated the setIcon
method. The parameter of that method
contains a call to the getResource()
method on an anonymous inner class of ImageIcon
.
Notice that the generated path for the image corresponds with its location in
the application's package structure.
Notes:
- If you use the External Image option in the icon property editor, the IDE will generate an absolute path to the image instead of copying the image to your project. Therefore, the image would appear when you run the application on your system, but it would probably not appear when running the application on another system.
- The
getResource
method is also useful for accessing other types of resources, such as text files that contain data that your application might need to use.
In the Design View, right-click the JLabel and choose Events > Mouse > mouseClicked/mousePressed/mouseReleased from the popup menu.
An event handler is generated for the corresponding event.
Note: You can get the mouse coordinates (for example, the location of a mouse
click) in the event handler using the
event.getPoint()
, event.getX()
, or event.getY()
methods. See Class MouseEvent for details.Displaying the Image as the Background on the Frame
There is no direct support for a background image of JFrame in the GUI Builder, since there is no direct support for a background image of JFrame in Swing.Anyway, there are various indirect ways to achieve such a goal. In this application, the JLabel with the image will be embedded within a JFrame component, while a transparent JPanel will be placed over the JLabel and used as the parent of all the components.
To add a transparent JPanel to the JFrame with the image:
- Choose File > New Project.
- In the New Project wizard, select Java > Java Application and click Next.
- For Project Name, type
BackgroundImageApp
. - Clear the Create Main Class checkbox.
- Click Finish.
- In the Projects window, expand the
BackgroundImageApp
node. - Right-click the Source Packages node and choose New > JFrame Form.
- For Class Name, type
ImageDisplay
. - For Package Name, type
org.me.mybackgroundapp
. - Click Finish.
- In the Design view, right-click the JFrame and choose Set Layout > Grid Bag Layout from the popup menu.
- Right-click the JFrame and choose Add From Pallette > Swing Containers > Panel from the popup menu.
- In the Properties window, deselect the jPanel's
opaque
property. - Right-click the JFrame and choose Add From Pallette > Swing Controls > Label from the popup menu.
- In the Projects window, right-click the
org.me.mybackgroundapp
node and choose New > Java Package. - Click Finish. A new package is added.
- In the GUI Designer, select the label that you have added to your form.
- In the Properties window, click the Properties category and scroll to the Icon property.
- Click the ellipsis (...) button.
- In the icon property dialog box, click Import to Project.
- In the file chooser navigate to any image that is on your system that you want to use. Then click Next.
- In the Select Target Folder page of the wizard, select the
newpackage
resources folder and click Finish. - Click OK to close the icon property dialog box.
- In the Navigator, right-click the jPanel and choose Properties from the popup menu.
- In the Properties dialog box set the
Grid X
,Grid Y
,Weight X
, andWeight Y
properties to1
and theFill
property toBoth
. - Click Close.
- Repeat steps 24 and 25 for the jLabel.
- In the Properties dialog box select the
text
property and deletejLabel1
. The background is done. Now you can drag, a jLabel and a jTextField to the jPanel from the Palette, for example. Both of them will display over the background image.
Note: The advantage of the described solution is that the background image is shown at both design-time and runtime.
Tidak ada komentar:
Posting Komentar