Nickisacode 2017-08-11 09:07 采纳率: 0%
浏览 616

show(e.getComponent(), p.x, p.y); 方法老是调用不了

package MyMenuBar;

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

import javax.swing.*;

public class JPopupMenu extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;

public JPopupMenu() {
    super();
    setTitle("创建弹出式菜单");
    setBounds(100, 100, 500, 375);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    final JLabel label = new JLabel();
    label.setText("                请在窗口中单击鼠标右键!");
    getContentPane().add(label, BorderLayout.CENTER);

    final JPopupMenu popupMenu = new JPopupMenu();// 创建弹出式菜单对象
    // 为窗体的顶层容器添加鼠标事件监听器
    addMouseListener(new MouseAdapter(){
           public void mouseClicked(MouseEvent e){
               //如果点击右键  显示子菜单
               if(e.getButton() == MouseEvent.BUTTON3){
                   //获取相对于窗口的逻辑位置
                   Point p = e.getPoint();

                   popupMenu.show(e.getComponent(), p.x, p.y);
               }

               //否则不显示子菜单
               else{
                   popupMenu.setVisible(false);
               }
           }
       });
    // 添加鼠标

    final JMenuItem cutItem = new JMenuItem("剪切");
    cutItem.addActionListener(new ItemListener());
    popupMenu.add(cutItem);

    final JMenuItem copyItem = new JMenuItem("复制");
    copyItem.addActionListener(new ItemListener());
    popupMenu.add(copyItem);

    final JMenuItem pastItem = new JMenuItem("粘贴");
    pastItem.addActionListener(new ItemListener());
    popupMenu.add(pastItem);

    final JMenu editMenu = new JMenu("编辑");
    popupMenu.add(editMenu);

    final JMenuItem readItem = new JMenuItem("只读");
    readItem.addActionListener(new ItemListener());
    editMenu.add(readItem);

    final JMenuItem writeItem = new JMenuItem("可写");
    writeItem.addActionListener(new ItemListener());
    editMenu.add(writeItem);

    final JMenu fontMenu = new JMenu("字体");
    editMenu.add(fontMenu);

    final JMenuItem boldfacedItem = new JMenuItem("加粗");
    boldfacedItem.addActionListener(new ItemListener());
    fontMenu.add(boldfacedItem);

    final JMenuItem italicItem = new JMenuItem("斜体");
    italicItem.addActionListener(new ItemListener());
    fontMenu.add(italicItem);

}

private class ItemListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        JMenuItem menuItem = (JMenuItem) e.getSource();
        System.out.println("您单击的是菜单项:" + menuItem.getText());
    }
}

public static void main(String[] args) {
    // TODO Auto-generated method stub
    JPopupMenu frame = new JPopupMenu();
    frame.setVisible(true);

}

}

popupMenu.show(e.getComponent(), p.x, p.y); 方法老是调用不了,一调用它就中间显示一个横线

报错:he method show(boolean) in the type Component is not applicable for the arguments (Component, int, int)

  • 写回答

1条回答 默认 最新

  • zqbnqsdsmd 2018-06-14 16:11
    关注
    评论

报告相同问题?

悬赏问题

  • ¥30 关于#java#的问题,请各位专家解答!
  • ¥30 vue+element根据数据循环生成多个table,如何实现最后一列 平均分 合并
  • ¥20 pcf8563时钟芯片不启振
  • ¥20 pip2.40更新pip2.43时报错
  • ¥15 换yum源但仍然用不了httpd
  • ¥50 C# 使用DEVMOD设置打印机首选项
  • ¥15 麒麟V10 arm安装gdal
  • ¥20 OPENVPN连接问题
  • ¥15 flask实现搜索框访问数据库
  • ¥15 mrk3399刷完安卓11后投屏调试只能显示一个设备