2012-03-15 75 views
4

我在netbeans中創建文本編輯器,並添加了名爲Copy,Cut &的jMenuItems粘貼到Edit菜單中。如何啓用複製/剪切/粘貼jMenuItem

如何啓用這些按鈕的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()); 
    }         
+1

請學習java命名約定並堅持他們 – kleopatra 2012-03-15 14:52:10

回答

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

} 

希望這有助於!

+0

謝謝! – donthedestroyer 2012-03-15 14:56:04

+0

沒問題!但是,請閱讀java swing並充分理解代碼如何擴展代碼!:) – 2012-03-15 17:49:44

3
JEditorPane edit=... your instance; 

然後使用

edit.cut(); 
    edit.copy(); 
    edit.paste(); 
相關問題