{"id":471,"date":"2017-02-08T22:58:40","date_gmt":"2017-02-08T14:58:40","guid":{"rendered":"http:\/\/www.rainweb.site\/?p=471"},"modified":"2023-08-07T20:57:29","modified_gmt":"2023-08-07T12:57:29","slug":"java-article41","status":"publish","type":"post","link":"http:\/\/rain1024.com\/index.php\/2017\/02\/08\/java-article41\/","title":{"rendered":"Java\u4e2d\u4f7f\u7528\u7ec4\u4ef6\u7f16\u5199\u7a97\u53e3\u5b9e\u73b0\u7f51\u4e0a\u56fe\u7247\u7684\u663e\u793a"},"content":{"rendered":"<h2>\u8fd9\u7bc7\u6587\u7ae0\u4f7f\u7528Java\u7ec4\u4ef6\u663e\u793a\u7a97\u53e3\uff0c\u5728\u901a\u8fc7\u8f93\u5165\u7684\u56fe\u7247url\u5730\u5740\u5728\u7a97\u53e3\u4e2d\u663e\u793a\u51fa\u6765\uff0c\u53ef\u4f5c\u4e3a\u4e00\u4e2a\u7f51\u7edc\u56fe\u7247\u67e5\u770b\u5668\uff0c\u611f\u5174\u8da3\u7684\u53ef\u4ee5\u6253\u5305\u6210jar\u6216\u8005.exe\u6587\u4ef6\u65b9\u4fbf\u4f7f\u7528\uff0c\u8fd9\u91cc\u6211\u53ea\u6302\u51fa\u4e86\u6e90\u7801\uff0c\u6548\u679c\u5982\u4e0b\u56fe\u3002<\/h2>\n<p><img decoding=\"async\" src=\"http:\/\/blog-1252406596.costj.myqcloud.com\/blog\/java15.jpg\" alt=\"\" \/><\/p>\n<p><img decoding=\"async\" src=\"http:\/\/blog-1252406596.costj.myqcloud.com\/blog\/java16.jpg\" alt=\"\" \/><\/p>\n<pre><code class=\"java\">package com.rain.image;\nimport java.awt.*;\nimport java.awt.event.*;\nimport java.net.*;\nimport javax.swing.*;\n\/\/\u4ece\u7f51\u7edc\u53d6\u5f97\u56fe\u8c61\npublic class GetImageDemo extends JFrame{\n    JTextField jtfUrl;  \/\/\u8f93\u5165\u56fe\u50cf\u5730\u5740url\n    JButton jbGetImage;  \/\/\u53d6\u56fe\u50cf\u6309\u94ae\n    Image image; \/\/\u83b7\u53d6\u7684\u56fe\u50cf\n    Toolkit toolKit;  \/\/Toolkit\u5bf9\u8c61,\u7528\u4e8e\u83b7\u53d6\u56fe\u50cf\n    public GetImageDemo(){\n        super(\"\u7f51\u7edc\u56fe\u7247\u67e5\u770b\u5668\");  \/\/\u8c03\u7528\u7236\u7c7b\u6784\u9020\u51fd\u6570\n        Container container=getContentPane();   \/\/\u5f97\u5230\u5bb9\u5668\n        jtfUrl=new JTextField(18); \/\/\u5b9e\u4f8b\u5316\u5730\u5740\u8f93\u5165\u6846\n        jbGetImage=new JButton(\"\u8f93\u5165\u56fe\u7247\u7684URL\");  \/\/\u5b9e\u4f8b\u5316\u6309\u94ae\n        container.setLayout(new FlowLayout()); \/\/\u8bbe\u7f6e\u5e03\u5c40\u7ba1\u7406\u5668\n        container.add(jtfUrl);  \/\/\u589e\u52a0\u7ec4\u4ef6\u5230\u5bb9\u5668\u4e0a\n        container.add(jbGetImage);\n        toolKit=getToolkit(); \/\/\u5f97\u5230\u5de5\u5177\u5305\n        jbGetImage.addActionListener(new ActionListener(){  \/\/\u6309\u94ae\u4e8b\u4ef6\u5904\u7406\n            public void actionPerformed(ActionEvent ent){\n                try{\n                    String urlStr=jtfUrl.getText();    \/\/\u5f97\u5230\u56fe\u50cf\u7684URL\u5730\u5740\n                    URL url=new URL(urlStr);\n                    image=toolKit.getImage(url); \/\/\u83b7\u53d6\u56fe\u50cf\n                    repaint(); \/\/\u91cd\u7ed8\u5c4f\u5e55\n                }\n                catch(MalformedURLException ex){\n                    ex.printStackTrace(); \/\/\u8f93\u51fa\u51fa\u9519\u4fe1\u606f\n                }\n            }\n        });\n        setSize(1280,640);  \/\/\u8bbe\u7f6e\u7a97\u53e3\u5c3a\u5bf8\n        setVisible(true);  \/\/\u8bbe\u7f6e\u7a97\u53e3\u53ef\u89c6\n        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  \/\/\u5173\u95ed\u7a97\u53e3\u65f6\u9000\u51fa\u7a0b\u5e8f\n    }\n    public void paint(Graphics g){\n        super.paint(g);\n        if (image!=null){\n            g.drawImage(image,100,70,this); \/\/\u5728\u7ec4\u4ef6\u4e0a\u7ed8\u5236\u56fe\u50cf\n        }\n    }\n    public static void main(String[] args){\n        new GetImageDemo();\n    }\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd9\u7bc7\u6587\u7ae0\u4f7f\u7528Java\u7ec4\u4ef6\u663e\u793a\u7a97\u53e3\uff0c\u5728\u901a\u8fc7\u8f93\u5165\u7684\u56fe\u7247url\u5730\u5740\u5728\u7a97\u53e3\u4e2d\u663e\u793a\u51fa\u6765\uff0c\u53ef\u4f5c\u4e3a\u4e00\u4e2a\u7f51\u7edc\u56fe\u7247\u67e5\u770b\u5668\uff0c\u611f\u5174\u8da3\u2026 <span class=\"read-more\"><a href=\"http:\/\/rain1024.com\/index.php\/2017\/02\/08\/java-article41\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":472,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"aside","meta":{"footnotes":""},"categories":[31],"tags":[46],"class_list":["post-471","post","type-post","status-publish","format-aside","has-post-thumbnail","hentry","category-java","tag-java","post_format-post-format-aside"],"_links":{"self":[{"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/posts\/471","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/comments?post=471"}],"version-history":[{"count":1,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/posts\/471\/revisions"}],"predecessor-version":[{"id":1499,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/posts\/471\/revisions\/1499"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/media\/472"}],"wp:attachment":[{"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/media?parent=471"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/categories?post=471"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/tags?post=471"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}