您当前的位置: 首页 > 学无止境 > 心得笔记 网站首页心得笔记
javaswing动态更新ui布局
发布时间:2017-09-23 10:44:47编辑:雪饮阅读()
用到远程地址json解析详见javase请求并解析后端返回的json数据
用到远程网络url文件下载详见javase实现网络url文件下载(解决小文件下载问题)
package test;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JFrame;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import ui.HttpRequestUtil;
import bean.JsonBean;
public class test extends JFrame {
private static final long serialVersionUID = 1L;
private static JPanel jContentPane = null;
private static JFrame frame;
private static JButton jButton;
public test() {super();}
public static boolean download(String url,String filename){
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse;
try {
httpResponse = httpClient.execute(httpGet);
StatusLine statusLine = httpResponse.getStatusLine();
if (statusLine.getStatusCode() == 200) {
File xml = new File("c:/sitemap/"+filename);
FileOutputStream outputStream = new FileOutputStream(xml);
InputStream inputStream = httpResponse.getEntity().getContent();
byte buff[] = new byte[4096];
int counts = 0;
while ((counts = inputStream.read(buff)) != -1) {
System.out.println(".......");
outputStream.write(buff, 0, counts);
}
outputStream.flush();
outputStream.close();
}
}
catch (ClientProtocolException e) {return false;}
catch (IOException e) {return false;}
httpClient.getConnectionManager().shutdown();
return true;
}
public static void main(String[] args) {
initialize();
//界面初始化绘制完毕后开启子线程请求数据
JsonBean jsonData=HttpRequestUtil.httpRequest("http://localhost/", "POST", "");
//默认图片
String localImg="C:/mz.png";
for(JsonBean.DataBean.ListBean list: jsonData.getData().getListBean()){
JLabel jlabel=new JLabel();
jlabel.setText(list.getTitle());
jlabel.setPreferredSize(new Dimension(120,113));
String thumb=list.getThumb();
String timestr=list.getImputti()+".jpg";
//如果文件网址为空则不下载,直接加载本地图片
if(thumb==null||thumb.isEmpty()){jlabel.setIcon(new ImageIcon(localImg));}
else{
if(download(thumb,timestr)){jlabel.setIcon(new ImageIcon("c:/sitemap/"+timestr));}
}
jContentPane.add(jlabel);
//这个最重要重新触发ui
frame.setVisible(true);
}
}
private static void initialize() {
frame = new JFrame("LabelDemo");
frame.setExtendedState(MAXIMIZED_BOTH);
frame.setContentPane(getJContentPane());
frame.setTitle("JFrame");
frame.setVisible(true);
}
private static JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setBackground(Color.blue);
jContentPane.setLayout(new FlowLayout());
JButton jpanel=getJButton();
jContentPane.add(jpanel);
}
return jContentPane;
}
private static JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setText("测试");
jButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
Toolkit.getDefaultToolkit().beep();
JOptionPane.showMessageDialog(null, "INFORMATION_MESSAGE", "INFORMATION_MESSAGE",JOptionPane.INFORMATION_MESSAGE);
}
});
}
return jButton;
}
}
关键字词:java,swing,更新