21
1 Class 6 Class 6

1 Class 6. 2 Objectives Objectives Enable your applications to perform actions in response to JButton clicks Get the text the user enters in a textfield

Embed Size (px)

Citation preview

Page 1: 1 Class 6. 2 Objectives Objectives Enable your applications to perform actions in response to JButton clicks Get the text the user enters in a textfield

1

Class 6Class 6

Page 2: 1 Class 6. 2 Objectives Objectives Enable your applications to perform actions in response to JButton clicks Get the text the user enters in a textfield

2

ObjectivesObjectivesEnable your applications to perform

actions in response to JButton clicksGet the text the user enters in a

textfield and convert it to an integer so arithmetic can take place

Use the plus operator

Page 3: 1 Class 6. 2 Objectives Objectives Enable your applications to perform actions in response to JButton clicks Get the text the user enters in a textfield

3

Expanding the program from class05:When the user clicks on the Add button, get the data in each of the text fields and add the two values together

Page 4: 1 Class 6. 2 Objectives Objectives Enable your applications to perform actions in response to JButton clicks Get the text the user enters in a textfield

4

In case the user clicks on the add button without typing anything into one or both of the textfields, add code to the previous problem from class 5 that will initially place a 0 in the two textfields

textField1.setText(“0”);textField2.setText(“0”);

Page 5: 1 Class 6. 2 Objectives Objectives Enable your applications to perform actions in response to JButton clicks Get the text the user enters in a textfield

5

How do we get the text that the user enters in a text field?

textField1.getText()

How do we put the value that the user entered from one text field into a different text field?

textField3.setText(textField1.getText());

Page 6: 1 Class 6. 2 Objectives Objectives Enable your applications to perform actions in response to JButton clicks Get the text the user enters in a textfield

6

How do we get the text that the user enters in a certain text field and add the value to the text that the user enters into a different text field and place the answer in a third text field?

Page 7: 1 Class 6. 2 Objectives Objectives Enable your applications to perform actions in response to JButton clicks Get the text the user enters in a textfield

7

How do we get the text that the user enters in a certain text field and add the value to the text that the user enters into a different text field and place the answer in a third text field?

Page 8: 1 Class 6. 2 Objectives Objectives Enable your applications to perform actions in response to JButton clicks Get the text the user enters in a textfield

8

Crucial to your understanding:

Data entered into text fields is what we call text or “string” data. In text format, this data cannot be used in any mathematical operations. Therefore, once you get the text, you have to convert the text to integer so that math can be performed. Additionally, if you want the mathematical answer to go back into a text field, you have to do the reverse… convert the integer to text (string) data.

All of the above would be done when the add button is clicked.

Page 9: 1 Class 6. 2 Objectives Objectives Enable your applications to perform actions in response to JButton clicks Get the text the user enters in a textfield

9

So first, add the code to allow an action to take place when the “add” button is clicked with the mouse.

Add to import statements: import java.awt.event.*;Add to user interface code: button1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {calculateJButtonActionPerformed(e); } } )

Page 10: 1 Class 6. 2 Objectives Objectives Enable your applications to perform actions in response to JButton clicks Get the text the user enters in a textfield

10

Before method main, add the code for the method calculateJButtonActionPerformed. This method will contain the code that defines WHAT is to be done when the button is clicked.

private void calculateJButtonActionPerformed(ActionEvent e){ textField3.setText(String.valueOf( Integer.parseInt (textField1.getText()) + Integer.parseInt(textField2.getText() ) ) )

} // end of method

Gets the text entered by the user in textField1 Gets the text entered by

the user in textField2

Page 11: 1 Class 6. 2 Objectives Objectives Enable your applications to perform actions in response to JButton clicks Get the text the user enters in a textfield

11

private void calculateJButtonActionPerformed(ActionEvent e){ textField3.setText(String.valueOf( Integer.parseInt (“23”) + Integer.parseInt(“42”) ) )

}

Gets the text entered by the user in textField1 Gets the text entered by

