JMENUITEM을 복사 / 자르거나 붙여 넣는 방법
-
16-12-2019 - |
문제
NetBeans에서 텍스트 편집기를 만들고 편집 메뉴에서 복사, 잘라 내기 및 붙여 넣기라는 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());
}
. 해결책
단순 편집기 예제, 복사, 붙여 넣기 :
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