EXT是里面的例子是用PHP,但在我們工程中,大多數是用Java的,在這篇博客里面,我主要要講一些用Java來開發EXT程序的例子,而且我們爭取是用Java轉化為JS的形式來做EXT,這樣的話,我們可以更加方便的去調試,因為Java的調試要比Javascript的調試容易得多。
?
這里,我們要引用一個JSON的框架 - SimpleJSON,SimpleJSON的框架下載方式與使用方法,請參見http://tntxia.iteye.com/blog/755752。
?
首先,我們模仿Swing做一個Component的組件。
?
package com.tntxia.extjava.tag;
public interface Component {
public String draw();
}
?
這個類是一個簡單的接口類,只包含了一個draw方法。用來讓所有的組件都繼承于它。
?
接下來,我們定義更有意義的東西。一個Button組件。
?
package com.tntxia.extjava.tag;
import org.json.simple.JSONObject;
public class Button implements Component {
private String id;
private String text;
private boolean pressed;
private int width;
private int height;
private String renderTo;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public boolean isPressed() {
return pressed;
}
public void setPressed(boolean pressed) {
this.pressed = pressed;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public String getRenderTo() {
return renderTo;
}
public void setRenderTo(String renderTo) {
this.renderTo = renderTo;
}
public String draw() {
JSONObject param = new JSONObject();
if(text!=null)
param.put("text", text);
param.put("pressed", Boolean.valueOf(true));
if(height!=0)
param.put("height", Integer.valueOf(height));
if(renderTo!=null)
param.put("renderTo", renderTo);
return "var "+id+" = new Ext.Button("+param+");";
}
}
?
這里我們實現了Component的draw方法,讓Button可以在頁面上顯示出來。
?
我們最終的目的,是可以在頁面上看到EXT的顯示結果,所以我們接下來就寫一個JSP頁面來實現顯示。
?
?
<%@ page language="java" contentType="text/html; charset=GBK"
pageEncoding="GBK"%>
<%@page import="com.tntxia.extjava.tag.Button"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>這是用Java實現的一個EXT按鈕</title>
<link rel="stylesheet" type="text/css" href="../ext/resources/css/ext-all.css" />
<link rel="stylesheet" type="text/css" href="../ext/resources/css/xtheme-access.css" />
<script type="text/javascript" src="../ext/adapter/ext/ext-base.js"></script>
<script type="text/javascript" src="../ext/ext-all.js"></script>
<script type="text/javascript">
function hello(){
alert("hello");
}
Ext.onReady(function(){
<%
Button button = new Button();
button.setId("button1");
button.setText("按鈕2");
button.setRenderTo("button");
out.println(button.draw());
%>
});
</script>
</head>
<body>
<div id="button"></div>
</body>
</html>
?
最終實現的效果比下:
?
?
?
?
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

