﻿{"id":81,"date":"2009-06-30T15:51:03","date_gmt":"2009-06-30T15:51:03","guid":{"rendered":""},"modified":"2011-11-18T16:54:29","modified_gmt":"2011-11-18T08:54:29","slug":"81","status":"publish","type":"post","link":"http:\/\/zerobox.org\/notes\/81.html","title":{"rendered":"Java\u6280\u5de7\u4e4b\u5728\u5c4f\u5e55\u4e0a\u5b9a\u4f4d\u7a97\u53e3"},"content":{"rendered":"<p>&nbsp;<\/p>\n<p>Java Abstract Window Toolkit\uff08AWT\uff09\u7684Window.setLocation\u51fd\u6570\u4e3a\u5f00\u53d1\u4eba\u5458\u63d0\u4f9b\u4e86\u4e00\u4e2a\u6761\u7406\u6e05\u6670\u7684\u65b9\u6cd5\u6765\u89e3\u51b3\u5728\u7528\u6237\u5c4f\u5e55\u4e0a\u5b9a\u4f4d\u4e00\u4e2a\u7a97\u53e3\u7684\u95ee\u9898\u3002\u4f46\u7528\u8fd9\u4e2a\u65b9\u6cd5\u5c31\u5fc5\u987b\u8003\u8651\u7cbe\u786e\u7684\u8c61\u7d20\u70b9\uff0c\u8fd9\u5c31\u5f88\u9ebb\u70e6\uff0c\u800c\u7528\u4e00\u4e2a\u7c7b\u4f3c\u4e8ejava.awt.BorderLayout\u7684\u7cfb\u7edf\u5c31\u8981\u597d\u7684\u591a\u3002<\/p>\n<p>\u5728BorderLayout\u4e2d\uff0c\u5404\u90e8\u5206\u90fd\u662f\u7531\u4e0b\u9762\u8fd9\u51e0\u4e2a\u533a\u57df\u51fd\u6570\u4e4b\u4e00\u5b9a\u4f4d\u7684\uff1a<\/p>\n<p>BorderLayout.NORTH<\/p>\n<p>BorderLayout.SOUTH<\/p>\n<p>BorderLayout.CENTER<\/p>\n<p>BorderLayout.WEST<\/p>\n<p>BorderLayout.EAST<\/p>\n<p>\u4e0b\u9762\u7684\u4ee3\u7801\u7528\u51e0\u4e4e\u76f8\u540c\u7684\u65b9\u6cd5\u5728\u5c4f\u5e55\u4e0a\u5b9a\u4f4d\u4e86\u4e00\u4e2a\u7a97\u53e3\u3002\u5b83\u9996\u5148\u786e\u5b9a\u4e86\u4e94\u4e2a\u56fa\u5b9a\u7684\u4f4d\u7f6e\uff0c\u7136\u540e\u7528\u5c4f\u5e55\u5927\u5c0f\u548c\u7a97\u53e3\u5927\u5c0f\u6765\u5b9a\u4f4d\u4e00\u4e2a\u7a97\u53e3\u3002<\/p>\n<p>package com.generationjava.awt;<\/p>\n<p>import java.awt.Dimension;<\/p>\n<p>import java.awt.Window;<\/p>\n<p>public final class WindowUtilities {<\/p>\n<p>\/\/ ints used so a case-switch statement can be used.<\/p>\n<p>static public final int TOP = 0;<\/p>\n<p>static public final int BOTTOM = 1;<\/p>\n<p>static public final int CENTER = 2;<\/p>\n<p>static public final int LEFT = 3;<\/p>\n<p>static public final int RIGHT = 4;<\/p>\n<p>\/**<\/p>\n<p>* Position the given window on the screen at the given location,<\/p>\n<p>*<\/p>\n<p style=\"text-indent: 2em;\">* @param w Window to position<\/p>\n<p>* @param x int horizontal position<\/p>\n<p>* @param y int vertical position<\/p>\n<p>*\/<\/p>\n<p>static public void positionWindowOnScreen(Window w, int x, int y) {<\/p>\n<p>Dimension scrn = w.getToolkit().getScreenSize();<\/p>\n<p>Dimension win = w.getSize();<\/p>\n<p>int nx = 0;<\/p>\n<p>int ny = 0;<\/p>\n<p>switch(x) {<\/p>\n<p>case LEFT: nx = 0; break;<\/p>\n<p>case CENTRE: nx = (int)((scrn.width-win.width)\/2); break;<\/p>\n<p>case RIGHT: nx = (int)(scrn.width-win.width); break;<\/p>\n<p>}<\/p>\n<p>switch(y) {<\/p>\n<p>case TOP: ny = 0; break;<\/p>\n<p>case CENTRE: ny = (int)((scrn.height-win.height)\/2); break;<\/p>\n<p>case BOTTOM: ny = (int)(scrn.height-win.height); break;<\/p>\n<p>}<\/p>\n<p>w.setLocation(nx,ny);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>\u8981\u786e\u5b9a\u4e00\u4e2a\u7a97\u53e3\u7684\u53f3\u4e0b\u89d2\u7684\u4f4d\u7f6e\uff0c\u5c31\u8981\u6267\u884c\u4e0b\u9762\u7684\u4ee3\u7801\uff1a<\/p>\n<p>Window window = &#8230;.<\/p>\n<p>WindowUtilities.positionWindowOnScreen(window,_<\/p>\n<p>WindowUtilities.RIGHT, WindowUtilities.BOTTOM);<\/p>\n<p>\u8be5\u65b9\u6cd5\u8fd8\u53ef\u4ee5\u8fdb\u884c\u6269\u5c55\u3002\u4f60\u53ef\u4ee5\u589e\u52a0\u4e00\u4e2a\u7b80\u5355\u7684centerWindowOnScreen\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u8c03\u7528\u4e86positionWindowOnScreen\u51fd\u6570\u3002\u4f8b\u5982\uff1a<\/p>\n<p>static public final void centerWindowOnScreen(Window w)<\/p>\n<p>{<\/p>\n<p>positionWindowOnScreen(w, CENTER, CENTER);<\/p>\n<p>}<\/p>\n<p>\u8fd9\u4e24\u4e2a\u51fd\u6570\u7b80\u5316\u4e86\u5728AWT\u548cSwing\u4e2d\u6240\u505a\u7684\u5de5\u4f5c\uff0c\u4f7f\u5f00\u53d1\u4eba\u5458\u53ef\u4ee5\u5c06\u66f4\u591a\u7684\u65f6\u95f4\u7528\u5728\u5f00\u53d1\u7528\u6237\u754c\u9762\u4e0a\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; Java Abstract Window Toolkit\uff08AWT\uff09 &hellip;<\/p>\n<p class=\"read-more\"><a href=\"http:\/\/zerobox.org\/notes\/81.html\">\u7ee7\u7eed\u9605\u8bfb &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[96],"class_list":["post-81","post","type-post","status-publish","format-standard","hentry","tag-java"],"views":811,"_links":{"self":[{"href":"http:\/\/zerobox.org\/notes\/wp-json\/wp\/v2\/posts\/81","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/zerobox.org\/notes\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/zerobox.org\/notes\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/zerobox.org\/notes\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/zerobox.org\/notes\/wp-json\/wp\/v2\/comments?post=81"}],"version-history":[{"count":0,"href":"http:\/\/zerobox.org\/notes\/wp-json\/wp\/v2\/posts\/81\/revisions"}],"wp:attachment":[{"href":"http:\/\/zerobox.org\/notes\/wp-json\/wp\/v2\/media?parent=81"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/zerobox.org\/notes\/wp-json\/wp\/v2\/categories?post=81"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/zerobox.org\/notes\/wp-json\/wp\/v2\/tags?post=81"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}