Swing使用JEditorPane显示HTML内容

2015-01-17 18:19:38|?次阅读|上传:wustguangh【已有?条评论】发表评论

关键词:Java, Swing, 界面设计|来源:唯设编程网

5. 加载HTML内容

/**
 * 
 * @param type
 * @param string 
 * @return
 */
private String buildDataListStr(String type, String prefix){
	String strHtml = "";
	//1.判断不需要返回内容的情况
	if(!mapList.containsKey(type))
		return strHtml;
	
	List<AccountSnapItemData> itemList = mapList.get(type);
	if(itemList.size() < 1)
		return strHtml;
	//2.填充内容行
	int index = 0;
	//合计金额
	double sum = 0;
	//内容行
	String strContentLine = "";
	for (AccountSnapItemData itemData : itemList) {
		if(itemData.getSum() == 0)
			continue;
		
		strContentLine += "<tr>"
				+"<td class='col1'>"+(++index)+"</td>" 
				+ "<td class='col2'>"+itemData.getAccountTitle()+"</td>" 
				+ "<td class='col3'>"+itemData.getMember()+"</td>" 
				+ "<td class='col4'>"+itemData.getSum()+"</td>"
    		                +"</tr>";			
		
		sum += itemData.getSum();
	}
	if(strContentLine.isEmpty())
		return strHtml;
	//3.返回拼凑内容
	strHtml += "<h2>"+prefix+type+"(合计:"+DataFormater.getDecimalFormat().format(sum)+"元)"+"</h2>";		
	strHtml += "<table cellspacing='1'>";
	strHtml += "<tr>"
		  +"<th class='col1'>序号</th>"
    		  +"<th class='col2'>账户名称</th>"
    		  +"<th class='col3'>家庭成员</th>"
    		  +"<th class='col4'>余额</th>"
		  +"</tr>";		
	strHtml += strContentLine;
	strHtml += "</table>";
	
	return strHtml;
}
/**
 * 构建html字符串
 * @return
 */
private String generateHTML(String dateLabel) {	
	String strSnapTime = null;
	try {
		if(dateLabel.equals("最新")){
			this.loadCurrentItemList();
			strSnapTime = DataFormater.getDateTimeFormat().format(Calendar.getInstance().getTime());
		}
		else{
			this.loadHistoryItemList(dateLabel);
			if(mapList.size()<1)
				return "<h1>对应快照数据为空!</h1>";				
			
			for( String item : mapList.keySet()) {
				List<AccountSnapItemData> snapList = mapList.get(item);
				for (AccountSnapItemData snapItem : snapList) {
					strSnapTime = snapItem.getSnapTime();
					break;
				}
				if(strSnapTime != null)
					break;
			}				
		}
	} catch (NormalException e) {
		e.printStackTrace();
	}		
	String strHtml = "<h1>财务概况</h1>";
	strHtml += "<div>" ;		
	strHtml += buildDataListStr("储蓄账户","1.  ");
	strHtml += buildDataListStr("信用账户","2.  ");
	strHtml += buildDataListStr("理财账户","3.  ");		
	strHtml += buildDataListStr("社保账户","4.  ");
	strHtml += buildDataListStr("公积金","5.  ");
	strHtml += buildDataListStr("消费卡/券","6.  ");
	strHtml += "</div>";
	if(strSnapTime != null){
		strHtml += "<p style='text-align:right'>统计时间:" + strSnapTime
				+ "</p>";
	}
	if (dateLabel.equals("最新")) {
		strHtml += "<div class='tlb'><a class='btn' href='EVT_SAVE'>  保存快照  </a></div>";
	}
	return strHtml;
}
发表评论0条 】
网友评论(共?条评论)..
Swing使用JEditorPane显示HTML内容