酷代码 AI
菜单
全部AI  /  热门  /  AI写代码神器
服务商

import javax.swing.*; import java.awt.*; import java.awt.datatransfer.clipboard; import java.awt.datatransfer.stringselection; import java.awt.datatransfer.transferable; import java.awt.event.*; import java.io.*; import java.nio.file.files; import java.nio.file.path; import java.nio.file.paths; import java.time.localdatetime; import java.util.hashmap; import java.util.list; import java.util.map; import java.util.random; public class copy { static private final jtextarea textarea = new jtext

Area(); static private final hashmap<string, string> clipboardMap = new hashmap<>(); public static void main(String[] args) { jframe frame = new jframe("Copy Text"); frame.setdefaultcloseoperation(jframe.exit_on_close); scrollpane scrollpane = new scrollpane(textarea); frame.add(scrollpane, borderlayout.center); textarea.seteditable(false); jmenu menu = new jmenu("Options"); jmenuitem copyItem = new jmenuitem("Copy"); jmenuitem pasteItem = new jmenuitem("Paste"); copyItem.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { copytext(); } }); pasteItem.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { pastetext(); } }); menu.add(copyItem); menu.add(pasteItem); jmenubar menubar = new jmenubar(); menubar.add(menu); frame.setjmenubar(menubar); frame.setsize(400, 400); frame.setvisible(true); } private static void copytext() { string text = textarea.gettext(); clipboard clipboard = toolkit.getdefaulttoolkit().getsystemclipboard(); clipboard.setcontents(new stringselection(text), null); // Store text in clipboardMap with timestamp localdatetime now = localdatetime.now(); clipboardMap.put(now.tostring(), text); joptionpane.showmessageDialog(null, "Text copied to clipboard"); } private static void pastetext() { clipboard clipboard = toolkit.getdefaulttoolkit().getsystemclipboard(); transferable contents = clipboard.getcontents(null); try { if (contents != null && contents.issupported(datatransfer.stringflavor)) { string text = (string) contents.gettransferdata(datatransfer.stringflavor); textarea.settext(text); joptionpane.showmessageDialog(null, "Text pasted from clipboard"); } } catch (exception e) { e.printstacktrace(); } } } [2024-08-25 09:40:33 | AI写代码神器 | 1497点数解答]

相关提问