the user in textField2

Data (23 and 42 are text and not integers); therefore, these numbers have to be converted to integers for use in arithmetic. This is done through the parseInt method.

Page 12: 1 Class 6. 2 Objectives Objectives Enable your applications to perform actions in response to JButton clicks Get the text the user enters in a textfield

12

private void calculateJButtonActionPerformed(ActionEvent e){ textField3.setText(String.valueOf( 23 + 42 ) )}

Data (23 and 42 are text and not integers); therefore, these numbers have to be converted to integers for use in arithmetic. This is done through the parseInt method.

Page 13: 1 Class 6. 2 Objectives Objectives Enable your applications to perform actions in response to JButton clicks Get the text the user enters in a textfield

13

private void calculateJButtonActionPerformed(ActionEvent e){ textField3.setText(String.valueOf(65 ) )}

The sum 65 is an integer but we want to put it in textField3. For data to go into a textField it has to be text (a string) not an integer. String.valueOf converts the 65 to a string (text characters).

Page 14: 1 Class 6. 2 Objectives Objectives Enable your applications to perform actions in response to JButton clicks Get the text the user enters in a textfield

14

private void calculateJButtonActionPerformed(ActionEvent e){ textField3.setText(“65” )}

The setText method then puts the 65 into textField3.

Page 15: 1 Class 6. 2 Objectives Objectives Enable your applications to perform actions in response to JButton clicks Get the text the user enters in a textfield

15

Putting it all together:

Page 16: 1 Class 6. 2 Objectives Objectives Enable your applications to perform actions in response to JButton clicks Get the text the user enters in a textfield

16

import javax.swing.*;import java.awt.*;import java.awt.event.*;public class GuiTest extends JFrame { private JLabel textLabel1,textLabel2,outputLabel; private JTextField textField1, textField2, textField3; private JButton button1, button2;

public GuiTest() {

Container contentPane = getContentPane(); contentPane.setLayout(new FlowLayout());

Page 17: 1 Class 6. 2 Objectives Objectives Enable your applications to perform actions in response to JButton clicks Get the text the user enters in a textfield

17

textLabel1= new JLabel ("Enter score 1"); contentPane.add(textLabel1);

textField1= new JTextField(10); contentPane.add(textField1); textField2 = new JTextField(10); contentPane.add(textField2); textField1.setText("0"); textField2.setText("0");

textLabel2 = new JLabel("Enter score 2"); outputLabel = new JLabel("The answer is:"); textField3 = new JTextField(10); textField3.setText("0"); textField3.setEditable(false);

Page 18: 1 Class 6. 2 Objectives Objectives Enable your applications to perform actions in response to JButton clicks Get the text the user enters in a textfield

18

contentPane.add(textLabel2); button1 = new JButton ("Add"); button2 = new JButton("Subtract"); button1.addActionListener( new ActionListener() { public void actionPerformed (ActionEvent e) { calculateActionPerformed(e); } } );

Page 19: 1 Class 6. 2 Objectives Objectives Enable your applications to perform actions in response to JButton clicks Get the text the user enters in a textfield

19

contentPane.add(button1);contentPane.add(button2);

contentPane.add(outputLabel); contentPane.add(textField3);

setTitle("Testing My Gui");setVisible(true);setSize(450, 300);

} public void calculateActionPerformed(ActionEvent e) { textField3.setText(String.valueOf( Integer.parseInt(textField1.getText()) + Integer.parseInt(textField2.getText())));}

Page 20: 1 Class 6. 2 Objectives Objectives Enable your applications to perform actions in response to JButton clicks Get the text the user enters in a textfield

20

public static void main(String a [ ]) {GuiTest myFrame = new GuiTest(); myFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE); } // end of main } // end of class

Page 21: 1 Class 6. 2 Objectives Objectives Enable your applications to perform actions in response to JButton clicks Get the text the user enters in a textfield

21

Conclusion Conclusion of Class 6of Class 6