Allllrighty then, this is the very last part of the little program I've been showing you. This is the part that starts really bringing it together
Here's what to do:
A) first off, we add a KeyListener along with all those other listeners at the top of the program, like so (MAKE IT LOOK LIKE THIS, DO NOT ADD ANOTHER WHOLE LINE):
implements ActionListener, ChangeListener, ItemListener, KeyListener
B) If this key listener thing sounds familar, well it should: your classmate Evan taught this to you weeks ago
C) The KeyListener requires that three methods be declared, even if you only are goiong to use one of them. Hey, I didn't set it up that way...not my idea! Anyway, just add this block underneath the (STILL empty) actionperformed code block
public void keyTyped(KeyEvent e) { }//empty method
public void keyPressed(KeyEvent e)
{
}// we will use this method
public void keyReleased(KeyEvent e) { }//empty method
D) Now the next step will use a switch case, which is similar to an if statement, but is especially adept at using ints. Here's the first small part which we will setup INSIDE the curly braces for the keyPressed(KeyEvent e) method
switch (e.getKeyCode()) //every key has an integer keyCode
{
case KeyEvent.VK_Q: // 1st in array?
whichOne=0;
break;
case KeyEvent.VK_W: // 2nd in array?
whichOne=1;
break;
case KeyEvent.VK_E: // 3rd in array?
whichOne=2;
break;
case KeyEvent.VK_R: // 4th in array?
whichOne=3;
} //end switch case
System.out.println(whichOne); //show value of integer in console
String codedStuff = enc.getText(); //get the stuff already in code JTextArea enc.setText(codedStuff + coded[whichOne]);//add new stuff into code JTextArea
} //end keypressed
E) The quick ones among you will have already figured out that this method receives input in the form of a key that's been pressed, then uses that key to set the value of an integer. This intger then points to an element in the coded Array, and then types that String to the other JTextArea, appending it at the end of encrypted text thats already there.
The entirety of the switch case can be found here:
http://www.box.net/shared/ppxd8c35tz
cheers,
Mr. L
Friday, April 24, 2009
Subscribe to:
Post Comments (Atom)

No comments:
Post a Comment