
EncryptorGUI 類別完整的範例程式碼,請參考EncryptorGUI.java
簡單說,我們在 EncryptorGUI 中建立一個 Encrypt 型態的屬性 (field) 即可
15 | private Encrypt e; |
因此必須先 import encryptor 套件 (package)
6 | import encryptor.Encrypt; |
我們在建構子 (constructor) 中,先將 e 設定為 null
61 | e = null ; // null 表示指向虛無的參考 |
null 是 Java 中特別的字面常數 (literal) ,表示什麼都沒有的參考 (reference) ,這裡是用 null 當 e 的初值。若一個參考變數 (reference variable) 被指向 null ,原先的實體物件 (instance) 若無其他參考變數連結,那麼這個實體物件就會被自動資源回收。
實際 e 的建立是用 New 按鈕 (button) ,採用 NewListener 的 inner 類別 (inner class) 處理相關事件 (event)
159 160 161 162 163 164 165 166 167 | // 建立新 Encrypt 物件的事件 class NewListener implements ActionListener { public void actionPerformed(ActionEvent event) { e = new Encrypt(); JLabel t = (JLabel) GUIComponent.get( 2 ); t.setText( "This is New button. A new Encrypt object is built." ); } } |
編碼為 Encode 按鈕,,採用 EncodeListener 處理相關事件
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 | // 進行編碼的事件 class EncodeListener implements ActionListener { public void actionPerformed(ActionEvent event) { JLabel t1 = (JLabel) GUIComponent.get( 2 ); JTextField t2 = (JTextField) GUIComponent.get( 4 ); if (userinput == "" ) { t1.setText( "This is Encode button. No input string!!" ); } else { if (e == null ) { t1.setText( "This is Encode button. No Encrypt object!!" ); } else { result = e.toEncode(userinput); t2.setText(result); t1.setText( "This is Encode button. The result is above." ); } } } } |
注意這裡,我們用巢狀的 if-else 來檢查使用者是否有輸入文字,若使用者沒有輸入文字,進行編碼就沒有意義。然後再檢查 e 是否已經被建立,同樣的,若無 e 就無法進行編碼。若使用者有輸入文字, userinput 就不會是空字串,然後使用者有新建 Encrypt 的 e 物件,這樣才會開始進行編碼
231 | result = e.toEncode(userinput); |
編碼就是呼叫 e 的 toEncode() 方法 (method) ,得到的結果給 result ,然後將 result 交給 Output 標籤 (label) 後的的文字方塊 (textfield) 顯示編碼結果。 result 也是我們新增的屬性,記得,所有新增的東西都要先宣告
17 | private String result; |
任何工作完成,我們都在 hint 標籤提供相對應的提示訊息。
編碼為 Decode 按鈕,,採用 DecodeListener 處理相關事件
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 | // 進行解碼的事件 class DecodeListener implements ActionListener { public void actionPerformed(ActionEvent event) { JLabel t1 = (JLabel) GUIComponent.get( 2 ); JTextField t2 = (JTextField) GUIComponent.get( 4 ); if (userinput == "" ) { t1.setText( "This is Decode button. No input string!!" ); } else { if (e == null ) { t1.setText( "This is Decode button. No Encrypt object!!" ); } else { result = e.toDecode(userinput); t2.setText(result); t1.setText( "This is Decode button. The result is above." ); } } } } |
整體上 DecodeListener 與 EncodeListener 相當類似,唯一的差別在
254 | result = e.toDecode(userinput); |
DecodeListener 這裡是呼叫 e 的 toDecode() , EncodeListener 的 e 是呼叫 toEncode() 。
記得要先重新編譯 EncryptorGUI.java ,然後呼叫執行 EncryptorGUIDemo.class 。好了,我們來玩玩看吧!
輸入英文句子 "There is no spoon." ,這會被 userinput 儲存

先按 Encode 按鈕,的確,還沒建立 Encrypt 物件

按 New 按鈕,建立 Encrypt 物件

再按 Encode 按鈕,此時順利完成編碼

重新按一次 New 按鈕

然後按 Encode 按鈕,得到新的編碼結果

接下來,我們繼續加入儲存 Encrypt 物件到檔案的功能,同時要來看看重要的例外處理哩!
中英文術語對照 | |
---|---|
屬性 | field |
套件 | package |
建構子 | constructor |
字面常數 | literal |
參考 | reference |
參考變數 | reference variable |
實體物件 | instance |
按鈕 | button |
inner 類別 | inner class |
事件 | event |
方法 | method |
標籤 | label |
文字方塊 | textfield |
您可以繼續參考
GUI 篇
相關目錄
回 Java 入門指南
回 Java 教材目錄
回首頁
參考資料
The JavaTM Tutorials: Getting Started
The JavaTM Tutorials: Learning the Java Language
The JavaTM Tutorials: Essential Classes
The Java Language Specification, Third Edition
本文於 2013 年 1 月訂正
1 則留言:
Hi 請問您的toEncode() 是定義在哪裡呢? thanks!!
張貼留言