-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPopup.java
More file actions
84 lines (59 loc) · 1.74 KB
/
Popup.java
File metadata and controls
84 lines (59 loc) · 1.74 KB
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
package gui;
/**
* RUN:
* javac -cp .; gui/Popup.java && java -cp .; gui.Popup
* OUTPUT:
*
*/
import java.io.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import net.mindview.util.*;
public class Popup extends JFrame {
private static final int WIDTH = 300;
private static final int HEIHGT = 200;
private JPopupMenu popup = new JPopupMenu();
private JTextField t = new JTextField(10);
public Popup() {
setLayout(new FlowLayout());
add(t);
ActionListener al = new ActionListener() {
public void actionPerformed(ActionEvent e) {
t.setText(((JMenuItem)e.getSource()).getText());
}
};
JMenuItem m = new JMenuItem("Hither");
m.addActionListener(al);
popup.add(m);
m = new JMenuItem("Yon");
m.addActionListener(al);
popup.add(m);
m = new JMenuItem("Afar");
m.addActionListener(al);
popup.add(m);
popup.addSeparator();
m = new JMenuItem("Stay Here");
m.addActionListener(al);
popup.add(m);
PopupListener pl = new PopupListener();
addMouseListener(pl);
t.addMouseListener(pl);
}
class PopupListener extends MouseAdapter {
public void mousePressed(MouseEvent e) {
maybeShowPopup(e);
}
public void mouseReleased(MouseEvent e) {
maybeShowPopup(e);
}
public void maybeShowPopup(MouseEvent e) {
popup.show(e.getComponent(), e.getX(), e.getY());
}
}
public static void main(String[] args) {
SwingConsole.run(new Popup(), WIDTH, HEIHGT);
}
}