-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPopupMenu.java
More file actions
63 lines (45 loc) · 1.44 KB
/
PopupMenu.java
File metadata and controls
63 lines (45 loc) · 1.44 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
package learn;
import java.awt.event.*;
import javax.swing.*;
import java.awt.Toolkit;
public class PopupMenu{
private JPopupMenu menu;
private Toolkit toolkit;
public PopupMenu(){
JFrame frame = new JFrame("JPopupMenu");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
toolkit = frame.getToolkit();
menu = new JPopupMenu();
JMenuItem menuItemBeep = new JMenuItem("Beep");
menuItemBeep.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
toolkit.beep();
}
});
menu.add(menuItemBeep);
JMenuItem menuItemClose = new JMenuItem("close");
menuItemClose.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
menu.add(menuItemClose);
frame.addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent e) {
if (e.getButton() == e.BUTTON3){
menu.show(e.getComponent(),e.getX(),e.getY());
}
}
});
frame.setSize(250,200);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
new PopupMenu();
// write your code here
}
}