-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPopUpMenu.java
More file actions
76 lines (71 loc) · 1.76 KB
/
PopUpMenu.java
File metadata and controls
76 lines (71 loc) · 1.76 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
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class PopUpMenu extends MouseAdapter implements ActionListener {
JFrame frame=new JFrame("popupmenu");
JPopupMenu menu=new JPopupMenu();
JTextField text=new JTextField();
JLabel label=new JLabel("press right");
public static void main(String[] args)
{
PopUpMenu m=new PopUpMenu();
m.go();
}
public void go()
{
JMenuItem item=new JMenuItem("New",KeyEvent.VK_N);
item.addActionListener(this);
menu.add(item);
item=new JMenuItem("Load",KeyEvent.VK_K);
item.addActionListener(this);
menu.add(item);
item=new JMenuItem("Save",KeyEvent.VK_S);
item.addActionListener(this);
menu.add(item);
menu.addSeparator();
item=new JMenuItem("Copy",KeyEvent.VK_C);
item.addActionListener(this);
menu.add(item);
item=new JMenuItem("Cut",KeyEvent.VK_U);
item.addActionListener(this);
menu.add(item);
item=new JMenuItem("Paste",KeyEvent.VK_P);
item.addActionListener(this);
menu.add(item);
item=new JMenuItem("Exit",KeyEvent.VK_E);
item.addActionListener(this);
menu.add(item);
label.addMouseListener(this);
text.setEditable(false);
Container con=frame.getContentPane();
con.add(label,"Center");
con.add(text,"South");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400,400);
frame.setVisible(true);
}
public void mousePressed(MouseEvent e)
{
PopUpShow(e);
}
public void mouseReleased(MouseEvent e)
{
PopUpShow(e);
}
private void PopUpShow(MouseEvent e)
{
//显示弹出菜单是必须调用show方法
if(e.isPopupTrigger())
{
menu.show(e.getComponent(),e.getX(),e.getY());
}
}
public void actionPerformed(ActionEvent e)
{
text.setText(e.getActionCommand());
if(e.getActionCommand() == "Exit")
{
System.exit(0);
}
}
}