-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathJDesktopPaneandJInternalFrame.java
100 lines (76 loc) · 2.71 KB
/
JDesktopPaneandJInternalFrame.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Fig. 14.12: DesktopTest.java
// Demonstrating JDesktopPane.
importjava.awt.*;
import java.awt.event.*;
importjavax.swing.*;
publicclass DesktopTest extends JFrame {
private JDesktopPane theDesktop;
// set up GUI
publicDesktopTest()
{
super("Using a JDesktopPane");
// create menu bar, menu and menu item
JMenuBar bar =new JMenuBar();
JMenu addMenu = new JMenu("Add");
JMenuItem newFrame =new JMenuItem("Internal Frame");
addMenu.add( newFrame );
bar.add( addMenu );
setJMenuBar( bar );
// set up desktop
theDesktop = new JDesktopPane();
getContentPane().add( theDesktop );
// set up listener for newFrame menu item
newFrame.addActionListener(
new ActionListener() { // anonymous inner class
// display new internal window
public void actionPerformed( ActionEvent event ) {
// create internal frame
JInternalFrame frame =newJInternalFrame(
"Internal Frame",true,true,true,true);
// attach panel to internal frame content pane
Container container = frame.getContentPane();
MyJPanel panel =newMyJPanel();
container.add( panel,BorderLayout.CENTER);
// set size internal frame to size of its contents
frame.pack();
// attach internal frame to desktop and show it
theDesktop.add( frame );
frame.setVisible(true);
}
}// end anonymous inner class
);// end call to addActionListener
setSize(600,460);
setVisible(true);
}// end constructor
public static void main( String args[] )
{
DesktopTest application = new DesktopTest();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}// end class DesktopTest
// class to display an ImageIcon on a panel
class MyJPanel extends JPanel {
privateImageIcon imageIcon;
privateString[] images = {"yellowflowers.png","purpleflowers.png",
"redflowers.png","redflowers2.png","lavenderflowers.png"};
// load image
public MyJPanel()
{
intrandomNumber = (int) ( Math.random() *5);
imageIcon = new ImageIcon( images[ randomNumber ] );
}
// display imageIcon on panel
publicvoid paintComponent( Graphics g )
{
// call superclass paintComponent method
super.paintComponent( g );
// display icon
imageIcon.paintIcon(this, g,0,0);
}
// return image dimensions
public Dimension getPreferredSize()
{
return new Dimension( imageIcon.getIconWidth(),
imageIcon.getIconHeight() );
}
}// end class MyJPanel