JAVA图形用户界面——菜单示例

package demo;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JTextArea;
class PopupMenuDemo extends JFrame{
	private JTextArea text;  
	private JPopupMenu popmenu; 
	public PopupMenuDemo(){       
		super("菜单示例");       
		this.setBounds(100,100,500,500);     
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);       
		this.text = new JTextArea("我是中国人");      
		this.getContentPane().add(this.text);      
		this.popmenu = new JPopupMenu();      
		String popstr[]= {"剪切","复制","粘贴"};      
		JMenuItem popmenuitthis[] = new JMenuItem[popstr.length];    
		MyActionListener3 mal = new MyActionListener3(text);    
		for(int i=0;i<popstr.length;i++){          
			popmenuitthis[i]=new JMenuItem(popstr[i]);          
			this.popmenu.add(popmenuitthis[i]);          
			popmenuitthis[i].addActionListener(mal);      
			}       
		this.text.add(this.popmenu);      
		MyMouselistener2 mml = new MyMouselistener2(text,popmenu);       
		this.text.addMouseListener(mml);       
		this.setVisible(true);   
		}
	}
class MyMouselistener2 implements MouseListener {      
	private JTextArea text;       
	private JPopupMenu popmenu;      
	public MyMouselistener2(JTextArea text, JPopupMenu popmenu)
	{           
		this.text = text;            
		this.popmenu = popmenu;        
		}        
	public void mouseClicked(MouseEvent e){            
		if(e.getModifiers()==MouseEvent.BUTTON3_MASK){                
			popmenu.show(text,e.getX(),e.getY());           
			}        
		}        
	public void mouseEntered(MouseEvent e){
		
	}        public void mouseExited(MouseEvent e){
		
	}        public void mousePressed(MouseEvent e){
		
	}        public void mouseReleased(MouseEvent e){
		
	}   
}    
class MyActionListener3 implements ActionListener {     
	private JTextArea text;       
	public MyActionListener3(JTextArea text){           
		this.text=text;        
		}       
	public void actionPerformed(ActionEvent e){   
		if(e.getActionCommand()=="复制"){text.copy();}           
		if(e.getActionCommand()=="粘贴"){text.paste();}            
		if(e.getActionCommand()=="剪切"){text.cut();
		}      
	} 
}
public class demo {       
	public static void main(String[] args) {           
		new PopupMenuDemo();       
		}   
	}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

轻晃痛

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值