
EncryptorGUI.java 的程式原始碼如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 | import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; import javax.swing.event.*; import encryptor.Encrypt; import java.io.*; public class EncryptorGUI { // 屬性區 private JFrame frame; private ArrayList<JComponent> GUIComponent; private String[] name; private int att[][]; private Encrypt e; private String userinput; private String result; // 設定屬性的建構子 public EncryptorGUI() { // fill 、 anchor 、 n 為常數 int fill[] = { GridBagConstraints.BOTH, GridBagConstraints.VERTICAL, GridBagConstraints.HORIZONTAL, GridBagConstraints.NONE}; int anchor[] = { GridBagConstraints.CENTER, GridBagConstraints.EAST, GridBagConstraints.SOUTHEAST, GridBagConstraints.SOUTH, GridBagConstraints.SOUTHWEST, GridBagConstraints.WEST, GridBagConstraints.NORTHWEST, GridBagConstraints.NORTH, GridBagConstraints.NORTHEAST}; int a[][] = {{ 0 , 0 , 1 , 1 , 0 , 0 , fill[ 3 ], anchor[ 5 ]}, { 0 , 1 , 1 , 1 , 0 , 0 , fill[ 3 ], anchor[ 5 ]}, { 0 , 3 , 7 , 1 , 0 , 0 , fill[ 3 ], anchor[ 5 ]}, { 1 , 0 , 6 , 1 , 0 , 0 , fill[ 0 ], anchor[ 5 ]}, { 1 , 1 , 6 , 1 , 0 , 0 , fill[ 0 ], anchor[ 5 ]}, { 0 , 2 , 1 , 1 , 0 , 0 , fill[ 0 ], anchor[ 0 ]}, { 1 , 2 , 1 , 1 , 0 , 0 , fill[ 0 ], anchor[ 0 ]}, { 2 , 2 , 1 , 1 , 0 , 0 , fill[ 0 ], anchor[ 0 ]}, { 3 , 2 , 1 , 1 , 0 , 0 , fill[ 0 ], anchor[ 0 ]}, { 4 , 2 , 1 , 1 , 0 , 0 , fill[ 0 ], anchor[ 0 ]}, { 5 , 2 , 1 , 1 , 0 , 0 , fill[ 0 ], anchor[ 0 ]}, { 6 , 2 , 1 , 1 , 0 , 0 , fill[ 0 ], anchor[ 0 ]}}; String n[] = { "Input" , "Output" , "hint..." , "New" , "Load" , "Save" , "Encode" , "Decode" , "Clear" , "Copy" }; // 以下用以上的常數設定所需屬性 name = n; att = a; e = null ; // null 表示指向虛無的參考 userinput = "" ; result = "" ; frame = new JFrame(); GUIComponent = new ArrayList<JComponent>( 12 ); } // run() 為建立視窗的函數 public void run() { frame.setSize( 600 , 160 ); frame.setLayout( new GridBagLayout()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 以下建立視窗元件 int i; for (i = 0 ; i < 3 ; i++) { JLabel nLabel = new JLabel(name[i]); GUIComponent.add(nLabel); } for (i = 0 ; i < 2 ; i++) { JTextField nText = new JTextField( "" , 32 ); GUIComponent.add(nText); } for (i = 3 ; i < 10 ; i++) { JButton nButton = new JButton(name[i]); GUIComponent.add(nButton); } for (i = 0 ; i < GUIComponent.size(); i++) { addComponent(i); } // 以下個視窗元件註冊所要處理的事件 JTextField t = (JTextField) GUIComponent.get( 3 ); t.addActionListener( new InputListener()); t.addCaretListener( new InputListener()); t = (JTextField) GUIComponent.get( 4 ); t.addActionListener( new OutputListener()); JButton b = (JButton) GUIComponent.get( 5 ); b.addActionListener( new NewListener()); b = (JButton) GUIComponent.get( 6 ); b.addActionListener( new LoadListener()); b = (JButton) GUIComponent.get( 7 ); b.addActionListener( new SaveListener()); b = (JButton) GUIComponent.get( 8 ); b.addActionListener( new EncodeListener()); b = (JButton) GUIComponent.get( 9 ); b.addActionListener( new DecodeListener()); b = (JButton) GUIComponent.get( 10 ); b.addActionListener( new ClearListener()); b = (JButton) GUIComponent.get( 11 ); b.addActionListener( new CopyListener()); frame.setVisible( true ); } // 設定 GridBagConstraints 的函數 private void addComponent( int i) { GridBagConstraints c = new GridBagConstraints(); int a[] = att[i]; c.gridx = a[ 0 ]; c.gridy = a[ 1 ]; c.gridwidth = a[ 2 ]; c.gridheight = a[ 3 ]; c.weightx = a[ 4 ]; c.weighty = a[ 5 ]; c.fill = a[ 6 ]; c.anchor = a[ 7 ]; frame.add(GUIComponent.get(i), c); } // 使用者輸入文字方塊的事件 class InputListener implements ActionListener, CaretListener { public void actionPerformed(ActionEvent event) { JTextField t2 = (JTextField) GUIComponent.get( 3 ); userinput = t2.getText(); JLabel t1 = (JLabel) GUIComponent.get( 2 ); t1.setText( "This is Input textfield. Your input is \'" + userinput + "\'" ); } public void caretUpdate(CaretEvent event) { JTextField t2 = (JTextField) GUIComponent.get( 3 ); userinput = t2.getText(); JLabel t1 = (JLabel) GUIComponent.get( 2 ); t1.setText( "This is Input textfield. Your input is \'" + userinput + "\'" ); } } // 輸出文字方塊的事件 class OutputListener implements ActionListener { public void actionPerformed(ActionEvent event) { JLabel t = (JLabel) GUIComponent.get( 2 ); t.setText( "This is Output textfield. Why do you do this?" ); } } // 建立新 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." ); } } // 載入已儲存 Encrypt 物件檔案的事件 class LoadListener implements ActionListener { public void actionPerformed(ActionEvent event) { JLabel t1 = (JLabel) GUIComponent.get( 2 ); JTextField t2 = (JTextField) GUIComponent.get( 4 ); try { FileInputStream fs = new FileInputStream( "encryptor.ser" ); ObjectInputStream is = new ObjectInputStream(fs); e = (Encrypt) is.readObject(); is.close(); fs.close(); t1.setText( "This is Load button. Encrypt object is loaded." ); } catch (Exception ex) { ex.printStackTrace(); t1.setText( "This is Load button. Encrypt object is not loaded." ); } finally { t2.setText( "after Load ....." ); } } } // 儲存 Encrypt 物件檔案的事件 class SaveListener implements ActionListener { public void actionPerformed(ActionEvent event) { JLabel t1 = (JLabel) GUIComponent.get( 2 ); JTextField t2 = (JTextField) GUIComponent.get( 4 ); try { FileOutputStream fs = new FileOutputStream( "encryptor.ser" ); ObjectOutputStream os = new ObjectOutputStream(fs); os.writeObject(e); os.close(); fs.close(); t1.setText( "This is Save button. Encrypt object is saved." ); } catch (Exception ex) { ex.printStackTrace(); t1.setText( "This is Save button. Encrypt object is not saved." ); } finally { t2.setText( "after Save ....." ); } } } // 進行編碼的事件 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." ); } } } } // 進行解碼的事件 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." ); } } } } // 清楚文字方塊及暫存資料的事件 class ClearListener implements ActionListener { public void actionPerformed(ActionEvent event) { userinput = "" ; result = "" ; JTextField t2 = (JTextField) GUIComponent.get( 3 ); t2.setText( "" ); JTextField t3 = (JTextField) GUIComponent.get( 4 ); t3.setText( "" ); JLabel t1 = (JLabel) GUIComponent.get( 2 ); t1.setText( "This is Clear button. Your input is clear." ); } } // 將輸出結果拷貝到作業系統的剪貼簿 class CopyListener implements ActionListener { public void actionPerformed(ActionEvent event) { JTextField t2 = (JTextField) GUIComponent.get( 4 ); t2.selectAll(); t2.copy(); JLabel t1 = (JLabel) GUIComponent.get( 2 ); t1.setText( "This is Copy button. Result is copied to clipboard." ); } } } /* 《程式語言教學誌》的範例程式 檔名:EncryptorGUI.java 功能:示範 Java 程式 作者:張凱慶 時間:西元 2011 年 4 月 */ |
您可以繼續參考
範例程式碼
相關目錄
回 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 月訂正
沒有留言:
張貼留言