4

我正在 netbeans 中制作文本编辑器,并在 Edit 菜单中添加了名为 Copy、Cut & Paste 的 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()); 
    }                                   
4

2 回答 2

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

}

希望这可以帮助!

于 2012-03-15T14:28:25.907 回答
3
JEditorPane edit=... your instance;

然后使用其中一种

    edit.cut();
    edit.copy();
    edit.paste();
于 2012-03-15T13:21:50.057 回答