{"id":202821,"date":"2025-05-22T11:51:28","date_gmt":"2025-05-22T03:51:28","guid":{"rendered":"https:\/\/server.hk\/cnblog\/202821\/"},"modified":"2025-05-22T11:51:28","modified_gmt":"2025-05-22T03:51:28","slug":"lambda-%e8%a1%a8%e8%be%be%e5%bc%8f%e5%9c%a8-java-gui-%e7%bc%96%e7%a8%8b%e4%b8%ad%e7%9a%84%e5%88%9b%e6%96%b0%e5%ba%94%e7%94%a8","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/202821\/","title":{"rendered":"lambda \u8868\u8fbe\u5f0f\u5728 Java GUI \u7f16\u7a0b\u4e2d\u7684\u521b\u65b0\u5e94\u7528"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>lambda \u8868\u8fbe\u5f0f\u5728 Java GUI \u7f16\u7a0b\u4e2d\u7684\u521b\u65b0\u5e94\u7528<\/h1>\n<p>\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u8981\u52aa\u529b\uff0c\u4f46\u662f\u4e0d\u8981\u6025\uff01\u4eca\u5929\u7684\u8fd9\u7bc7\u6587\u7ae0<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300alambda \u8868\u8fbe\u5f0f\u5728 Java GUI \u7f16\u7a0b\u4e2d\u7684\u521b\u65b0\u5e94\u7528\u300b<\/span>\u5c06\u4f1a\u4ecb\u7ecd\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u60f3\u6df1\u5165\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\uff0c\u53ef\u4ee5\u5173\u6ce8\u6211\uff01\u6211\u4f1a\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u6587\u7ae0\u7684\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u90fd\u80fd\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729945643671ce02b40e7c.jpg\" class=\"aligncenter\"><\/p>\n<h2>lambda \u8868\u8fbe\u5f0f\u5728 Java GUI \u7f16\u7a0b\u4e2d\u7684\u521b\u65b0\u5e94\u7528<\/h2>\n<p><strong>\u6982\u8ff0<\/strong><\/p>\n<p>Lambda \u8868\u8fbe\u5f0f\u4e3a Java GUI \u7f16\u7a0b\u5e26\u6765\u4e86\u9769\u65b0\uff0c\u5141\u8bb8\u4f7f\u7528\u66f4\u7b80\u6d01\u3001\u66f4\u5177\u8868\u8fbe\u529b\u7684\u4ee3\u7801\u6765\u5904\u7406\u4e8b\u4ef6\u548c\u64cd\u4f5c\u7ec4\u4ef6\u3002\u672c\u6587\u5c06\u63a2\u8ba8 lambda \u8868\u8fbe\u5f0f\u5728 Java GUI \u4e2d\u7684\u521b\u65b0\u5e94\u7528\uff0c\u5e76\u901a\u8fc7\u4e00\u4e2a\u5b9e\u6218\u6848\u4f8b\u5c55\u793a\u5176\u4f18\u52bf\u3002<\/p>\n<p><strong>\u4e8b\u4ef6\u5904\u7406<\/strong><\/p>\n<p>\u4f20\u7edf\u4e0a\uff0c\u4e8b\u4ef6\u5904\u7406\u9700\u8981\u4f7f\u7528\u533f\u540d\u5185\u90e8\u7c7b\u6216\u4e13\u95e8\u7684\u4e8b\u4ef6\u4fa6\u542c\u5668\u3002Lambda \u8868\u8fbe\u5f0f\u63d0\u4f9b\u4e86\u66f4\u7b80\u6d01\u7684\u65b9\u6cd5\uff1a<\/p>\n<pre>\/\/ \u6309\u94ae\u70b9\u51fb\u4e8b\u4ef6\uff0c\u4f7f\u7528\u533f\u540d\u5185\u90e8\u7c7b\nbutton.addActionListener(new ActionListener() {\n    @Override\n    public void actionPerformed(ActionEvent e) {\n        \/\/ \u64cd\u4f5c\u4ee3\u7801\n    }\n});\n\n\/\/ \u6309\u94ae\u70b9\u51fb\u4e8b\u4ef6\uff0c\u4f7f\u7528 lambda \u8868\u8fbe\u5f0f\nbutton.addActionListener(e -&gt; {\n    \/\/ \u64cd\u4f5c\u4ee3\u7801\n});<\/pre>\n<p><strong>\u7ec4\u4ef6\u64cd\u4f5c<\/strong><\/p>\n<p>lambda \u8868\u8fbe\u5f0f\u4e5f\u53ef\u4ee5\u7528\u4e8e\u52a8\u6001\u64cd\u4f5c\u7ec4\u4ef6\uff0c\u4f8b\u5982\u8bbe\u7f6e\u6587\u672c\u3001\u83b7\u53d6\u503c\u548c\u66f4\u65b0\u5e03\u5c40\uff1a<\/p>\n<pre>\/\/ \u8bbe\u7f6e\u6587\u672c\uff0c\u4f7f\u7528\u533f\u540d\u5185\u90e8\u7c7b\ntextField.setText(\"\u6587\u672c\");\n\n\/\/ \u8bbe\u7f6e\u6587\u672c\uff0c\u4f7f\u7528 lambda \u8868\u8fbe\u5f0f\ntextField.setText(s -&gt; \"\u6587\u672c\");\n\n\/\/ \u83b7\u53d6\u503c\uff0c\u4f7f\u7528\u533f\u540d\u5185\u90e8\u7c7b\nString value = comboBox.getSelectedItem().toString();\n\n\/\/ \u83b7\u53d6\u503c\uff0c\u4f7f\u7528 lambda \u8868\u8fbe\u5f0f\nString value = (String) comboBox.getSelectedItem();\n\n\/\/ \u66f4\u65b0\u5e03\u5c40\uff0c\u4f7f\u7528\u533f\u540d\u5185\u90e8\u7c7b\nframe.pack();\n\n\/\/ \u66f4\u65b0\u5e03\u5c40\uff0c\u4f7f\u7528 lambda \u8868\u8fbe\u5f0f\nframe.pack(() -&gt; {});<\/pre>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p><strong>GUI \u8ba1\u7b97\u5668<\/strong><\/p>\n<p>\u8ba9\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684 GUI \u8ba1\u7b97\u5668\uff0c\u6f14\u793a lambda \u8868\u8fbe\u5f0f\u7684\u5e94\u7528\u3002<\/p>\n<p><strong>\u4ee3\u7801:<\/strong><\/p>\n<pre>import javax.swing.*;\nimport java.awt.event.*;\n\npublic class Calculator {\n\n    public static void main(String[] args) {\n        \/\/ \u521b\u5efa GUI \u7ec4\u4ef6\n        JFrame frame = new JFrame();\n        JPanel panel = new JPanel();\n        JTextField txtExpression = new JTextField();\n        JButton btnCalculate = new JButton(\"\u8ba1\u7b97\");\n        JLabel lblResult = new JLabel();\n\n        \/\/ \u4e8b\u4ef6\u5904\u7406\uff0c\u4f7f\u7528 lambda \u8868\u8fbe\u5f0f\n        btnCalculate.addActionListener(e -&gt; {\n            String expression = txtExpression.getText();\n            try {\n                int result = eval(expression);\n                lblResult.setText(String.valueOf(result));\n            } catch (Exception ex) {\n                lblResult.setText(\"\u65e0\u6548\u8868\u8fbe\u5f0f\");\n            }\n        });\n\n        \/\/ \u7ec4\u4ef6\u64cd\u4f5c\uff0c\u4f7f\u7528 lambda \u8868\u8fbe\u5f0f\n        txtExpression.setText(\"2 + 2\");\n        lblResult.setText(\"\u7ed3\u679c\uff1a\");\n        frame.pack();\n\n        \/\/ \u8bbe\u7f6e GUI \u5c5e\u6027\n        frame.setVisible(true);\n        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);\n    }\n\n    \/\/ \u8ba1\u7b97\u8868\u8fbe\u5f0f\u7684\u7b80\u5355\u65b9\u6cd5\n    private static int eval(String expression) {\n        return Integer.parseInt(expression);\n    }\n}<\/pre>\n<p>\u4eca\u5929\u5173\u4e8e\u300alambda \u8868\u8fbe\u5f0f\u5728 Java GUI \u7f16\u7a0b\u4e2d\u7684\u521b\u65b0\u5e94\u7528\u300b\u7684\u5185\u5bb9\u5c31\u4ecb\u7ecd\u5230\u8fd9\u91cc\u4e86\uff0c\u662f\u4e0d\u662f\u5b66\u8d77\u6765\u4e00\u76ee\u4e86\u7136\uff01\u60f3\u8981\u4e86\u89e3\u66f4\u591a\u5173\u4e8ejava,GUI\u7684\u5185\u5bb9\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>lambda \u8868\u8fbe\u5f0f\u5728 Java&#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-202821","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202821","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=202821"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202821\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=202821"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=202821"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=202821"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}