{"id":39534,"date":"2024-11-26T12:17:30","date_gmt":"2024-11-26T04:17:30","guid":{"rendered":"https:\/\/fwq.ai\/blog\/39534\/"},"modified":"2024-11-26T12:17:30","modified_gmt":"2024-11-26T04:17:30","slug":"java%e6%80%8e%e4%b9%88%e5%ae%9e%e7%8e%b0%e4%b8%80%e4%b8%aa%e6%8c%89%e9%92%ae%e6%95%b0%e7%bb%84","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/39534\/","title":{"rendered":"java\u600e\u4e48\u5b9e\u73b0\u4e00\u4e2a\u6309\u94ae\u6570\u7ec4"},"content":{"rendered":"<blockquote><p>\n  \u5728 java \u4e2d\u5b9e\u73b0\u6309\u94ae\u6570\u7ec4\u7684\u6b65\u9aa4\u5982\u4e0b\uff1a\u521b\u5efa\u4e00\u4e2a jbutton \u7c7b\u578b\u7684\u6570\u7ec4\u6765\u5b58\u50a8\u6309\u94ae\u3002\u5faa\u73af\u521d\u59cb\u5316\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u6309\u94ae\uff0c\u5e76\u8bbe\u7f6e\u6587\u672c\u3002\u521b\u5efa\u4e00\u4e2a jpanel \u5e76\u4f7f\u7528 add() \u65b9\u6cd5\u5c06\u6309\u94ae\u6dfb\u52a0\u5230\u9762\u677f\u4e2d\u3002\uff08\u53ef\u9009\uff09\u4e3a\u6bcf\u4e2a\u6309\u94ae\u6dfb\u52a0 actionlistener \u4ee5\u54cd\u5e94\u5355\u51fb\u4e8b\u4ef6\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/16\/2024111612361950549.jpg\" class=\"aligncenter\" title=\"java\u600e\u4e48\u5b9e\u73b0\u4e00\u4e2a\u6309\u94ae\u6570\u7ec4\u63d2\u56fe\" alt=\"java\u600e\u4e48\u5b9e\u73b0\u4e00\u4e2a\u6309\u94ae\u6570\u7ec4\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5728 Java \u4e2d\u5b9e\u73b0\u4e00\u4e2a\u6309\u94ae\u6570\u7ec4<\/strong><\/p>\n<p><strong>1. \u521b\u5efa\u4e00\u4e2a\u6309\u94ae\u6570\u7ec4<\/strong><\/p>\n<p>\u8981\u521b\u5efa\u6309\u94ae\u6570\u7ec4\uff0c\u53ef\u4ee5\u4f7f\u7528\u6807\u51c6\u7684 JButton \u7c7b\u5e76\u4f7f\u7528\u6570\u7ec4\u6765\u5b58\u50a8\u5b83\u4eec\u3002<\/p>\n<pre>JButton[] buttons = new JButton[5];<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u521d\u59cb\u5316\u6570\u7ec4<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>\u7136\u540e\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u5faa\u73af\u6765\u521d\u59cb\u5316\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u6309\u94ae\uff1a<\/p>\n<pre>for (int i = 0; i &lt; buttons.length; i++) {\n    buttons[i] = new JButton(\"Button \" + (i + 1));\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u5c06\u6309\u94ae\u6dfb\u52a0\u5230\u9762\u677f\u4e2d<\/strong><\/p>\n<p>\u8981\u5c06\u6309\u94ae\u6dfb\u52a0\u5230 GUI \u9762\u677f\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 JPanel \u7c7b\u548c add() \u65b9\u6cd5\uff1a<\/p>\n<pre>JPanel panel = new JPanel();\nfor (JButton button : buttons) {\n    panel.add(button);\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>4. \u6dfb\u52a0\u76d1\u542c\u5668\uff08\u53ef\u9009\uff09<\/strong><\/p>\n<p>\u60a8\u53ef\u4ee5\u4f7f\u7528 ActionListener \u63a5\u53e3\u4e3a\u6bcf\u4e2a\u6309\u94ae\u6dfb\u52a0\u76d1\u542c\u5668\uff0c\u4ee5\u54cd\u5e94\u5355\u51fb\u4e8b\u4ef6\uff1a<\/p>\n<pre>for (JButton button : buttons) {\n    button.addActionListener(new ActionListener() {\n        @Override\n        public void actionPerformed(ActionEvent e) {\n            \/\/ \u5728\u8fd9\u91cc\u5904\u7406\u5355\u51fb\u4e8b\u4ef6\n        }\n    });\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u793a\u4f8b\u7528\u6cd5\uff1a<\/strong><\/p>\n<p>\u4ee5\u4e0b\u4ee3\u7801\u5c55\u793a\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5176\u4e2d\u4f7f\u7528\u4e86\u6309\u94ae\u6570\u7ec4\uff1a<\/p>\n<pre>import javax.swing.*;\nimport java.awt.event.ActionEvent;\nimport java.awt.event.ActionListener;\n\npublic class ButtonArrayExample {\n\n    public static void main(String[] args) {\n        \/\/ \u521b\u5efa\u6309\u94ae\u6570\u7ec4\n        JButton[] buttons = new JButton[5];\n\n        \/\/ \u521d\u59cb\u5316\u6570\u7ec4\n        for (int i = 0; i &lt; buttons.length; i++) {\n            buttons[i] = new JButton(\"Button \" + (i + 1));\n        }\n\n        \/\/ \u5c06\u6309\u94ae\u6dfb\u52a0\u5230\u9762\u677f\u4e2d\n        JPanel panel = new JPanel();\n        for (JButton button : buttons) {\n            panel.add(button);\n        }\n\n        \/\/ \u521b\u5efa\u4e00\u4e2a JFrame \u5e76\u6dfb\u52a0\u9762\u677f\n        JFrame frame = new JFrame(\"Button Array Example\");\n        frame.add(panel);\n        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);\n        frame.setSize(300, 200);\n        frame.setVisible(true);\n\n        \/\/ \u6dfb\u52a0\u76d1\u542c\u5668\uff08\u53ef\u9009\uff09\n        for (JButton button : buttons) {\n            button.addActionListener(new ActionListener() {\n                @Override\n                public void actionPerformed(ActionEvent e) {\n                    JOptionPane.showMessageDialog(null, \"You clicked button \" + button.getText());\n                }\n            });\n        }\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u600e\u4e48\u5b9e\u73b0\u4e00\u4e2a\u6309\u94ae\u6570\u7ec4\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 java \u4e2d\u5b9e\u73b0\u6309\u94ae\u6570\u7ec4\u7684\u6b65\u9aa4\u5982\u4e0b\uff1a\u521b\u5efa\u4e00\u4e2a jbutton \u7c7b\u578b\u7684\u6570\u7ec4\u6765\u5b58\u50a8\u6309\u94ae\u3002\u5faa\u73af\u521d\u59cb\u5316\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u6309\u94ae\uff0c\u5e76\u8bbe\u7f6e\u6587\u672c\u3002\u521b\u5efa\u4e00\u4e2a jpanel \u5e76\u4f7f\u7528 add() \u65b9\u6cd5\u5c06\u6309\u94ae\u6dfb\u52a0\u5230\u9762\u677f\u4e2d\u3002\uff08\u53ef\u9009\uff09\u4e3a\u6bcf\u4e2a\u6309\u94ae\u6dfb\u52a0 actionlistener \u4ee5\u54cd\u5e94\u5355\u51fb\u4e8b\u4ef6\u3002 \u5982\u4f55\u5728 Java \u4e2d\u5b9e\u73b0\u4e00\u4e2a\u6309\u94ae\u6570\u7ec4 1. \u521b\u5efa\u4e00\u4e2a\u6309\u94ae\u6570\u7ec4 \u8981\u521b\u5efa\u6309\u94ae\u6570\u7ec4\uff0c\u53ef\u4ee5\u4f7f\u7528\u6807\u51c6\u7684 JButton \u7c7b\u5e76\u4f7f\u7528\u6570\u7ec4\u6765\u5b58\u50a8\u5b83\u4eec\u3002 JButton[] buttons = new JButton[5]; \u767b\u5f55\u540e\u590d\u5236 2. \u521d\u59cb\u5316\u6570\u7ec4 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u7136\u540e\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u5faa\u73af\u6765\u521d\u59cb\u5316\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u6309\u94ae\uff1a for (int i = 0; i &lt; buttons.length; i++) { buttons[i] = new JButton(&#8220;Button &#8221; + (i + 1)); } \u767b\u5f55\u540e\u590d\u5236 3. \u5c06\u6309\u94ae\u6dfb\u52a0\u5230\u9762\u677f\u4e2d \u8981\u5c06\u6309\u94ae\u6dfb\u52a0\u5230 GUI \u9762\u677f\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-39534","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/39534","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=39534"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/39534\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=39534"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=39534"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=39534"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}