用java写了一个弹出菜单JPopupMenu
如何使用mouseClicked做鼠标单击右键的事件处理
实现的要求是在我的JTextArea内

解决方案 »

  1.   

    package guidemo; import java.awt.*; 
    import java.awt.event.MouseEvent; 
    import java.awt.event.MouseListener; /** 
    * <p>Title: 图形用户界面</p> 

    * <p>Description: 简单的图形界面编程</p> 

    * <p>Copyright: Copyright (c) 2006</p> 

    * <p>Company: </p> 

    * @author vic 
    * @version 1.0 
    */ 
    public class ColorFrame extends Frame implements MouseListener { 
    Label L; //标签 
    TextField T; //文本域 
    Button B1, B2; //按钮 
    public ColorFrame() { 
    this.setLayout(null); //想要手动指定各组件的的位置 
    L = new Label("输入学号:"); //设定标签L内容 
    L.setBounds(60, 50, 50, 25); //设定标签L外观 
    this.add(L); //将标签L添加到窗口中 
    T = new TextField("请在这里输入"); //设定文本域T的内容 
    T.setBounds(125, 50, 90, 25); //设定文本域T的外观 
    this.add(T); //将文本域T添加到窗口中 
    B1 = new Button("变红!"); //设定按钮B1的内容 
    B1.setBounds(25, 90, 90, 25); //设定按钮B1的外观 
    B1.addMouseListener(this);//在B1上注册鼠标监听器 
    this.add(B1); //将按钮B1添加到窗口中 
    B2 = new Button("变绿!"); 
    B2.setBounds(125, 90, 90, 25); 
    B2.addMouseListener(this); 
    this.add(B2); 
    WindowDestroyer Listener = new WindowDestroyer(); //创建关闭窗口监听器 
    this.addWindowListener(Listener); //将监听器添加到窗口中 
    this.setBackground(Color.yellow); //设定窗口背景颜色 
    this.setTitle("This is Frame!"); //设定窗口标题文字 
    this.setBounds(0, 0, 250, 220); //设定窗口位置和大小 
    this.setVisible(true); //显示窗口 
    } public void mouseClicked(MouseEvent e) { 
    if (e.getComponent() == B1) {//getComponent返回按钮上面的字符串 
    this.setBackground(Color.red); 

    if (e.getComponent() == B2) { 
    this.setBackground(Color.green); 

    } public void mouseExited(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseReleased(MouseEvent e) {} public void mousePressed(MouseEvent e) {} public static void main(String[] args) { 
    new ColorFrame();