如何启用复制/剪切/粘贴jMenuItem

时间:2012-03-15 12:49:09

标签: java swing jmenuitem cut-and-paste

我在netbeans中制作文本编辑器,并添加了名为Copy,Cut& amp;的jMenuItems。粘贴在“编辑”菜单中。

如何在actionPerformed()

之后启用这些按钮来执行这些功能

这是我的尝试:

    private void CopyActionPerformed(java.awt.event.ActionEvent evt) {                                     

       JMenuItem Copy = new JMenuItem(new DefaultEditorKit.CopyAction()); 
    }                                    

    private void PasteActionPerformed(java.awt.event.ActionEvent evt) {                                      
     JMenuItem Paste = new JMenuItem(new DefaultEditorKit.PasteAction()); 
    }                                     

    private void CutActionPerformed(java.awt.event.ActionEvent evt) {                                    
       JMenuItem Cut = new JMenuItem(new DefaultEditorKit.CutAction()); 
    }                                   

2 个答案:

答案 0 :(得分:6)

剪切,复制,粘贴的简单编辑器示例:

      public class SimpleEditor extends JFrame {

      public static void main(String[] args) {
      JFrame window = new SimpleEditor();
      window.setVisible(true);
      }
      private JEditorPane editPane;   

      public SimpleEditor() {
      editPane = new JEditorPane("text/rtf","");
      JScrollPane scroller = new JScrollPane(editPane);
      setContentPane(scroller);
      setDefaultCloseOperation(EXIT_ON_CLOSE);
      JMenuBar bar = new JMenuBar();
      setJMenuBar(bar);
      setSize(600,500);

      JMenu editMenu = new JMenu("Edit");

      Action cutAction = new DefaultEditorKit.CutAction();
      cutAction.putValue(Action.NAME, "Cut");
      editMenu.add(cutAction);

      Action copyAction = new DefaultEditorKit.CopyAction();
      copyAction.putValue(Action.NAME, "Copy");
      editMenu.add(copyAction);

      Action pasteAction = new DefaultEditorKit.PasteAction();
      pasteAction.putValue(Action.NAME, "Paste");
      editMenu.add(pasteAction);

      bar.add(editMenu);
   }

}

希望这有帮助!

答案 1 :(得分:3)

JEditorPane edit=... your instance;

然后使用其中一个

    edit.cut();
    edit.copy();
    edit.paste();