如何启用复制/剪切/粘贴JmenuItem
-
16-12-2019 - |
题
我正在NetBeans中制作文本编辑器,并在编辑菜单中添加了名为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());
}
. 解决方案
简单编辑器示例与剪切,复制,粘贴:
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);
}
}
.
希望这有帮助!
其他提示
JEditorPane edit=... your instance;
.
然后使用其中一个
edit.cut();
edit.copy();
edit.paste();
. 不隶属于 StackOverflow