简体   繁体   English

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

[英]How to make my Popup menu works in Java?

I found a popup menu program and it works standalone. 我找到了一个弹出菜单程序,它可以独立运行。 And then I made another program "Note", within which I hope to add the popup menu functionality into. 然后,我制作了另一个程序“ Note”,希望在其中添加弹出菜单功能。

The popup menu part now is below: 现在,弹出菜单部分如下:

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!");
    }
  }

}

And the "Note" code is below (partial): 并且“注释”代码在下面(部分):

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");

    }

But with these codes the "Note" can work normally while the popup menu doesn't work. 但是,使用这些代码,在弹出菜单不起作用时,“注释”可以正常工作。 I guess the jFrame related code is not correct, but I don't know how to correct it. 我猜jFrame相关的代码不正确,但是我不知道如何更正它。 Who can help? 谁能帮忙? Thanks! 谢谢!

Plus: I used NetBeans to make the project and below is the compilation info (it is hard to understand). 另外:我用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)

I found the solution as below. 我发现解决方案如下。 The key is that no JFrame instance can be found while it is there already, this means no "new" needed and just call its functions. 关键是已经存在的JFrame实例无法找到,这意味着不需要“新”实例,只需调用其函数即可。 And notice the sequence: setContentPane() and then initComponents(). 并注意以下顺序: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();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM