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个回答

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