Java Reference
In-Depth Information
40
new ActionListener() // anonymous inner class
41
{
42
// display new internal window
43
@Override
44
public void actionPerformed(ActionEvent event)
45
{
46
// create internal frame
47
JInternalFrame frame = new JInternalFrame(
"Internal Frame" , true , true , true , true );
48
49
50
MyJPanel panel = new MyJPanel();
51
frame.add(panel, BorderLayout.CENTER );
52
frame.pack(); // set internal frame to size of contents
53
54
theDesktop.add(frame); // attach internal frame
frame.setVisible( true ); // show internal frame
55
56
}
57
}
58
);
59
} // end DesktopFrame constructor
60
} // end class DesktopFrame
61
62
// class to display an ImageIcon on a panel
63
class MyJPanel extends JPanel
64
{
65
private static final SecureRandom generator = new SecureRandom();
66
private final ImageIcon picture; // image to be displayed
67
private final static String[] images = { "yellowflowers.png" ,
68
"purpleflowers.png" , "redflowers.png" , "redflowers2.png" ,
69
"lavenderflowers.png" };
70
71
// load image
72
public MyJPanel()
73
{
74
int randomNumber = generator.nextInt(images.length);
75
picture = new ImageIcon(images[randomNumber]); // set icon
76
}
77
78
// display imageIcon on panel
79
@Override
80
public void paintComponent(Graphics g)
81
{
82
super .paintComponent(g);
83
picture.paintIcon( this , g, 0 , 0 ); // display icon
84
}
85
86
// return image dimensions
87
public Dimension getPreferredSize()
88
{
89
return new Dimension(picture.getIconWidth(),
picture.getIconHeight());
90
91
}
92
} // end class MyJPanel
Fig. 22.11 | Multiple-document interface. (Part 2 of 2.)
Search WWH ::




Custom Search