Java Reference
In-Depth Information
Tip
A
splash screen
is an image that is displayed while the application is starting up. If your
program takes a long time to load, you may display a splash screen to alert the user. For
example, the following command:
splash screen
java -splash:image/us.gif TestImageIcon
displays an image while the program
TestImageIcon
is being loaded.
12.25
How do you create an
ImageIcon
from the file
image/us.gif
in the class directory?
12.26
Will the following code display three buttons? Will the buttons display the same icon?
✓
✓
Check
Point
1
import
javax.swing.*;
2
import
java.awt.*;
3
4
public class
Test
extends
JFrame {
5
public static void
main(String[] args) {
6
// Create a frame and set its properties
7 JFrame frame =
new
Test();
8 frame.setTitle(
"ButtonIcons"
);
9 frame.setSize(
200
,
100
);
10 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
11 frame.setVisible(
true
);
12 }
13
14
public
Test() {
15 ImageIcon usIcon =
new
ImageIcon(
"image/us.gif"
);
16 JButton jbt1 =
new
JButton(usIcon);
17 JButton jbt2 =
new
JButton(usIcon);
18
19 JPanel p1 =
new
JPanel();
20 p1.add(jbt1);
21
22 JPanel p2 =
new
JPanel();
23 p2.add(jbt2);
24
25 JPanel p3 =
new
JPanel();
26 p2.add(jbt1);
27
28 add(p1, BorderLayout.NORTH);
29 add(p2, BorderLayout.SOUTH);
30 add(p3, BorderLayout.CENTER);
31 }
32 }
12.27
Can a border or an icon be shared by GUI components?
Key
Point
We have used
JButton
in the examples to demonstrate the basics of GUI programming. This
section will introduce more features of
JButton
. The following sections will introduce GUI
components
JCheckBox
,
JRadioButton
,
JLabel
,
JTextField
, and
JPasswordField
.
More GUI components such as
JTextArea
,
JComboBox
,
JList
,
JScrollBar
, and
JSlider
will be introduced in Chapter 17. The relationship of these classes is pictured in
Figure 12.14.