Java Reference
In-Depth Information
60
61
// determine whether event should trigger pop-up menu
62
private
void
checkForTriggerEvent(MouseEvent event)
63
{
64
if
(event.isPopupTrigger())
popupMenu.show(
event.getComponent(), event.getX(), event.getY());
65
66
67
}
68
}
69
);
70
}
// end PopupFrame constructor
71
72
// private inner class to handle menu item events
73
private
class
ItemHandler
implements
ActionListener
74
{
75
// process menu item selections
76
@Override
77
public
void
actionPerformed(ActionEvent event)
78
{
79
// determine which menu item was selected
80
for
(
int
i =
0
; i < items.length; i++)
81
{
82
if
(event.getSource() == items[i])
83
{
84
getContentPane().setBackground(colorValues[i]);
85
return
;
86
}
87
}
88
}
89
}
// end private inner class ItemHandler
90
}
// end class PopupFrame
Fig. 22.7
|
JPopupMenu
for selecting colors. (Part 3 of 3.)
1
// Fig. 22.8: PopupTest.java
2
// Testing PopupFrame.
3
import
javax.swing.JFrame;
4
5
public
class
PopupTest
6
{
7
public
static
void
main(String[] args)
8
{
9
PopupFrame popupFrame =
new
PopupFrame();
10
popupFrame.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE
);
11
popupFrame.setSize(
300
,
200
);
12
popupFrame.setVisible(
true
);
13
}
14
}
// end class PopupTest
Fig. 22.8
|
Test class for
PopupFrame
. (Part 1 of 2.)