{"id":768,"date":"2015-06-04T15:49:05","date_gmt":"2015-06-04T07:49:05","guid":{"rendered":"https:\/\/ai.zhousir.top\/?p=768"},"modified":"2026-08-18T12:09:35","modified_gmt":"2026-08-18T04:09:35","slug":"java-gui%e7%a8%8b%e5%ba%8f%e8%ae%be%e8%ae%a1%e5%85%a5%e9%97%a8","status":"publish","type":"post","link":"https:\/\/ai.zhousir.top\/?p=768","title":{"rendered":"Java GUI\u7a0b\u5e8f\u8bbe\u8ba1\u5165\u95e8"},"content":{"rendered":"<section id=\"wenyan\" style=\"font-family: system-ui, 'Apple Color Emoji', 'Segoe UI', 'Segoe UI Symbol', 'Noto Sans', Roboto, sans-serif; line-height: 1.75; font-size: 16px; color: #000000; caret-color: #000000;\" data-provider=\"WenYan\">\n<p style=\"margin: 1em 0px;\">\u5b9e\u9645\u5e94\u7528\u91cc\u8bb8\u591a\u754c\u9762\u90fd\u5c5e\u4e8e GUI\u3002\u6bd4\u5982\u70b9\u51fb QQ \u56fe\u6807\u4f1a\u5f39\u51fa\u767b\u5f55\u5bf9\u8bdd\u6846\uff0c\u8fd9\u4e2a\u56fe\u6807\u6240\u4ee3\u8868\u7684\u4ea4\u4e92\u65b9\u5f0f\u5c31\u662f\u56fe\u5f62\u5316\u7528\u6237\u754c\u9762\u3002\u8f6c\u81f3\uff1a<a href=\"http:\/\/blog.csdn.net\/lijizh1013\/article\/details\/7989784\">Java GUI\u7a0b\u5e8f\u8bbe\u8ba1\u7684\u521d\u6b65\u4e0a<\/a>\u3002<\/p>\n<p style=\"margin: 1em 0px;\"><a href=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2024\/05\/image.png\" rel=\"fancybox\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-3498\" src=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2024\/05\/image.png\" alt=\"\" width=\"2151\" height=\"1105\" srcset=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2024\/05\/image.png 2151w, https:\/\/ai.zhousir.top\/wp-content\/uploads\/2024\/05\/image-768x395.png 768w, https:\/\/ai.zhousir.top\/wp-content\/uploads\/2024\/05\/image-1536x789.png 1536w, https:\/\/ai.zhousir.top\/wp-content\/uploads\/2024\/05\/image-2048x1052.png 2048w\" sizes=\"(max-width: 2151px) 100vw, 2151px\" \/><\/a><\/p>\n<p><!--more--><\/p>\n<h2 style=\"padding: 1px 12.5px; color: #ffffff; margin: 1.2em 0px 1em; border-radius: 4px; display: inline-block; background-color: #4870ac; font-size: 1.3em;\">\u4e00\u3001\u547d\u4ee4\u884c\u4e0e\u56fe\u5f62\u754c\u9762<\/h2>\n<p style=\"margin: 1em 0px;\">\u7528\u6237\u754c\u9762\u5206\u4e24\u7c7b\u3002\u547d\u4ee4\u884c\u7528\u6237\u63a5\u53e3\uff08Command Line User Interface\uff09\u5c31\u662f\u5e38\u89c1\u7684 DOS \u64cd\u4f5c\uff1a\u9700\u8981\u8bb0\u5fc6\u547d\u4ee4\uff0c\u64cd\u4f5c\u4e0d\u76f4\u89c2\uff0c\u4f8b\u5982\u521b\u5efa\u6216\u5220\u9664\u6587\u4ef6\u5939\u3002\u56fe\u5f62\u7528\u6237\u754c\u9762\uff08GUI\uff09\u4ee5\u56fe\u5f62\u5316\u65b9\u5f0f\u4e0e\u7528\u6237\u4ea4\u4e92\uff0c\u4f8b\u5982 Microsoft Windows\u3001Word\uff0c\u66f4\u53cb\u597d\u3001\u66f4\u4e30\u5bcc\uff0c\u662f\u5f53\u524d\u5e94\u7528\u7a0b\u5e8f\u8bbe\u8ba1\u7684\u4e3b\u6d41\u3002<\/p>\n<p style=\"margin: 1em 0px;\">GUI \u4e2d\uff0c<strong style=\"color: #4870ac;\">\u7ec4\u4ef6\uff08Component\uff09<\/strong>\u662f\u57fa\u672c\u7ec4\u6210\u5143\u7d20\uff1a\u51e1\u80fd\u4ee5\u56fe\u5f62\u5316\u65b9\u5f0f\u663e\u793a\u5728\u5c4f\u5e55\u4e0a\u5e76\u80fd\u4e0e\u7528\u6237\u4ea4\u4e92\u7684\u5bf9\u8c61\u90fd\u662f\u7ec4\u4ef6\u3002JDK \u7684 <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">java.awt<\/code> \u5305\u5b9a\u4e49\u4e86 Window\u3001Menu\u3001Button\u3001Label\u3001TextField\u3001Scrollbar \u7b49\u7ec4\u4ef6\u7c7b\u3002<\/p>\n<table style=\"display: table; text-align: left; overflow: auto; border-collapse: collapse; margin: 1.4em auto; max-width: 100%; table-layout: fixed; overflow-wrap: break-word; word-break: break-all;\" width=\"800\">\n<thead>\n<tr>\n<th style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top; text-align: center; font-weight: bold; color: #4870ac; background: #f7f7f7;\">\u7c7b\u578b<\/th>\n<th style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top; text-align: center; font-weight: bold; color: #4870ac; background: #f7f7f7;\">\u8bf4\u660e<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">Container<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u7ee7\u627f Component\uff0c\u672c\u8eab\u4e5f\u662f\u7ec4\u4ef6\uff1b\u63a7\u5236\u7ec4\u4ef6\u5fc5\u987b\u653e\u8fdb\u5bb9\u5668\u624d\u80fd\u663e\u793a<\/td>\n<\/tr>\n<tr>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">Window<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u65e0\u8fb9\u6846\u83dc\u5355\u680f\u7684\u9876\u5c42\u5bb9\u5668\uff0c\u4e00\u822c\u7528\u5b50\u7c7b Frame<\/td>\n<\/tr>\n<tr>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">Panel<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u6700\u7b80\u5355\u5e38\u7528\u7684\u5bb9\u5668\uff0c\u4e0d\u80fd\u72ec\u7acb\u5b58\u5728\uff0c\u5fc5\u987b\u52a0\u5165\u5176\u4ed6\u5bb9\u5668<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2 style=\"padding: 1px 12.5px; color: #ffffff; margin: 1.2em 0px 1em; border-radius: 4px; display: inline-block; background-color: #4870ac; font-size: 1.3em;\">\u4e8c\u3001\u7b2c\u4e00\u4e2a Frame \u4e0e Panel<\/h2>\n<p style=\"margin: 1em 0px;\">\u793a\u4f8b 1\uff1a\u7b2c\u4e00\u4e2a GUI \u5e94\u7528\u7a0b\u5e8f\u3002Frame \u9ed8\u8ba4\u51e0\u4e4e\u53ea\u5bb9\u7eb3\u6807\u9898\u680f\u548c\u6309\u94ae\uff0c\u8981\u7528 setSize\uff1b\u9ed8\u8ba4\u4e0d\u53ef\u89c1\uff0c\u8981\u7528 setVisible(true)\uff1b\u7ec4\u4ef6\u4f4d\u7f6e\u7531\u5e03\u5c40\u7ba1\u7406\u5668\u51b3\u5b9a\u3002<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">import java.awt.*;\nclass TestFirstFrame {\n    public static void main(String args[]) {\n        Frame frame = new Frame(\"\u7b2c\u4e00\u4e2a\u56fe\u5f62\u7528\u6237\u754c\u9762\u5e94\u7528\u7a0b\u5e8f\");\n        Label lbl = new Label(\"\u8fd9\u662f\u6211\u7684\u7b2c\u4e00\u4e2a\u56fe\u5f62\u7528\u6237\u754c\u9762\uff01\");\n        lbl.setBackground(Color.pink);\n        frame.setLayout(new FlowLayout());\n        frame.add(lbl);\n        frame.setSize(200, 100);\n        frame.setVisible(true);\n    }\n}<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">\u793a\u4f8b 2\uff1a\u5bb9\u5668 Panel\u3002\u53d6\u6d88 Frame \u9ed8\u8ba4\u5e03\u5c40\u540e\uff0c\u53ef\u4eba\u5de5\u8bbe\u7f6e panel \u7684\u5c3a\u5bf8\u548c\u4f4d\u7f6e\u3002<code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">setSize<\/code> \u8bbe\u5bbd\u9ad8\uff0c<code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">setLocation<\/code> \u8bbe\u5de6\u4e0a\u89d2\u5750\u6807\uff0c\u5355\u4f4d\u90fd\u662f\u50cf\u7d20\u3002\u6bcf\u4e2a GUI \u5bb9\u5668\u90fd\u6709\u81ea\u5df1\u7684\u5750\u6807\u7cfb\u3002<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">Frame frame = new Frame(\"\u5bb9\u5668Panel\u7684\u4f7f\u7528\");\nPanel panel = new Panel();\nButton btn = new Button(\"\u786e\u5b9a\");\npanel.setBackground(Color.cyan);\npanel.setSize(100, 50);\npanel.setLocation(40, 40);\nframe.setLayout(null);\nframe.add(panel);\npanel.add(btn);\nframe.setLocation(80, 100);\nframe.setSize(200, 100);\nframe.setVisible(true);<\/code><\/pre>\n<h2 style=\"padding: 1px 12.5px; color: #ffffff; margin: 1.2em 0px 1em; border-radius: 4px; display: inline-block; background-color: #4870ac; font-size: 1.3em;\">\u4e09\u3001\u5e03\u5c40\u7ba1\u7406\u5668<\/h2>\n<p style=\"margin: 1em 0px;\">\u5bb9\u5668\u5bf9\u5176\u4e2d\u7ec4\u4ef6\u7684\u6392\u5217\u65b9\u5f0f\uff08\u4f4d\u7f6e\u548c\u5927\u5c0f\uff09\u79f0\u4e3a<strong style=\"color: #4870ac;\">\u5e03\u5c40\uff08Layout\uff09<\/strong>\u3002\u6bcf\u4e2a\u5bb9\u5668\u521b\u5efa\u65f6\u90fd\u4f1a\u5e26\u4e00\u4e2a\u9ed8\u8ba4\u5e03\u5c40\u7ba1\u7406\u5668\uff0c\u4e5f\u53ef\u4ee5\u968f\u65f6\u6362\u6210\u65b0\u7684\uff1a<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">\u5bb9\u5668\u5bf9\u8c61.setLayout(\u5e03\u5c40\u7ba1\u7406\u5668\u5bf9\u8c61);\n\u5e03\u5c40\u7ba1\u7406\u5668 \u5bb9\u5668\u5bf9\u8c61.getLayout();<\/code><\/pre>\n<h3 style=\"padding: 0px; color: #4870ac; margin: 1.2em 0px 1em; font-size: 1.3em;\">FlowLayout<\/h3>\n<p style=\"margin: 1em 0px;\">\u6d41\u5f0f\u5e03\u5c40\uff0c\u662f Panel \u53ca\u5176\u5b50\u7c7b\u7684\u9ed8\u8ba4\u5e03\u5c40\u3002\u7ec4\u4ef6\u6309\u52a0\u5165\u6b21\u5e8f\u9010\u884c\u5b9a\u4f4d\uff0c\u884c\u5185\u4ece\u5de6\u5230\u53f3\uff0c\u4e00\u884c\u6392\u6ee1\u540e\u6362\u884c\uff0c\u6309\u539f\u59cb\u5927\u5c0f\u663e\u793a\u3002\u5bf9\u9f50\u53ef\u7528 LEFT\u3001RIGHT\u3001CENTER\u3002<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">f.setLayout(new FlowLayout());\nf.add(button1);\nf.add(button2);\nf.add(button3);<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">\u5bb9\u5668\u5c3a\u5bf8\u88ab\u91cd\u7f6e\u65f6\uff0c\u7ec4\u4ef6\u4f4d\u7f6e\u4f1a\u8ddf\u7740\u8c03\u6574\uff0c\u4f46\u5c3a\u5bf8\u7ef4\u6301\u4e0d\u53d8\uff1a<\/p>\n<p style=\"margin: 1em 0px;\"><img decoding=\"async\" src=\"http:\/\/img.my.csdn.net\/uploads\/201209\/17\/1347895034_1829.png\" alt=\"\" \/><\/p>\n<h3 style=\"padding: 0px; color: #4870ac; margin: 1.2em 0px 1em; font-size: 1.3em;\">BorderLayout<\/h3>\n<p style=\"margin: 1em 0px;\">\u8fb9\u754c\u5e03\u5c40\uff0c\u662f Window \u53ca\u5176\u5b50\u7c7b\u7684\u9ed8\u8ba4\u5e03\u5c40\u3002\u628a\u5bb9\u5668\u5206\u6210 East\u3001West\u3001South\u3001North\u3001Center \u4e94\u4e2a\u533a\u57df\uff0c\u6bcf\u4e2a\u533a\u57df\u53ea\u80fd\u52a0\u4e00\u4e2a\u7ec4\u4ef6\uff0c\u540e\u52a0\u7684\u4f1a\u66ff\u6362\u5148\u524d\u7684\u3002\u7ec4\u4ef6\u5c3a\u5bf8\u88ab\u5f3a\u5236\u6210\u6240\u5728\u533a\u57df\u5927\u5c0f\u3002<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">f.setLayout(new BorderLayout());\nf.add(btnNorth, \"North\");\nf.add(btnSouth, \"South\");\nf.add(btnWest, \"West\");\nf.add(btnEast, \"East\");\nf.add(btnCenter, \"Center\"); \/\/ \u4e0d\u6307\u540d\u5219\u9ed8\u8ba4 Center<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">\u5bb9\u5668\u5c3a\u5bf8\u53d8\u5316\u65f6\uff0c\u76f8\u5bf9\u4f4d\u7f6e\u4e0d\u53d8\uff1a\u5317\u3001\u5357\u53ea\u80fd\u6c34\u5e73\u7f29\u653e\uff0c\u4e1c\u3001\u897f\u53ea\u80fd\u5782\u76f4\u7f29\u653e\uff0c\u4e2d\u90e8\u4e24\u4e2a\u65b9\u5411\u90fd\u53ef\u7f29\u653e\u3002<\/p>\n<blockquote style=\"display: block; font-size: 0.9em; border-left: 3px solid #4870ac; padding: 0.5em 1em; margin: 0px; background: #f6f8fa; color: #40464f;\">\n<p style=\"margin: 1em 0px;\"><strong style=\"color: #4870ac;\">\u4e00\u53e5\u8bdd\u603b\u7ed3\uff1a\u7ec4\u4ef6\u5fc5\u987b\u653e\u8fdb\u5bb9\u5668\uff1bFrame \u7528 BorderLayout\uff0cPanel \u7528 FlowLayout\uff1bsetVisible(true) \u4e4b\u540e\u7a97\u53e3\u624d\u51fa\u73b0\u3002<\/strong><\/p>\n<\/blockquote>\n<\/section>\n","protected":false},"excerpt":{"rendered":"<p>\u5b9e\u9645\u5e94\u7528\u91cc\u8bb8\u591a\u754c\u9762\u90fd\u5c5e\u4e8e GUI\u3002\u6bd4\u5982\u70b9\u51fb QQ \u56fe\u6807\u4f1a\u5f39\u51fa\u767b\u5f55\u5bf9\u8bdd\u6846\uff0c\u8fd9\u4e2a\u56fe\u6807\u6240\u4ee3\u8868\u7684\u4ea4\u4e92\u65b9\u5f0f\u5c31\u662f\u56fe\u5f62\u5316\u7528\u6237 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[64],"tags":[43],"_links":{"self":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/768"}],"collection":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=768"}],"version-history":[{"count":3,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/768\/revisions"}],"predecessor-version":[{"id":3540,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/768\/revisions\/3540"}],"wp:attachment":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=768"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=768"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=768"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}