我正在 netbeans 中制作文本编辑器,并在编辑菜单中添加了名为复制、剪切和粘贴的 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());
}
最佳答案
带有剪切、复制、粘贴的简单编辑器示例:
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);
}
}
希望这对您有所帮助!
关于java - 如何启用复制/剪切/粘贴 jMenuItem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9720035/