Download presentation
Presentation is loading. Please wait.
Published byStewart Newton Modified over 9 years ago
1
1 Class 6
2
2 Objectives Objectives Enable your applications to perform actions in response to JButton clicks Get the text the user enters in a textfield and convert it to an integer so arithmetic can take place Use the plus operator
3
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
4
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”);
5
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());
6
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?
7
7
8
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.
9
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); } } )
10
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
11
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.
12
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.
13
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).
14
14 private void calculateJButtonActionPerformed(ActionEvent e) { textField3.setText(“65” ) } The setText method then puts the 65 into textField3.
15
15 Putting it all together:
16
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());
17
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);
18
18 contentPane.add(textLabel2); button1 = new JButton ("Add"); button2 = new JButton("Subtract"); button1.addActionListener( new ActionListener() { public void actionPerformed (ActionEvent e) { calculateActionPerformed(e); } } );
19
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()))); }
20
20 public static void main(String a [ ]) {GuiTest myFrame = new GuiTest(); myFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE); } // end of main } // end of class
21
21 Conclusion of Class 6
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.