Download presentation
Presentation is loading. Please wait.
Published byCaroline Walters Modified over 8 years ago
1
1 Lesson: Applets with User Input and Output with GUI ICS4M
2
2 Learning objectives How to create Label? How to create TextField? How to create Button? How to respond to user action?
3
3 Applet as a Container Applet Viewer:
4
4 The “init” method Q: What is init method used for?Q: What is init method used for? A:A: To initialize any instance variables.To initialize any instance variables. To set up the GUI components used by the Applet.To set up the GUI components used by the Applet. public void init ( ) { // Place the body of the init method here. }
5
5 Demo: BusFare3
6
6 Label What is a label? A label is Java’s way of adding a single line of text into an applet. The text is non-interactive. That is, it cannot be clicked on. A label is Java’s way of adding a single line of text into an applet. The text is non-interactive. That is, it cannot be clicked on.
7
7 How to create a label? Use constructor: Label myLabel = new Label(“Hello World”); Label myLabel = new Label(“Hello World”); “Hello World” myLabel Note: you need to add this label into the Applet container.
8
8 TextField What is a Text field? A Text Field is a single line text input. A Text Field is a single line text input. Applet’s way of getting input from user. Q: Where does this class come from?
9
9 How to create text field? 2 different constructors. Constructor 1: TextField(int cols) TextField(int cols) Creates a TextField cols columns wide. Creates a TextField cols columns wide. Constructor 2: TextField (String str, int cols) TextField (String str, int cols) Allows user to initialize the text field with initial string. Allows user to initialize the text field with initial string.
10
10 Example: Text Field TextField input; input = new TextField(5); Or TextField input = new TextField(5)
11
11 Add method void add (Component comp) Adds Component comp to the Applet using the Applet’s layout manager. Adds Component comp to the Applet using the Applet’s layout manager. Q: Which class does “add” method belong to? Q: Why are we using this version of “add” method?
12
12 Example: add method // Set up GUI components. public void init ( ) { prompt = new Label(“Enter age then press Return”); input = new TextField(5); // Place label and text field on applet add (prompt); add (input); } // init method
13
13 Action method boolean action (Event evt, Object o) Call by the system when a button is clicked or an ENTER is pressed in a text field in the applet. Call by the system when a button is clicked or an ENTER is pressed in a text field in the applet. The Button or TextField object that was clicked or had Return pressed is passed as the target field in evt. The Button or TextField object that was clicked or had Return pressed is passed as the target field in evt. The method must return “TRUE” if the event was handled and “FALSE” if the event was not handled. The method must return “TRUE” if the event was handled and “FALSE” if the event was not handled. Q1: How do we get the input from the TextField? Q2: How do we put something in a TextField?
14
14 Demo: BlockButton
15
15 Buttons There are 2 constructors for creating buttons: public Button();No Label public Button();No Label public Button(String label)With label public Button(String label)With label To create a button, we use Button myButton = new Button(“ButtonName”); Button myButton = new Button(“ButtonName”); Then add the button to the container. Then add the button to the container.
16
16 Class Exercise: Pig Latin Learn how to use TextArea. Write the Pig Latin program using Labels, TextField, and TextArea Hint: Use subString or charAt methods to break up your string into small tokens. Pig Latin rule: Move the first character of each word to the back and add “ay”. Move the first character of each word to the back and add “ay”. For example: nice icenay For example: nice icenay
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.