Java Network Programming. CSD008 Week 4. Topics for this week. GUIs JBuilder’s GUI Designer Applet s More GUI components Last week we looked at Panel s and Label s This week we will look at Button s, TextField s and TextArea s. Create a new Applet.
Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.
Java Network Programming CSD008 Week 4
Topics for this week • GUIs • JBuilder’s GUI Designer • Applets • More GUI components • Last week we looked at Panels and Labels • This week we will look at Buttons, TextFields and TextAreas
Create a new Applet • Follow the instructions from week 3, part 2 to: • Create a new project called week4part1 • Create an Applet called ButtonTester • Add a Panel (from AWT menu)called mainPanel to the Center of the Applet using BorderLayout • Change the Layout of mainPanel to XYLayout • Add a Label called messageLabel that contains the text “Messages will appear here” • Run the Applet to make sure it’s what we want
The New Applet Your new applet should look like this when you run it
a. Click on the button in the Toolbar for creating Buttons, then drag on the mainPanel to draw it Add a Button
a. Now change the name of the button to changeTextButton and change the label of it to “Press Me” Edit the Button
a. Double-click on the Button you have just drawn and you will brought to some code, we add code where the cursor is b. This is a method (remember Objects and method etc) that is called every time the Button is pressed Functionality of the Button
a. Write the code you want to be executed when the changeTextButton is pressed within this method. We will take a simple example that just prints a message every time the button is pressed. Code for the Button Now run the Applet and test it!!
Button Code cont’d • Now lets change the code so that rather than printing the message, it will display a message in messageLabel • Note that setText(String text) is a method of the class Label Now run the Applet and test it!!
Input of Text • Now we will add a TextField so that a user can input text to the program • Go into the designer again, and in the same way as before (the Label and Button) pick the TextField from the Toolbar and draw it on to mainPanel • Name this object inputTextField and change its text to “input text here”
Display the Input Text • Now lets edit the Buttons functionality again so that it displays the text from inputTextField in the messageLabel Now run the Applet and test it!!
Related Tasks • Experiment with the following in an Applet (i.e. try setting or reading their values etc) • TextAreas – very similar to TextField • ScrollPane – you can insert TextAreas in these so you can scroll through a large amount of text • (Optional) If there are any other objects you’d like to understand try using them
Comments • It is essential that you understand and can reproduce on your own everything we have done so far • Week 1: Basics of Java • Week 2: OOP • Week 3 / 4: GUIs • Always keep a backup of your work • Start a new project every week or any time you start a new program