Java Reference
In-Depth Information
12 frame.setVisible(
true
);
13 }
14
15
public
Test() {
16 jbtOK.setToolTipText(
"This is a button"
);
17 add(
new
JButton(
"OK"
)
);
18 }
19 }
12.23
Show the output of the following code:
import
javax.swing.*;
public class
Test {
public static void
main(String[] args) {
JButton jbtOK =
new
JButton(
"OK"
);
System.out.println(jbtOK.isVisible());
JFrame frame =
new
JFrame();
System.out.println(frame.isVisible());
}
}
12.24
What happens if you add a button to a container several times, as shown below? Does
it cause syntax errors? Does it cause runtime errors?
JButton jbt =
new
JButton();
JPanel panel =
new
JPanel();
panel.add(jbt);
panel.add(jbt);
panel.add(jbt);
Image icons can be displayed in many GUI components. Image icons are objects
created using the
ImageIcon
class.
Key
Point
An icon is a fixed-size picture; typically it is small and used to decorate components. Images
are normally stored in image files. Java currently supports three image formats: GIF (Graph-
ics Interchange Format), JPEG (Joint Photographic Experts Group), and PNG (Portable Net-
work Graphics). The image file names for these types end with
.gif
,
.jpg
, and
.png
,
respectively. If you have a bitmap file or image files in other formats, you can use image-
processing utilities to convert them into the GIF, JPEG, or PNG format for use in Java.
To display an image icon, first create an
ImageIcon
object using
new
javax.swing.ImageIcon(filename)
. For example, the following statement creates an
icon from an image file
us.gif
in the
image
directory under the current class path:
image-file format
ImageIcon icon =
new
ImageIcon(
"image/us.gif"
);
create
ImageIcon
image/us.gif
is located in
c:\book\image\us.gif
. The back slash (
\
) is the Windows
file path notation. In UNIX, the forward slash (
/
) should be used. In Java, the forward
slash (
/
) is used to denote a relative file path under the Java classpath (e.g.,
image/us.gif
,
as in this example).
file path character
Tip
File names are not case sensitive in Windows but are case sensitive in UNIX. To enable your
programs to run on all platforms, name all the image files consistently, using lowercase.
naming files consistently