發發牢騷
IE7訪問我的部落格頁面布局異常!Firefox2.0添加新隨筆JS腳本異常!!
|/__
換了風格
posted @ 2006-11-23 16:49 leon 閱讀(400) | 評論 (0) | 編輯 收藏
像寫情書一樣coding
posted @ 2006-11-23 16:49 leon 閱讀(400) | 評論 (0) | 編輯 收藏
我們在做GUI編程的時候經常需要用到JFileChooser組件構造一個文件選取對話框來為用戶提供打開文件、保存文件等操作。
通常的做法是調用JFileChooser.showXXX()方法顯示文件選取對話框并且選擇一個文件后,點擊Approve 按鈕(默認情況下標有 "Open" 或 "Save"),當對話框關閉后使用JFileChooser.getSelectedFile()方法得到選取的文件(或使用JFileChooser.getSelectedFiles()取得選取的文件數組),然后再對被選取的文件有效性進行驗證(例如,文件的文件名是否合法、選取的路徑下是否已有同名文件存在等等),如果驗證不通過,需要再次打開文件選擇對話框進行選取。
顯然,驗證沒有通過的情況下,文件選取對話框被反復的打開和關閉,影響用戶體驗。
我現在介紹一個方法,在點擊了文件選取對話框上的Approve 按鈕后,文件選取對話框關閉前對選取的文件進行驗證,如果驗證沒有通過,那么對話框不關閉,直接進行下一次選擇。
查看JFileChooser的API,可以發現這樣一個方法 public void approveSelection() ,這個方法會在用戶單擊 Approve 按鈕時由 UI 調用此方法。導致使用等于 APPROVE_SELECTION
的命令字符串激發一個操作事件。
那么,我們現在可以選擇繼承JFileChooser再覆寫這個方法,將對選中文件的有效性驗證寫入這個方法中,只有當驗證通過時才調用超類的approveSelection() 完成文件選取,否則直接返回,繼續選擇新的文件。
下面是我寫的一個demo以供參考:
import java.io.File;
import javax.swing.JOptionPane;
/**
?* 在 JFileChooser 中進行文件驗證的小技巧
?* @author Chen Wei
?* @email chenwei.mobi@gmail.com
?*/
public class JFileChooserDemo extends javax.swing.JFileChooser{
???
??? public void approveSelection(){
??????? File file = getSelectedFile();
???????
??????? // 驗證文件名是否合法
??????? if (!validateFileName(file.getName())) {
??????????? JOptionPane.showMessageDialog(getParent(), "文件名不能包含下列任何字符之一:\n \\ / : * ? \" < > |");
??????????? return;
??????? }else{
??????????? super.approveSelection();
??????? }
??? }
???
??? /**
???? * 驗證輸入字符串參數是否為有效文件名。
???? * @param name 待驗證的文件名字符串。
???? * @return 通過驗證,文件名無效返回 false,有效返回 true。
???? */
??? public static boolean validateFileName(String name) {
??????? if (name.indexOf('\\') != -1 || name.indexOf('/') != -1 ||
??????????? name.indexOf(':') != -1 || name.indexOf('*') != -1 ||
??????????? name.indexOf('?') != -1 || name.indexOf('"') != -1 ||
??????????? name.indexOf('<') != -1 || name.indexOf('>') != -1 ||
??????????? name.indexOf('|') != -1) {
??????????? return false;
??????? } else {
??????????? return true;
??????? }
??? }
???
??? public static void main(String[] args){
??????? JFileChooserDemo chooser = new JFileChooserDemo();
??????? chooser.showOpenDialog(null);
??? }
}
程序運行截圖:
posted @ 2006-11-23 14:15 leon 閱讀(5397) | 評論 (7) | 編輯 收藏
posted @ 2006-10-30 17:16 leon 閱讀(537) | 評論 (0) | 編輯 收藏
這兩天操作XML使用到了Jdom,在創建XML文件并輸出到硬盤的時候遇到一個中文編碼的問題:Jdom默認輸出的XML編碼是UTF-8,但是文檔中如果出現中文字符那么該中文字符就會變成亂碼,造成XML文件無法被正確解析。
UTF-8應該是可以用來表示中文的吧?我不知道這是不是Jdom的一個BUG(Jdom 1.0,beta了10次的產物哦!)。我google了一下,大家解決這個問題的辦法無非是把Jdom的輸出字符集改為GBK或者GB2312,但是這樣就會有一些副作用,如果在沒有特定字符集(GBK或者GB2312)的操作系統上不是依然不能正確解析嗎?一個比較好的解決辦法是先把中文轉換成Unicode編碼在直接輸出,程序解析XML后的時候再把Unicode編碼轉回中文就沒有問題了。
于是我查看了JDK的文檔,截至Java 5好像都沒有做類似轉換的類可以直接使用,但是我發現一個類 java.util.Properties,它的源代碼里有兩個私有(private)方法 loadConvert (char[] in, int off, int len, char[] convtBuf) 和 saveConvert(String theString, boolean escapeSpace) 其實就是做特殊字符和Unicode編碼字符間轉換的,我把它們提取出來,單獨包裝到一個類里就可以使用了。
下面是我包裝的類 CharacterSetToolkit
/*
?* CharacterSetToolkit.java
?*
?* Created on 2006年10月27日, 下午2:06
?*
?* To change this template, choose Tools | Template Manager
?* and open the template in the editor.
?*/
package mobi.chenwei.lang;
/**
?* 進行字符操作的工具類
?* @author Chen Wei
?* @email chenwei.mobi@gmail.com
?*/
public class CharacterSetToolkit {
???
??? /** Creates a new instance of CharacterSetToolkit */
??? public CharacterSetToolkit() {
??? }
???
??? private static final char[] hexDigit = {
??????? '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'
??? };
???
??? private static char toHex(int nibble) {
??????? return hexDigit[(nibble & 0xF)];
??? }
???
??? /**
???? * 將字符串編碼成 Unicode 。
???? * @param theString 待轉換成Unicode編碼的字符串。
???? * @param escapeSpace 是否忽略空格。
???? * @return 返回轉換后Unicode編碼的字符串。
???? */
??? public static String toUnicode(String theString, boolean escapeSpace) {
??????? int len = theString.length();
??????? int bufLen = len * 2;
??????? if (bufLen < 0) {
??????????? bufLen = Integer.MAX_VALUE;
??????? }
??????? StringBuffer outBuffer = new StringBuffer(bufLen);
??????? for(int x=0; x<len; x++) {
??????????? char aChar = theString.charAt(x);
??????????? // Handle common case first, selecting largest block that
??????????? // avoids the specials below
??????????? if ((aChar > 61) && (aChar < 127)) {
??????????????? if (aChar == '\\') {
??????????????????? outBuffer.append('\\'); outBuffer.append('\\');
??????????????????? continue;
??????????????? }
??????????????? outBuffer.append(aChar);
??????????????? continue;
??????????? }
??????????? switch(aChar) {
??????????????? case ' ':
??????????????????? if (x == 0 || escapeSpace)
??????????????????????? outBuffer.append('\\');
??????????????????? outBuffer.append(' ');
??????????????????? break;
??????????????? case '\t':outBuffer.append('\\'); outBuffer.append('t');
????????????????????????? break;
??????????????? case '\n':outBuffer.append('\\'); outBuffer.append('n');
????????????????????????? break;
??????????????? case '\r':outBuffer.append('\\'); outBuffer.append('r');
????????????????????????? break;
??????????????? case '\f':outBuffer.append('\\'); outBuffer.append('f');
????????????????????????? break;
??????????????? case '=': // Fall through
??????????????? case ':': // Fall through
??????????????? case '#': // Fall through
??????????????? case '!':
??????????????????? outBuffer.append('\\'); outBuffer.append(aChar);
??????????????????? break;
??????????????? default:
??????????????????? if ((aChar < 0x0020) || (aChar > 0x007e)) {
??????????????????????? outBuffer.append('\\');
??????????????????????? outBuffer.append('u');
??????????????????????? outBuffer.append(toHex((aChar >> 12) & 0xF));
??????????????????????? outBuffer.append(toHex((aChar >>? 8) & 0xF));
??????????????????????? outBuffer.append(toHex((aChar >>? 4) & 0xF));
??????????????????????? outBuffer.append(toHex( aChar??????? & 0xF));
??????????????????? } else {
??????????????????????? outBuffer.append(aChar);
??????????????????? }
??????????? }
??????? }
??????? return outBuffer.toString();
??? }
???
??? /**
???? * 從 Unicode 碼轉換成編碼前的特殊字符串。
???? * @param in Unicode編碼的字符數組。
???? * @param off 轉換的起始偏移量。
???? * @param len 轉換的字符長度。
???? * @param convtBuf 轉換的緩存字符數組。
???? * @return 完成轉換,返回編碼前的特殊字符串。
???? */
??? public String fromUnicode(char[] in, int off, int len, char[] convtBuf) {
??????? if (convtBuf.length < len) {
??????????? int newLen = len * 2;
??????????? if (newLen < 0) {
??????????????? newLen = Integer.MAX_VALUE;
??????????? }
??????????? convtBuf = new char[newLen];
??????? }
??????? char aChar;
??????? char[] out = convtBuf;
??????? int outLen = 0;
??????? int end = off + len;
??????? while (off < end) {
??????????? aChar = in[off++];
??????????? if (aChar == '\\') {
??????????????? aChar = in[off++];
??????????????? if (aChar == 'u') {
??????????????????? // Read the xxxx
??????????????????? int value = 0;
??????????????????? for (int i = 0; i < 4; i++) {
??????????????????????? aChar = in[off++];
??????????????????????? switch (aChar) {
??????????????????????? case '0':
??????????????????????? case '1':
??????????????????????? case '2':
??????????????????????? case '3':
??????????????????????? case '4':
??????????????????????? case '5':
??????????????????????? case '6':
??????????????????????? case '7':
??????????????????????? case '8':
??????????????????????? case '9':
??????????????????????????? value = (value << 4) + aChar - '0';
??????????????????????????? break;
??????????????????????? case 'a':
??????????????????????? case 'b':
??????????????????????? case 'c':
??????????????????????? case 'd':
??????????????????????? case 'e':
??????????????????????? case 'f':
??????????????????????????? value = (value << 4) + 10 + aChar - 'a';
??????????????????????????? break;
??????????????????????? case 'A':
??????????????????????? case 'B':
??????????????????????? case 'C':
??????????????????????? case 'D':
??????????????????????? case 'E':
??????????????????????? case 'F':
??????????????????????????? value = (value << 4) + 10 + aChar - 'A';
??????????????????????????? break;
??????????????????????? default:
??????????????????????????? throw new IllegalArgumentException(
??????????????????????????????????? "Malformed \\uxxxx encoding.");
??????????????????????? }
??????????????????? }
??????????????????? out[outLen++] = (char) value;
??????????????? } else {
??????????????????? if (aChar == 't') {
??????????????????????? aChar = '\t';
??????????????????? } else if (aChar == 'r') {
??????????????????????? aChar = '\r';
??????????????????? } else if (aChar == 'n') {
??????????????????????? aChar = '\n';
??????????????????? } else if (aChar == 'f') {
??????????????????????? aChar = '\f';
??????????????????? }
??????????????????? out[outLen++] = aChar;
??????????????? }
??????????? } else {
??????????????? out[outLen++] = (char) aChar;
??????????? }
??????? }
??????? return new String(out, 0, outLen);
??? }
}
posted @ 2006-10-28 20:53 leon 閱讀(10684) | 評論 (3) | 編輯 收藏
9月26日(下周二),city8(www.city8.com)將進行一次升級。
更新內容:
1、上海的中環、外環以內的數據添加,這樣一來整個上海的數據就全了,大家想去哪,就可以看到哪了。
?2、三維實景窗口變大,看高樓不那么累了:)
3、操作按鈕重新設計,這樣大家可以方便實現在街道中行走、全屏瀏覽等很酷的體驗。
4、實景地圖分享:可以方便在地圖添加文字表述,如“我的家”,并通過msn,qq,blog,論壇等方式與他人分享
界面先睹為快:
posted @ 2006-09-23 14:25 leon 閱讀(472) | 評論 (2) | 編輯 收藏
posted @ 2006-09-19 12:02 leon 閱讀(18197) | 評論 (2) | 編輯 收藏
posted @ 2006-08-27 18:41 leon 閱讀(1217) | 評論 (1) | 編輯 收藏
posted @ 2006-08-27 14:33 leon 閱讀(1619) | 評論 (0) | 編輯 收藏
使用這個方法前需要先將圖像文件從磁盤上讀到一個 java.awt.image.BufferedImage 對象中,我們可以用 J2SE 包含的 ImageIO 庫。
posted @ 2006-08-16 10:55 leon 閱讀(2958) | 評論 (0) | 編輯 收藏
posted @ 2006-07-03 12:06 leon 閱讀(21330) | 評論 (69) | 編輯 收藏
posted @ 2006-06-21 11:07 leon 閱讀(2083) | 評論 (9) | 編輯 收藏
posted @ 2006-06-13 10:28 leon 閱讀(462) | 評論 (0) | 編輯 收藏
Java.lang.Math的round()方法返回的是整型,如果要保留小數位的話可以先乘以(小數位數 *? 10),使用Java.lang.Math的round()方法計算之后再除以(小數位數 *? 10)。
posted @ 2006-06-07 11:50 leon 閱讀(503) | 評論 (1) | 編輯 收藏
posted @ 2006-06-03 20:45 leon 閱讀(314) | 評論 (0) | 編輯 收藏
posted @ 2006-06-03 14:51 leon 閱讀(729) | 評論 (0) | 編輯 收藏
posted @ 2005-11-29 17:04 leon 閱讀(2587) | 評論 (5) | 編輯 收藏
posted @ 2005-11-02 23:08 leon 閱讀(3859) | 評論 (0) | 編輯 收藏
posted @ 2005-10-26 11:39 leon 閱讀(1263) | 評論 (0) | 編輯 收藏
我們都知道,圖像對象可以編碼成指定圖像格式文件保存在硬盤上,需要時再對其進行解碼讀入內存。但是除了這樣還有別的辦法可以將圖像對象保存在硬盤上嗎?熟悉Java I/O 的人也許可以想到采用對象序列化(Object serialization)試一試,很好,但是如果你研究了 BufferedImage?類的結構后就會大失所望(至少當時我是這樣)。
BufferedImage?提供一般圖像管理。BufferedImage 對象包括另外兩個對象:Raster 和 ColorModel。Raster 對象包含另外兩個對象:DataBuffer 和 SampleModel。不幸的是,他們都沒有實現序列化所必需的 Serializable 接口,所以無法直接對他們進行對象序列化。
我在學習 JAI 的時候發現了 javax.media.jai.remote 包里有一個類 SerializableRenderedImage,這個類實現了RenderedImage, Serializable 接口,可以將 RanderedImage 對象作為構造函數的參數實例化一個可以序列化的圖像對象。
SerializableRenderedImage(RenderedImage?source) ??????????Constructs a SerializableRenderedImage wrapper for a RenderedImage source. |
SerializableRenderedImage(RenderedImage?source, boolean?useDeepCopy) ??????????Constructs a SerializableRenderedImage wrapper for a RenderedImage source. |
SerializableRenderedImage(RenderedImage?source, boolean?useDeepCopy, OperationRegistry?registry, String?formatName, TileCodecParameterList?encodingParam, TileCodecParameterList?decodingParam) ??????????Constructs a SerializableRenderedImage wrapper for a RenderedImage source. |
posted @ 2005-10-18 17:36 leon 閱讀(2834) | 評論 (1) | 編輯 收藏
AWT 使用 ImageProducer / ImagConsumer 模式,支持加載和顯示 GIF 圖像文件格式和 JPEG 圖像文件格式。因為圖像的加載和顯示是異步方式進行的,所以有大量加載和顯示的技術。
在 AWT 中,提供了一個 java.awt.Image 類。java.awt.Image 類代表一個圖像對象被作為參數傳遞給其他用來顯示和處理圖像的其他 AWT 對象使用。例如,通過調用 Graphics.drawImage(java.awt.Image, int, int, ImageObserver) 方法,可以在組件中畫出圖像。
java.awt.Image 是一個定義方法的抽象類,它定義的方法提供的對圖像信息的訪問。而創建和處理圖像的基本結構則在 java.awt.image 包中。注意,這里不要和 java.awt.Image 發生混淆。
AWT? 加載和顯示圖像使用的是 ImageProducer / ImagConsumer 模式,我們必須了解3個術語,ImageProducer(圖像生產者),ImageConsumer(圖像消費者)和ImageObserver(圖像觀察者)。
ImageProducer 負責生產圖像的位,ImagConsumer 接受圖像的位,ImageObserver 監視 ImageProducer 的圖像生產過程。ImageProducer 生產傳遞給 ImagConsumer 與圖像相關的位。因為圖像生產過程是異步進行的,并不是一次性生產所有圖像位,所以當 ImageProducer 加載圖像時,ImageObserver 用來監視它的進展情況。因為 java.awt.Component 實現了 ImageObserver 接口,所以 AWT 中的每個組件都可以是ImageObserver,當一個給定的 ImageProducer 進行異步操作時,這個 ImageObserver 可以選擇是否被更新。java.awt.image 包為 ImageProducer,ImagConsumer 和 ImageObserver 都定義了接口。
ImageProducer
和圖像相關的位并不存儲在 java.awt.Image 中,每個圖像都維護一個和一個 ImageProducer?的關聯。這個 ImageProducer?的責任是生產圖像的位并將它們傳送給 ImagConsumer,用于過濾該圖像。
java.awt.image軟件包中,FilteredImageSource(被過濾的圖像源)和 MemoryImageSource(內存的圖像源)實現了 ImageProducer? 接口,是 ImageProducer?。
ImagConsumer
java.awt.image軟件包中,ImageFilter(圖像過濾器)和 PixelGrabber(像素抓取器)實現了 ImagConsumer 接口,是 ImagConsumer。
ImageProducer?和 ImagConsumer 的詳細介紹請閱讀 使用 ImageProducer? / ImagConsumer 進行圖像過濾
ImageObserver
ImageObserver接口中,定義了一個常數集合和一個方法:
public boolean imageUpdate(image img, int flags, int x, int y, int width, int height);
ImageObserver的常數 | |
標志 | 含義 |
ABORT | 圖像加載被中斷 |
ALLBITS | 所有的位都已加載給圖像 |
ERROR | 在加載過程中發生錯誤 |
FRAMEBITS | 多幀圖像的一個幀被傳送,一般用于GIF |
HEIGHT | 圖像的高度已經可用 |
PROPERTIES | 圖像的屬性已經可用 |
SOMEBITS | 圖像的縮放變體的多個位已經可用 |
WIDTH | 圖像的寬度已經可用 |
輸出結果:
image width = -1 height = -1
圖像的高度和寬度只有在圖像被完全加載后才是有效的,輸出結果說明 java.awt.image 相關的圖像位在需要之前不被生產。
例2,圖像異步生產
輸出結果:
drawing?image...
false
drawing?image...
false
drawing?image...
false
drawing?image...
true
輸出結果說明組件作為 ImageObserver ,監視 ImageProducer 異步生產圖像,一旦有新的圖像位被生產時就重繪圖像,圖像完全加載后 drawImage() 方法返回 true。
例3,重載 ImageObserver 的 imageUpdate() 方法,在圖像完全加載前不調用 repaint()
例4,重載? Component.update() 方法,被調用時不清除背景圖像,直接調用 paint() 方法繪制圖像,消除閃爍
?
posted @ 2005-10-11 10:35 leon 閱讀(2622) | 評論 (3) | 編輯 收藏