1
+ package gui ;
2
+
3
+ /**
4
+ * RUN:
5
+ * javac -cp .; gui/Popup.java && java -cp .; gui.Popup
6
+ * OUTPUT:
7
+ *
8
+ */
9
+
10
+ import java .io .*;
11
+ import java .util .*;
12
+
13
+ import javax .swing .*;
14
+ import javax .swing .event .*;
15
+
16
+ import java .awt .*;
17
+ import java .awt .event .*;
18
+
19
+ import net .mindview .util .*;
20
+
21
+ public class Popup extends JFrame {
22
+
23
+ private static final int WIDTH = 300 ;
24
+ private static final int HEIHGT = 200 ;
25
+
26
+ private JPopupMenu popup = new JPopupMenu ();
27
+ private JTextField t = new JTextField (10 );
28
+
29
+ public Popup () {
30
+ setLayout (new FlowLayout ());
31
+
32
+ add (t );
33
+
34
+ ActionListener al = new ActionListener () {
35
+ public void actionPerformed (ActionEvent e ) {
36
+ t .setText (((JMenuItem )e .getSource ()).getText ());
37
+ }
38
+ };
39
+
40
+ JMenuItem m = new JMenuItem ("Hither" );
41
+ m .addActionListener (al );
42
+ popup .add (m );
43
+
44
+ m = new JMenuItem ("Yon" );
45
+ m .addActionListener (al );
46
+ popup .add (m );
47
+
48
+ m = new JMenuItem ("Afar" );
49
+ m .addActionListener (al );
50
+ popup .add (m );
51
+
52
+ popup .addSeparator ();
53
+
54
+ m = new JMenuItem ("Stay Here" );
55
+ m .addActionListener (al );
56
+ popup .add (m );
57
+
58
+ PopupListener pl = new PopupListener ();
59
+ addMouseListener (pl );
60
+
61
+ t .addMouseListener (pl );
62
+ }
63
+
64
+
65
+ class PopupListener extends MouseAdapter {
66
+
67
+ public void mousePressed (MouseEvent e ) {
68
+ maybeShowPopup (e );
69
+ }
70
+
71
+ public void mouseReleased (MouseEvent e ) {
72
+ maybeShowPopup (e );
73
+ }
74
+
75
+ public void maybeShowPopup (MouseEvent e ) {
76
+ popup .show (e .getComponent (), e .getX (), e .getY ());
77
+ }
78
+ }
79
+
80
+
81
+ public static void main (String [] args ) {
82
+ SwingConsole .run (new Popup (), WIDTH , HEIHGT );
83
+ }
84
+ }
0 commit comments