📄 popupmenutest.java
字号:
//sample13 how to make popmenu
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
class PopupMenutest extends JPanel{
static JFrame frame1;
JLabel ll;
JPopupMenu popupMenu;
public PopupMenutest(){
ll=new JLabel("mouse right click");
popupMenu = new JPopupMenu();
JMenuItem menu1=new JMenuItem("orange");
JMenuItem menu2=new JMenuItem("pineapple");
JMenuItem menu3=new JMenuItem("mango");
menu1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
ll.setText("orange");
}
});
menu2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
ll.setText("pineapple");
}
});
menu1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
ll.setText("mango");
}
});
popupMenu.add(menu1);
popupMenu.add(menu2);
popupMenu.add(menu3);
addMouseListener(new MouseAdapter(){
public void mouseReleased(MouseEvent e){
if(e.isPopupTrigger()){
popupMenu.show(e.getComponent(),e.getX(),e.getY());
}
}
});
add(ll);
}
public static void main(String args[]){
frame1 = new JFrame("");
PopupMenutest p1=new PopupMenutest();
frame1.getContentPane().add("Center",p1);
frame1.getContentPane().setBackground(Color.gray);
frame1.setSize(200,200);
frame1.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
frame1.setVisible(true);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -