{"id":66368,"date":"2025-05-03T09:15:39","date_gmt":"2025-05-03T01:15:39","guid":{"rendered":"https:\/\/fwq.ai\/blog\/66368\/"},"modified":"2025-05-03T09:15:39","modified_gmt":"2025-05-03T01:15:39","slug":"java%e4%b8%8b%e6%8b%89%e6%a1%86%e6%80%8e%e4%b9%88%e5%ae%89%e8%a3%85%e7%9b%91%e5%90%ac%e5%99%a8-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/66368\/","title":{"rendered":"java\u4e0b\u62c9\u6846\u600e\u4e48\u5b89\u88c5\u76d1\u542c\u5668"},"content":{"rendered":"<blockquote><p>\n  \u5728 java \u4e2d\u4e3a\u4e0b\u62c9\u6846\u8bbe\u7f6e\u76d1\u542c\u5668\u7684\u65b9\u6cd5\uff1a\u521b\u5efa\u5b9e\u73b0 actionlistener \u63a5\u53e3\u7684\u76d1\u542c\u5668\u7c7b\uff1b\u4f7f\u7528 addactionlistener \u65b9\u6cd5\u5c06\u76d1\u542c\u5668\u6dfb\u52a0\u5230\u4e0b\u62c9\u6846\uff1b\u5728 actionperformed \u65b9\u6cd5\u4e2d\u5904\u7406\u4e0b\u62c9\u6846\u9009\u9879\u66f4\u6539\u4e8b\u4ef6\uff0c\u4f8b\u5982\u83b7\u53d6\u9009\u4e2d\u7684\u9009\u9879\u5e76\u8fdb\u884c\u76f8\u5e94\u5904\u7406\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/15\/2024111520002565627.jpg\" class=\"aligncenter\" title=\"java\u4e0b\u62c9\u6846\u600e\u4e48\u5b89\u88c5\u76d1\u542c\u5668\u63d2\u56fe\" alt=\"java\u4e0b\u62c9\u6846\u600e\u4e48\u5b89\u88c5\u76d1\u542c\u5668\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5728 Java \u4e2d\u4e3a\u4e0b\u62c9\u6846\u8bbe\u7f6e\u76d1\u542c\u5668<\/strong><\/p>\n<p><strong>\u5b89\u88c5\u76d1\u542c\u5668<\/strong><\/p>\n<p>\u5728 Java \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u4e3a\u4e0b\u62c9\u6846\uff08ComboBox\uff09\u8bbe\u7f6e\u76d1\u542c\u5668\uff1a<\/p>\n<ol>\n<li> <strong>\u521b\u5efa\u76d1\u542c\u5668\u7c7b\uff1a<\/strong>\u5b9e\u73b0 ActionListener \u63a5\u53e3\u5e76\u8986\u76d6\u5176 actionPerformed \u65b9\u6cd5\u3002<\/li>\n<li> <strong>\u5c06\u76d1\u542c\u5668\u6dfb\u52a0\u5230\u4e0b\u62c9\u6846\uff1a<\/strong>\u4f7f\u7528 addActionListener \u65b9\u6cd5\u5c06\u76d1\u542c\u5668\u9644\u52a0\u5230\u4e0b\u62c9\u6846\u3002<\/li>\n<li> <strong>\u5904\u7406\u4e8b\u4ef6\uff1a<\/strong>\u5728 actionPerformed \u65b9\u6cd5\u4e2d\uff0c\u5904\u7406\u5f53\u4e0b\u62c9\u6846\u4e2d\u7684\u9009\u9879\u53d1\u751f\u66f4\u6539\u65f6\u8981\u6267\u884c\u7684\u4ee3\u7801\u3002<\/li>\n<\/ol>\n<p><strong>\u793a\u4f8b\u4ee3\u7801<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4e3a\u4e0b\u62c9\u6846\u8bbe\u7f6e\u76d1\u542c\u5668\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre>import javax.swing.*;\nimport java.awt.event.ActionListener;\n\npublic class ComboBoxListenerExample {\n\n    public static void main(String[] args) {\n        \/\/ \u521b\u5efa\u4e0b\u62c9\u6846\n        JComboBox&lt;String&gt; comboBox = new JComboBox&lt;&gt;(new String[] {\"Option 1\", \"Option 2\", \"Option 3\"});\n\n        \/\/ \u521b\u5efa\u76d1\u542c\u5668\u7c7b\n        class ComboBoxActionListener implements ActionListener {\n            @Override\n            public void actionPerformed(ActionEvent e) {\n                \/\/ \u83b7\u53d6\u4e0b\u62c9\u6846\u4e2d\u9009\u5b9a\u7684\u9009\u9879\n                String selectedItem = (String) comboBox.getSelectedItem();\n\n                \/\/ \u5904\u7406\u9009\u9879\u66f4\u6539\n                System.out.println(\"Selected item: \" + selectedItem);\n            }\n        }\n\n        \/\/ \u521b\u5efa\u76d1\u542c\u5668\u5b9e\u4f8b\n        ActionListener listener = new ComboBoxActionListener();\n\n        \/\/ \u5c06\u76d1\u542c\u5668\u6dfb\u52a0\u5230\u4e0b\u62c9\u6846\n        comboBox.addActionListener(listener);\n\n        \/\/ \u521b\u5efa\u548c\u663e\u793a\u7a97\u53e3\n        JFrame frame = new JFrame(\"ComboBox Listener Example\");\n        frame.add(comboBox);\n        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);\n        frame.pack();\n        frame.setVisible(true);\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u4e0b\u62c9\u6846\u600e\u4e48\u5b89\u88c5\u76d1\u542c\u5668\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8IDCBABY\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 java \u4e2d\u4e3a\u4e0b\u62c9\u6846\u8bbe\u7f6e\u76d1\u542c\u5668\u7684\u65b9\u6cd5\uff1a\u521b\u5efa\u5b9e\u73b0 actionlistener \u63a5\u53e3\u7684\u76d1\u542c\u5668\u7c7b\uff1b\u4f7f\u7528 addactionlistener \u65b9\u6cd5\u5c06\u76d1\u542c\u5668\u6dfb\u52a0\u5230\u4e0b\u62c9\u6846\uff1b\u5728 actionperformed \u65b9\u6cd5\u4e2d\u5904\u7406\u4e0b\u62c9\u6846\u9009\u9879\u66f4\u6539\u4e8b\u4ef6\uff0c\u4f8b\u5982\u83b7\u53d6\u9009\u4e2d\u7684\u9009\u9879\u5e76\u8fdb\u884c\u76f8\u5e94\u5904\u7406\u3002 \u5982\u4f55\u5728 Java \u4e2d\u4e3a\u4e0b\u62c9\u6846\u8bbe\u7f6e\u76d1\u542c\u5668 \u5b89\u88c5\u76d1\u542c\u5668 \u5728 Java \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u4e3a\u4e0b\u62c9\u6846\uff08ComboBox\uff09\u8bbe\u7f6e\u76d1\u542c\u5668\uff1a \u521b\u5efa\u76d1\u542c\u5668\u7c7b\uff1a\u5b9e\u73b0 ActionListener \u63a5\u53e3\u5e76\u8986\u76d6\u5176 actionPerformed \u65b9\u6cd5\u3002 \u5c06\u76d1\u542c\u5668\u6dfb\u52a0\u5230\u4e0b\u62c9\u6846\uff1a\u4f7f\u7528 addActionListener \u65b9\u6cd5\u5c06\u76d1\u542c\u5668\u9644\u52a0\u5230\u4e0b\u62c9\u6846\u3002 \u5904\u7406\u4e8b\u4ef6\uff1a\u5728 actionPerformed \u65b9\u6cd5\u4e2d\uff0c\u5904\u7406\u5f53\u4e0b\u62c9\u6846\u4e2d\u7684\u9009\u9879\u53d1\u751f\u66f4\u6539\u65f6\u8981\u6267\u884c\u7684\u4ee3\u7801\u3002 \u793a\u4f8b\u4ee3\u7801 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u4ee5\u4e0b\u662f\u4e00\u4e2a\u4e3a\u4e0b\u62c9\u6846\u8bbe\u7f6e\u76d1\u542c\u5668\u7684\u793a\u4f8b\u4ee3\u7801\uff1a import javax.swing.*; import java.awt.event.ActionListener; public class ComboBoxListenerExample { public static void main(String[] args) { \/\/ \u521b\u5efa\u4e0b\u62c9\u6846 JComboBox&lt;String&gt; comboBox = new JComboBox&lt;&gt;(new String[] {&#8220;Option 1&#8221;, &#8220;Option [&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-66368","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/66368","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=66368"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/66368\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=66368"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=66368"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=66368"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}