如何使我的弹出菜单在Java中工作?

时间:2012-08-06 15:45:36

标签: java swing jpopupmenu

我找到了一个弹出菜单程序,它可以独立运行。然后我创建了另一个程序“Note”,我希望在其中添加弹出菜单功能。

弹出菜单部分现在如下:

package my.demo;

// The original code is from link: http://www.java2s.com/Code/Java/Swing-JFC/AsimpleexampleofJPopupMenu.htm
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.ImageIcon;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.border.BevelBorder;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;

public class PopupMenuExample extends JPanel {

  public JPopupMenu popup;

  public PopupMenuExample() {

    popup = new JPopupMenu();

    ActionListener menuListener;
        menuListener = new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent event) {
            System.out.println("Popup menu item ["
        + event.getActionCommand() + "] was pressed.");
    }
 };

    JMenuItem item;
    popup.add(item = new JMenuItem("Left", new ImageIcon("1.gif")));
    item.setHorizontalTextPosition(JMenuItem.RIGHT);
    item.addActionListener(menuListener);

    popup.add(item = new JMenuItem("Center", new ImageIcon("2.gif")));
    item.setHorizontalTextPosition(JMenuItem.RIGHT);
    item.addActionListener(menuListener);

    popup.add(item = new JMenuItem("Right", new ImageIcon("3.gif")));
    item.setHorizontalTextPosition(JMenuItem.RIGHT);
    item.addActionListener(menuListener);

    popup.add(item = new JMenuItem("Full", new ImageIcon("4.gif")));
    item.setHorizontalTextPosition(JMenuItem.RIGHT);
    item.addActionListener(menuListener);

    popup.addSeparator();

    popup.add(item = new JMenuItem("Settings . . ."));
    item.addActionListener(menuListener);

    popup.setLabel("Justification");
    popup.setBorder(new BevelBorder(BevelBorder.RAISED));
    popup.addPopupMenuListener(new PopupPrintListener());   // listener of Popup menu

    addMouseListener(new MousePopupListener());         // listener of mouse
  }

  // An inner class to check whether mouse events are the popup trigger
  class MousePopupListener extends MouseAdapter {
      @Override
    public void mousePressed(MouseEvent e) {
      checkPopup(e);
    }

      @Override
    public void mouseClicked(MouseEvent e) {
      checkPopup(e);
    }

      @Override
    public void mouseReleased(MouseEvent e) {
      checkPopup(e);
    }

    private void checkPopup(MouseEvent e) {
      if (e.isPopupTrigger()) {
        popup.show(PopupMenuExample.this, e.getX(), e.getY());
      }
    }
  }

  // An inner class to show when popup events occur
  class PopupPrintListener implements PopupMenuListener {
    @Override
    public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
      System.out.println("Popup menu will be visible!");
    }

      @Override
    public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
      System.out.println("Popup menu will be invisible!");
    }

      @Override
    public void popupMenuCanceled(PopupMenuEvent e) {
      System.out.println("Popup menu is hidden!");
    }
  }

}

“注意”代码低于(部分):

package my.demo;

import java.awt.event.*;
import java.io.*;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import javax.swing.*;

/**
 *
 * @author root
 */
public class MyNoteUI extends javax.swing.JFrame {

    JFrame jFrame;
    JFileChooser fc;

    /**
     * Creates new form MyNoteUI
     */
    public MyNoteUI() {

        initComponents();

        jFrame = new javax.swing.JFrame();
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jFrame.setContentPane(new PopupMenuExample());
        jFrame.setTitle("My NoteUI");

    }

但是使用这些代码时,“注意”可以正常工作,而弹出菜单不起作用。我猜jFrame相关代码不正确,但我不知道如何纠正它。谁可以帮忙?谢谢!

加: 我使用NetBeans来制作项目,下面是编译信息(很难理解)。

/home/tomxue/mycode/0___GitHub/MyNote/nbproject/build-impl.xml:1026: The following error occurred while executing this line:
/home/tomxue/mycode/0___GitHub/MyNote/nbproject/build-impl.xml:853: taskdef class org.netbeans.modules.java.j2seproject.copylibstask.CopyLibs cannot be found
 using the classloader AntClassLoader[]
BUILD FAILED (total time: 0 seconds)

1 个答案:

答案 0 :(得分:0)

我找到了解决方案如下。关键是当它已经存在时,没有找到JFrame实例,这意味着不需要“新”,只需调用它的函数即可。并注意序列:setContentPane()然后是initComponents()。

public class MyNoteUI extends javax.swing.JFrame {

    JFileChooser fc;

    /**
     * Creates new form MyNoteUI
     */
    public MyNoteUI() {

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setContentPane(new PopupMenuExample());
        setTitle("My NoteUI");

        initComponents();
    }