Programming IT, Week 9 Lab
Action Listeners
Dr and Dr Kevin :
There are two parts to this exercise. You do not have to do both parts. Part 1 is simpler and is more closely aligned to the examples seen in the lectures. Part 2 is more challenging. If you want to you can jump straight to Part 2.
Copyright By PowCoder代写 加微信 powcoder
You will build a ¡°FortuneTeller¡± program that uses a button to generate fortunes. See the image below.
You have been provided with two classes, FortuneTeller.java and FortunePanel.java. These provide the basic setup for the program, but with no event handling. You have also been provided with a text file containing so-called ¡°fortunes¡±, called ¡°fortunes.txt¡±. Create a new project containing these files.
You should create new classes, FortuneTeller1.java and FortunePanel1.java by copying the original files. You must edit these files so that when the user presses the central button, a random fortune is chosen from the list and displayed in the textbox.
Hint: include an array of strings as an instance variable of FortuneTeller1.java, and fill it with the strings from the text file in the constructor. Your ActionPerformed method can then generate a random index and access the corresponding string from the array.
You will work towards a program that has buttons for performing various actions. Start with the CipherLab.java file that you can get from Moodle. You will also need Mono.java. This will save you time doing the initial layout allowing you to focus on the ACTION!
You are going to create a GUI for an encoding and decoding system. This will take some text and encode it using a keyword. Given encoded text and the keyword, the system can decode. All of the code for encoding and decoding is provided in Mono.java (you do not need to modify this at all).
1. Download CipherLab.java, Mono.java and put them both into a new project. When you run CipherLab.java you should see this:
The final system will work as follows:
– A user will enter a keyword in the text field at the top. A keyword will be all capital
letters (just characters A to Z) and each character can only be used once.
– They will then type some input text into the next field (also in capitals)
– If they click encode, the text in the input will be encoded into the output
– If they click decode, the text in the output will be decoded back We¡¯ll now go through this one step at a time…
2. Modify the CipherLab class so that it implements ActionListener. You¡¯ll need to add an import line and also add an actionPerformed method.
3. Add the class as the action listener to the two buttons (you can do this in the setupFrame() method, or in the constructor (after setupFrame has been called)).
4. We¡¯ll now fill in the actionPerformed method. Firstly, add an if statement to check which button has been pressed. Use the getSource() method of the action event and compare it to each button in turn.
5. Within the method, add the code for encoding if the encode button is pressed. Here is some example code for how to use the Mono class to encode a string with a particular keyword:
Your code will be a bit different as you should get the key from the keyword textfield, and the input from the inputArea. Both components have getText() methods for this. Finally use the setText(String text) method for the outputArea to display the output. Note that the method will only encode upper case characters.
6. Repeat this process for decode. It is the same process, but should use the decode method for Mono instead of encode.
7. Test your code by encoding some text and then copying the output into the input area and clicking decode. In the output, you should have the original text.
8. Encode a message and email it to your neighbour. Give them the key, and they should be able to decode.
9. [optional from here] In the final part, you will make the program a bit more user friendly. A valid keyword has to be all capital letters and not use the same letter more than once (HELO is valid, HELLO is not). Modify the code so that initially the buttons are not enabled. Buttons can be enabled by calling their setEnabled(true) or setEnabled(false) methods.
10. Add the class as an action listener to the keyword text field. Actions are created from a text field when the user types some text and then presses return.
11. Add another if statement in your action listener that will detect events from the text field.
12. Within this part of the listener, we will check the keyword. The Mono class has a static boolean method for checking keywords checkKeyword(String key). Call this method from within the action listener (remember that static methods are called from the class and not from a specific object) and if it returns true, enable the encode and decode buttons. If false, disable the buttons.
13. [very optional ¡ª this is using things not covered in the course, but is here in case you want to experiment]. Waiting for a user to press return is a bit annoying (the action is
String key = “HELO”;
mono = new Mono(key);
String inputText = “A MESSAGE TO DECODE”; outputText = mono.encode(inputText);
only fired then). You can use a KeyListener to detect individual key presses. Modify your code so that it uses a key listener to update the button status as the user types in the keyword field. Hint: use the keyReleased method (i.e. an event is fired when the finger comes off the key) ¡ª it seems to work better than the keyTyped one.
14. [ditto] Encode in real time as the user types in the input area!
15. [ditto] Remove the encode / decode buttons and replace with a two radio buttons so
that the user can choose between real time encoding and decoding.
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com