popupmenu右键菜单组件

package itheima005;

import java.awt.*;
import java.awt.event.*;

public class popupmenuDemo {
    public static void main(String[] args) {
        new popupmenuDemo().init();
    }

    Frame frame = new Frame("测试popupMenu");

    TextArea ta = new TextArea("我爱中华", 6, 30);
    Panel p = new Panel();

    PopupMenu popupMenu = new PopupMenu();
    MenuItem comment = new MenuItem("注释");
    MenuItem cancel = new MenuItem("取消注释");
    MenuItem copy = new MenuItem("复制");
    MenuItem save = new MenuItem("保存");
    Menu save2 = new Menu("保存在");


    private void init() {

        ActionListener actionListener = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                ta.append("点击了" + e.getActionCommand() + "\n");

            }
        };

        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

        comment.addActionListener(actionListener);
        cancel.addActionListener(actionListener);
        copy.addActionListener(actionListener);
        save.addActionListener(actionListener);

        //组装视图
        popupMenu.add(comment);
        popupMenu.add(cancel);
        popupMenu.add(copy);
        save2.add(save);
        popupMenu.add(save2);

        p.setPreferredSize(new Dimension(200, 300));

        //监听鼠标右键释放
        p.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseReleased(MouseEvent e) {
                boolean flag = e.isPopupTrigger();
                if(flag){
                    popupMenu.show(p,e.getX(),e.getY());
                }
            }
        });

        p.add(popupMenu);

        //
        frame.add(ta);
        frame.add(p,BorderLayout.SOUTH);
        frame.pack();
        frame.setVisible(true);

    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值