Java Reference
In-Depth Information
Display 18.4
Using Icons
(part 2 of 3)
18
public static void
main(String[] args)
19
{
20
IconDemo iconGui =
new
IconDemo();
21
iconGui.setVisible(true);
22
}
23
public
IconDemo()
24
{
25
super
("Icon Demonstration");
26
setSize(WIDTH, HEIGHT);
27
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
28
setBackground(Color.WHITE);
29
setLayout(
new
BorderLayout());
30
JLabel dukeLabel =
new
JLabel("Mood check");
31
ImageIcon dukeIcon =
new
ImageIcon("duke_waving.gif");
32
dukeLabel.setIcon(dukeIcon);
33
add(dukeLabel, BorderLayout.NORTH);
34
JPanel buttonPanel =
new
JPanel();
35
buttonPanel.setLayout(
new
FlowLayout());
36
JButton happyButton =
new
JButton("Happy");
37
ImageIcon happyIcon =
new
ImageIcon("smiley.gif");
38
happyButton.setIcon(happyIcon);
39
happyButton.addActionListener(
this
);
40
buttonPanel.add(happyButton);
41
JButton sadButton =
new
JButton("Sad");
42
ImageIcon sadIcon =
new
ImageIcon("sad.gif");
43
sadButton.setIcon(sadIcon);
44
sadButton.addActionListener(
this
);
45
buttonPanel.add(sadButton);
46
add(buttonPanel, BorderLayout.SOUTH);
47
message =
new
JTextField(TEXT_FIELD_SIZE);
48
add(message, BorderLayout.CENTER);
49
}
50
public void
actionPerformed(ActionEvent e)
51
{
52
String actionCommand = e.getActionCommand();