Java 粘贴菜单项,附加到JTextField时不执行任何操作

Java 粘贴菜单项,附加到JTextField时不执行任何操作,java,swing,jtextfield,key-bindings,jpopupmenu,Java,Swing,Jtextfield,Key Bindings,Jpopupmenu,我有一个JTextField,因为Swing中内置了支持,所以使用Cntl-V自动粘贴。但我还需要一个弹出菜单来帮助不太熟悉快捷键的用户。下面的代码 import javax.swing.*; import java.awt.*; public class TestPopup { public static void main(final String[] args) { JFrame frame = new JFrame(); JTextFie

我有一个JTextField,因为Swing中内置了支持,所以使用Cntl-V自动粘贴。但我还需要一个弹出菜单来帮助不太熟悉快捷键的用户。下面的代码

import javax.swing.*;
import java.awt.*;

public class TestPopup
{
    public static void main(final String[] args)
    {
        JFrame frame = new JFrame();
        JTextField widget      = new JTextField(50);
        final JPopupMenu popup = new JPopupMenu();
        popup.add(widget.getActionMap().get("paste"));
        widget.add(popup);
        widget.setComponentPopupMenu(popup);
        frame.add(widget);
        frame.pack();
        frame.setVisible(true);
    }
}
显示粘贴选项,但选中时不执行任何操作。 此外,注释显示为“粘贴”而不是“粘贴”

我做错了什么

*解决方案*

不管怎样,让它工作,使用DefaultEditorKit.pasteAction而不是“粘贴”使粘贴工作(我不清楚“粘贴”操作在它存在时的实际作用)

但这不会解决名称问题,为此我引入了菜单项

import javax.swing.*;
import javax.swing.text.DefaultEditorKit;
import java.awt.*;

public class TestPopup
{
    public static void main(final String[] args)
    {
        JFrame frame = new JFrame();
        JTextField widget      = new JTextField(50);
        final JPopupMenu popup = new JPopupMenu();
        JMenuItem pasteMenuItem = new JMenuItem(widget.getActionMap().get(DefaultEditorKit.pasteAction));
        pasteMenuItem.setText("Paste");
        popup.add(pasteMenuItem);

        widget.setComponentPopupMenu(popup);
        frame.add(widget);
        frame.pack();
        frame.setVisible(true);
    }
}

请注意,
DefaultEditorKit.pasteAction
操作的名称,
“从剪贴板粘贴”
。直接设置菜单项的
操作
可能更容易:

JMenuItem pasteMenuItem = new JMenuItem(new DefaultEditorKit.PasteAction());

@mKorbel i don’t follow为了更快地获得更好的帮助,发布一个(最小的完整和可验证的示例)。
JtextField小部件=新的JtextField(50)顺便说一句-这不会编译。请不要再浪费我们的时间在不可编译的代码片段上,这些代码片段与实际使用的代码“有点模糊”。您是指
DefaultEditorKit.PasteAction
?这里引用了一个完整的示例。
JMenuItem pasteMenuItem = new JMenuItem(new DefaultEditorKit.PasteAction());