{"id":202595,"date":"2025-05-22T09:18:11","date_gmt":"2025-05-22T01:18:11","guid":{"rendered":"https:\/\/server.hk\/cnblog\/202595\/"},"modified":"2025-05-22T09:18:11","modified_gmt":"2025-05-22T01:18:11","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-java-%e5%87%bd%e6%95%b0%e8%bf%9b%e8%a1%8c%e4%ba%8b%e4%bb%b6%e5%a4%84%e7%90%86%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/202595\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528 Java \u51fd\u6570\u8fdb\u884c\u4e8b\u4ef6\u5904\u7406\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u5982\u4f55\u4f7f\u7528 Java \u51fd\u6570\u8fdb\u884c\u4e8b\u4ef6\u5904\u7406\uff1f<\/h1>\n<p>\u4f60\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u76f8\u5173\u7684\u77e5\u8bc6\u5417\uff1f\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5982\u4f55\u4f7f\u7528 Java \u51fd\u6570\u8fdb\u884c\u4e8b\u4ef6\u5904\u7406\uff1f\u300b<\/span>\uff0c\u4e3b\u8981\u4ecb\u7ecd\u7684\u5185\u5bb9\u5c31\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\uff0c\u5982\u679c\u4f60\u60f3\u63d0\u5347\u81ea\u5df1\u7684\u5f00\u53d1\u80fd\u529b\uff0c\u5c31\u4e0d\u8981\u9519\u8fc7\u8fd9\u7bc7\u6587\u7ae0\uff0c\u5927\u5bb6\u8981\u77e5\u9053\u7f16\u7a0b\u7406\u8bba\u57fa\u7840\u548c\u5b9e\u6218\u64cd\u4f5c\u90fd\u662f\u4e0d\u53ef\u6216\u7f3a\u7684\u54e6\uff01<\/p>\n<p>\u5982\u4f55\u4f7f\u7528 Java \u51fd\u6570\u8fdb\u884c\u4e8b\u4ef6\u5904\u7406\uff1f\u4f7f\u7528\u51fd\u6570\u5f0f\u63a5\u53e3\uff08Consumer\u3001Runnable\uff09\u5c06\u4ee3\u7801\u5757\u5173\u8054\u5230\u7279\u5b9a\u4e8b\u4ef6\u3002\u5f53\u4e8b\u4ef6\u53d1\u751f\u65f6\uff0c\u8c03\u7528\u76f8\u5173\u7684\u4ee3\u7801\u5757\uff0c\u5b9e\u73b0\u4e8b\u4ef6\u5904\u7406\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729921166671c808e67763.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528 Java \u51fd\u6570\u8fdb\u884c\u4e8b\u4ef6\u5904\u7406<\/strong><\/p>\n<p><strong>\u7b80\u4ecb<\/strong><\/p>\n<p>\u4e8b\u4ef6\u5904\u7406\u662f\u54cd\u5e94\u5e94\u7528\u7a0b\u5e8f\u4e2d\u53d1\u751f\u7684\u4e8b\u4ef6\u7684\u8fc7\u7a0b\u3002\u5728 Java \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u51fd\u6570\u5f0f\u63a5\u53e3\uff08\u5982 <code>Consumer<\/code> \u548c <code>Runnable<\/code>\uff09\u6765\u5b9e\u73b0\u4e8b\u4ef6\u5904\u7406\u3002<\/p>\n<p><strong>\u51fd\u6570\u5f0f\u63a5\u53e3<\/strong><\/p>\n<p>\u51fd\u6570\u5f0f\u63a5\u53e3\u662f\u4e00\u79cd\u53ea\u58f0\u660e\u4e00\u4e2a\u62bd\u8c61\u65b9\u6cd5\u7684\u63a5\u53e3\u3002\u5728 Java \u4e2d\uff0c\u6709\u4e24\u4e2a\u5e38\u89c1\u7684\u51fd\u6570\u5f0f\u63a5\u53e3\u7528\u4e8e\u4e8b\u4ef6\u5904\u7406\uff1a<\/p>\n<ul>\n<li><code>Consumer&lt;T&gt;<\/code>\uff1a\u63a5\u53d7\u4e00\u4e2a\u7c7b\u578b\u4e3a <code>T<\/code> \u7684\u53c2\u6570\uff0c\u4e0d\u8fd4\u56de\u4efb\u4f55\u503c\u3002<\/li>\n<li><code>Runnable<\/code>\uff1a\u4e0d\u63a5\u53d7\u4efb\u4f55\u53c2\u6570\uff0c\u4e0d\u8fd4\u56de\u4efb\u4f55\u503c\u3002<\/li>\n<\/ul>\n<p><strong>\u4e8b\u4ef6\u5904\u7406<\/strong><\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528\u51fd\u6570\u5f0f\u63a5\u53e3\u6765\u5c06\u4ee3\u7801\u5757\u5173\u8054\u5230\u7279\u5b9a\u7684\u4e8b\u4ef6\u3002\u5f53\u4e8b\u4ef6\u53d1\u751f\u65f6\uff0c\u5c06\u8c03\u7528\u76f8\u5173\u7684\u4ee3\u7801\u5757\u3002<\/p>\n<p>\u4ee5\u4e0b\u793a\u4f8b\u6f14\u793a\u4e86\u5982\u4f55\u4f7f\u7528 <code>Consumer<\/code> \u6765\u5904\u7406\u6309\u94ae\u70b9\u51fb\u4e8b\u4ef6\uff1a<\/p>\n<pre>import java.awt.event.ActionEvent;\n\npublic class ButtonExample {\n\n    public static void main(String[] args) {\n        \/\/ \u521b\u5efa\u4e00\u4e2a\u6309\u94ae\n        JButton button = new JButton(\"\u70b9\u51fb\u6211\");\n\n        \/\/ \u5c06\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\u5173\u8054\u5230\u6309\u94ae\n        button.addActionListener(new ActionListener() {\n            @Override\n            public void actionPerformed(ActionEvent e) {\n                System.out.println(\"\u6309\u94ae\u88ab\u70b9\u51fb\u4e86\uff01\");\n            }\n        });\n    }\n}<\/pre>\n<p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c<code>ActionListener<\/code> \u662f\u4e00\u4e2a\u51fd\u6570\u5f0f\u63a5\u53e3\uff0c\u5b83\u7684 <code>actionPerformed<\/code> \u65b9\u6cd5\u63a5\u53d7\u4e00\u4e2a <code>ActionEvent<\/code> \u53c2\u6570\u3002\u5f53\u6309\u94ae\u88ab\u70b9\u51fb\u65f6\uff0c\u5c06\u8c03\u7528 <code>actionPerformed<\/code> \u65b9\u6cd5\uff0c\u5e76\u6253\u5370\u4e00\u6761\u6d88\u606f\u3002<\/p>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u8003\u8651\u4e00\u4e2a\u5e94\u7528\u7a0b\u5e8f\uff0c\u5176\u4e2d\u7528\u6237\u53ef\u4ee5\u8f93\u5165\u6587\u672c\u5e76\u5355\u51fb\u6309\u94ae\u4ee5\u63d0\u4ea4\u6587\u672c\u3002\u53ef\u4ee5\u4f7f\u7528\u51fd\u6570\u5f0f\u63a5\u53e3\u6765\u5904\u7406\u63d0\u4ea4\u6309\u94ae\u7684\u70b9\u51fb\u4e8b\u4ef6\u5e76\u6267\u884c\u4ee5\u4e0b\u64cd\u4f5c\uff1a<\/p>\n<ul>\n<li>\u9a8c\u8bc1\u8f93\u5165\u7684\u6587\u672c\u3002<\/li>\n<li>\u5982\u679c\u6587\u672c\u6709\u6548\uff0c\u5219\u5c06\u5176\u53d1\u9001\u5230\u670d\u52a1\u5668\u3002<\/li>\n<li>\u5982\u679c\u6587\u672c\u65e0\u6548\uff0c\u5219\u663e\u793a\u9519\u8bef\u6d88\u606f\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0b\u793a\u4f8b\u6f14\u793a\u4e86\u5982\u4f55\u5b9e\u73b0\u6b64\u64cd\u4f5c\uff1a<\/p>\n<pre>import java.awt.event.ActionEvent;\n\npublic class FormExample {\n\n    public static void main(String[] args) {\n        \/\/ \u521b\u5efa\u6587\u672c\u8f93\u5165\u6846\u548c\u63d0\u4ea4\u6309\u94ae\n        JTextField textField = new JTextField();\n        JButton submitButton = new JButton(\"\u63d0\u4ea4\");\n\n        \/\/ \u5c06\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\u5173\u8054\u5230\u63d0\u4ea4\u6309\u94ae\n        submitButton.addActionListener(new ActionListener() {\n            @Override\n            public void actionPerformed(ActionEvent e) {\n                String text = textField.getText();\n\n                \/\/ \u9a8c\u8bc1\u8f93\u5165\u6587\u672c\n                if (isValid(text)) {\n                    \/\/ \u53d1\u9001\u6587\u672c\u5230\u670d\u52a1\u5668\n                    sendTextToServer(text);\n                } else {\n                    \/\/ \u663e\u793a\u9519\u8bef\u6d88\u606f\n                    showError(\"\u8f93\u5165\u65e0\u6548\uff01\");\n                }\n            }\n        });\n    }\n\n    private static boolean isValid(String text) {\n        \/\/ \u8fd4\u56de text \u662f\u5426\u6709\u6548\u7684\u903b\u8f91\n    }\n\n    private static void sendTextToServer(String text) {\n        \/\/ \u53d1\u9001 text \u5230\u670d\u52a1\u5668\u7684\u903b\u8f91\n    }\n\n    private static void showError(String message) {\n        \/\/ \u663e\u793a message \u7684\u903b\u8f91\n    }\n}<\/pre>\n<p>\u4eca\u5929\u5173\u4e8e\u300a\u5982\u4f55\u4f7f\u7528 Java \u51fd\u6570\u8fdb\u884c\u4e8b\u4ef6\u5904\u7406\uff1f\u300b\u7684\u5185\u5bb9\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\uff0c\u5982\u679c\u6709\u4ec0\u4e48\u7591\u95ee\u6216\u8005\u5efa\u8bae\uff0c\u53ef\u4ee5\u5728\u516c\u4f17\u53f7\u4e0b\u591a\u591a\u56de\u590d\u4ea4\u6d41\uff1b\u6587\u4e2d\u82e5\u6709\u4e0d\u6b63\u4e4b\u5904\uff0c\u4e5f\u5e0c\u671b\u56de\u590d\u7559\u8a00\u4ee5\u544a\u77e5\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u4f7f\u7528 Java \u51fd\u6570\u8fdb\u884c\u4e8b\u4ef6&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-202595","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202595","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=202595"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202595\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=202595"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=202595"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=202595"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}