gpt4 book ai didi

java - 如何让我的弹出菜单在 Java 中工作?

转载 作者:行者123 更新时间:2023-12-01 05:12:41 25 4
gpt4 key购买 nike

我找到了一个弹出菜单程序,它可以独立工作。然后我制作了另一个程序“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)

最佳答案

我找到了如下解决方案。关键是,当 JFrame 实例已经存在时,就找不到它,这意味着不需要"new",只需调用它的函数即可。请注意顺序: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();
}

关于java - 如何让我的弹出菜单在 Java 中工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11831582/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com