The following looks:
 
 | 01 | importjava.awt.*; | 
| 02 | importjava.awt.event.*; | 
| 03 | importjavax.swing.*; | 
| 04 | 
| 05 | publicclassKeyEventTest extendsJFrame implementsKeyListener { | 
| 06 |     privateString baris1="", baris2="", baris3=""; | 
| 07 |     privateJTextArea textArea; | 
| 08 | 
| 09 |     publicKeyEventTest() { | 
| 10 |         super("Mencoba Key Event"); | 
| 11 | 
| 12 |         textArea = newJTextArea (10,15); | 
| 13 |         textArea.setText("Tekan sembarang tombol di keyboard..."); | 
| 14 |         textArea.setEnabled(false); | 
| 15 |         textArea.setDisabledTextColor(Color.BLACK); | 
| 16 |         getContentPane().add(textArea); | 
| 17 | 
| 18 |         addKeyListener (this); | 
| 19 | 
| 20 |         setSize (300,150); | 
| 21 |         setLocationRelativeTo(null); | 
| 22 |         setVisible(true); | 
| 23 |     } | 
| 24 | 
| 25 |     publicvoidkeyPressed (KeyEvent e) { | 
| 26 |         baris1 = "Tombol yang ditekan : "+ e.getKeyText(e.getKeyCode()); | 
| 27 |         setLines2and3 (e); | 
| 28 |     } | 
| 29 | 
| 30 |     publicvoidkeyReleased (KeyEvent e) { | 
| 31 |         baris1 = "Tombol yang dilepas : "+ e.getKeyText(e.getKeyCode()); | 
| 32 |         setLines2and3 (e); | 
| 33 |     } | 
| 34 | 
| 35 |     publicvoidkeyTyped (KeyEvent e) { | 
| 36 |         baris1 = "Tombol yang ditulis : "+ e.getKeyChar(); | 
| 37 |         setLines2and3 (e); | 
| 38 |     } | 
| 39 | 
| 40 |     privatevoidsetLines2and3 (KeyEvent e) { | 
| 41 |         baris2 = "This key is "+ (e.isActionKey() ? "": "not ") + "an action key"; | 
| 42 |         String temp = e.getKeyModifiersText(e.getModifiers()); | 
| 43 |         baris3 = "Modifier key pressed : "+ (temp.equals("") ? "none": temp); | 
| 44 |         textArea.setText(baris1 + "\n"+ baris2 + "\n"+ baris3 + "\n"); | 
| 45 |     } | 
| 46 | 
| 47 |     publicstaticvoidmain (String args[]) { | 
| 48 |             KeyEventTest test = newKeyEventTest(); | 
| 49 |             test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | 
| 50 |         } | 
| 51 | } | 

 
 
  
0 komentar: