同步操作将从 yswift/LearnJava 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
package io.webapi;import java.awt.EventQueue;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.border.EmptyBorder;import com.google.gson.Gson;import io.HttpTools;import javax.imageio.ImageIO;import javax.swing.ImageIcon;import javax.swing.JButton;import javax.swing.JLabel;import java.awt.event.ActionListener;import java.awt.image.BufferedImage;import java.io.IOException;import java.net.URL;import java.awt.event.ActionEvent;public class DogAPI extends JFrame {private JPanel contentPane;private JLabel lblJson;private JLabel lblImage;/*** Launch the application.*/public static void main(String[] args) {EventQueue.invokeLater(new Runnable() {public void run() {try {DogAPI frame = new DogAPI();frame.setVisible(true);} catch (Exception e) {e.printStackTrace();}}});}/*** Create the frame.*/public DogAPI() {setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setBounds(100, 100, 658, 514);contentPane = new JPanel();contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));setContentPane(contentPane);contentPane.setLayout(null);JButton btnLoad = new JButton("Fetch");btnLoad.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {try {load();} catch (IOException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}}});btnLoad.setBounds(10, 10, 93, 23);contentPane.add(btnLoad);lblJson = new JLabel("New label");lblJson.setBounds(10, 40, 622, 39);contentPane.add(lblJson);lblImage = new JLabel("");lblImage.setBounds(10, 94, 603, 377);contentPane.add(lblImage);}public void load() throws IOException {// 获取,并显示jsonString url = "https://dog.ceo/api/breeds/image/random";String json = HttpTools.doGetString(url);// 解析出 image urlGson gson = new Gson();DogJson dog = gson.fromJson(json, DogJson.class);String imageUrl = dog.getMessage();lblJson.setText(imageUrl);// 获取图像BufferedImage image = ImageIO.read(new URL(imageUrl));// JLabel lbl = new JLabel(new ImageIcon(image));// contentPane.add(lbl);// 显示lblImage.setIcon(new ImageIcon(image));}}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。