酷代码 AI
菜单
服务商

Java 实现复制粘贴功能:打造简易文本复制粘贴程序

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(); } } } ######[AI写代码神器 | 1497点数解答 | 2024-08-25 09:40:33]

相关提问
本站限时免费提问中
实用工具查看更多
Linux在线手册 [开发类]
Jquery在线手册 [开发类]
今日油价 [生活类]
图片互转base64 [开发类]
时间转换器 [开发类]