The urlconnection of basic learning in JavaFX

Source: Internet
Author: User

A standard JavaFX file contains three parts: Main class, Control class, interface design (XML+CSS)

1,main.java

 PackageApplication; Importjavafx.application.Application;ImportJavafx.fxml.FXMLLoader;ImportJavafx.stage.Stage;Importjavafx.scene.Parent;ImportJavafx.scene.Scene; Public classMainextendsApplication {@Override Public voidStart (Stage primarystage) {Try{Parent root= Fxmlloader.load (GetClass (). GetResource ("Main.fxml")); Scene Scene=NewScene (root); Scene.getstylesheets (). Add (GetClass (). GetResource ("Application.css"). Toexternalform ());            Primarystage.setscene (Scene); Primarystage.settitle ("The key to the Kingdom");        Primarystage.show (); } Catch(Exception e) {e.printstacktrace (); }    }         Public Static voidMain (string[] args) {launch (args); }}

2,conntroller.java

 PackageApplication;ImportJava.io.BufferedInputStream;Importjava.io.IOException;ImportJava.io.InputStream;ImportJava.io.InputStreamReader;ImportJava.io.Reader;Importjava.net.MalformedURLException;ImportJava.net.URL;Importjava.net.URLConnection;ImportJava.util.ResourceBundle;Importjavafx.event.ActionEvent;ImportJavafx.fxml.FXML;Importjavafx.fxml.Initializable;ImportJavafx.scene.control.Button;ImportJavafx.scene.control.TextArea;ImportJavafx.scene.control.TextField; Public classControllerImplementsinitializable{@FXMLPrivateTextArea TextArea; @FXMLPrivateTextField TextField; @FXMLPrivatebutton button; @FXMLPrivate voidhandlebuttonaction (ActionEvent event) {Try{String Corpid= "TEST"; String Corpsecret= "TEST"; URL URL=NewURL ("https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=" +corpid+ "&corpsecret=" +Corpsecret); URLConnection UC=url.openconnection (); InputStream Raw=Uc.getinputstream (); InputStream Buffer=NewBufferedinputstream (RAW); Reader R=Newinputstreamreader (buffer); intC; StringBuffer Body=NewStringBuffer ();  while((c = R.read ())! = 1) {body.append (Char) c); } String Key= Body.substring (17, 81); String Xuehao=Textfield.gettext (); URL Url2=NewURL ("https://qyapi.weixin.qq.com/cgi-bin/user/get?access_token=" +key+ "&userid=" +Xuehao); URLConnection UC2=url2.openconnection (); InputStream in=Uc2.getinputstream (); Inch=NewBufferedinputstream (in); Reader R2=NewInputStreamReader (in); inti; StringBuffer Body2=NewStringBuffer ();  while((i = R2.read ())! = 1) {body2.append (Char) (i); } textarea.appendtext (Body2.tostring ()+ "\ n"); } Catch(Malformedurlexception ex) {}Catch(IOException ex) {}} @Override Public voidInitialize (URL arg0, ResourceBundle arg1) {//TODO auto-generated Method Stub            }        }

3,main.fxml

<?XML version= "1.0" encoding= "UTF-8"?><?Import java.lang.*?><?Import java.util.*?><?Import javafx.scene.*?><?Import javafx.scene.control.*?><?Import javafx.scene.layout.*?><AnchorpaneID= "Anchorpane"Prefheight= "420.0"Prefwidth= "378.0"xmlns= "HTTP://JAVAFX.COM/JAVAFX/8"Xmlns:fx= "HTTP://JAVAFX.COM/FXML/1"Fx:controller= "Application." Controller ">    <Children>        <ButtonFx:id= "button"LAYOUTX= "264.0"layouty= "20.0"onAction= "#handleButtonAction"text= "One-click Get" />      <TextFieldFx:id= "TextField"LAYOUTX= "97.0"layouty= "20.0"Prefheight= "23.0"Prefwidth= "157.0" />      <TextAreaFx:id= "textarea"LAYOUTX= "7.0"layouty= "51.0"Prefheight= "362.0"Prefwidth= "364.0"WrapText= "true" />      <LabelLAYOUTX= "23.0"layouty= "20.0"Prefheight= "23.0"Prefwidth= "91.0"text= "Please enter the study number:" />    </Children></Anchorpane>

4,application.css

The urlconnection of basic learning in JavaFX

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.