码迷,mamicode.com
首页 > 编程语言 > 详细

JavaFX基础学习之URLConnection

时间:2016-12-24 11:38:47      阅读:317      评论:0      收藏:0      [点我收藏+]

标签:text   prim   oid   out   bsp   load   print   zab   control   

一个标准的JavaFX文件包含三个部分:主类 、 控制类、 界面设计(XML+CSS)

 

 

1,main.java

package application;
    
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;



public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {
            Parent root = FXMLLoader.load(getClass().getResource("main.fxml"));
            Scene scene = new Scene(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 void main(String[] args) {
        launch(args);
    }
}

 

 

 

2,Conntroller.java

package application;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ResourceBundle;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;

public class Controller implements Initializable{
    
    @FXML
    private TextArea textarea;
    @FXML
    private TextField textfield;
    @FXML
    private Button button;
    
    
    @FXML
    private void handleButtonAction(ActionEvent event) {
        try {
            String corpid = "TEST";
            String corpsecret = "TEST";
            URL url = new URL("https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid="+corpid+"&corpsecret="+corpsecret);
            URLConnection uc = url.openConnection();
            InputStream raw = uc.getInputStream();
            InputStream buffer = new BufferedInputStream(raw);
            Reader r = new InputStreamReader(buffer);
            int c;
            StringBuffer body = new StringBuffer();
            while ((c = r.read()) != -1){
           
            body.append((char)c);
           
            }
            String key = body.substring(17, 81);
            
            
            String xuehao = textfield.getText();                    
            URL url2 = new URL("https://qyapi.weixin.qq.com/cgi-bin/user/get?access_token="+key+"&userid="+xuehao);
            URLConnection uc2 = url2.openConnection();
            InputStream in = uc2.getInputStream();
            in = new BufferedInputStream(in);
            Reader r2 = new InputStreamReader(in);
            int i;
            StringBuffer body2 = new StringBuffer();
            while ((i = r2.read()) != -1){
                body2.append((char)i);
            }
            
            textarea.appendText(body2.toString()+"\n");
            
            
        } catch (MalformedURLException ex) {
            
        } catch (IOException ex) {
            
        }
        
        

    }
    
    
    
    
    
    

    @Override
    public void initialize(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.*?>

<AnchorPane id="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>
        <Button fx:id="button" layoutX="264.0" layoutY="20.0" onAction="#handleButtonAction" text="一键获取" />
      <TextField fx:id="textfield" layoutX="97.0" layoutY="20.0" prefHeight="23.0" prefWidth="157.0" />
      <TextArea fx:id="textarea" layoutX="7.0" layoutY="51.0" prefHeight="362.0" prefWidth="364.0" wrapText="true" />
      <Label layoutX="23.0" layoutY="20.0" prefHeight="23.0" prefWidth="91.0" text="请输入学号:" />
    </children>
</AnchorPane>

 

 

 

4,application.css

 

JavaFX基础学习之URLConnection

标签:text   prim   oid   out   bsp   load   print   zab   control   

原文地址:http://www.cnblogs.com/lzhd24/p/6216671.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!