Hands-on Introduction to Object-Oriented Programming CIS 3100

Finally we can add the newly created button to our

This preview shows 6 out of 8 pages.

// Finally we can add the newly created button to our program window program.addButton(goButton); } } After we execute the GUIConverter’s main method, the following window will appear: BUT, while there now is a button, and I can enter numbers into the fields fine, nothing happens when I click the “Convert” button. To activate the button we need to implement a method that does the actual conversion. But the method should execute exactly when the button is pressed so we can not execute it manually or programmatically. Instead, we implement a specific method that is automatically called by the operating system whenever the button is pressed. That method must be called actionPerformed and must have the form: public void actionPerformed(ActionEvent ae) { // specific stuff } The actual implementation of our method is pretty easy: 1. retrieve the value in the DoubleField input 2. apply the conversion formula 3. put the converted value into the DoubleField ouput That is simple, because the DoubleField class provides just the right method setDouble and getDouble . Thus, we add the following method to our GUIConverter : public void actionPerformed(ActionEvent ae) { double temp = input.getDouble(); double convertedTemp = 5.0 / 9.0 * (temp - 32.0); output.setDouble(convertedTemp); } Note the similarity to our earlier and much simpler Converter program!
Image of page 6

Subscribe to view the full document.

However, that does not compile because the ActionEvent class is unknown and the method actionPerformed does not know anything about the input and output objects defined in another method! To fix the problem, we first import the appropriate Java class and second we move the definition of the input and output objects out of the main method and to the top of the class (we also mark them as static for technical reasons explained later). Our new, almost final, code is as follows: import javax.swing.JButton; import java.awt.event.ActionEvent; public class GUIConverter { // We now create two objects of type DoubleField at the top of our class // as FIELDS for our class (remember, classes have fields and methods static DoubleField input = new DoubleField(); static DoubleField output = new DoubleField(); public void actionPerformed(ActionEvent ae) { double temp = input.getDouble(); double convertedTemp = 5.0 / 9.0 * (temp - 32.0); output.setDouble(convertedTemp); } public static void main(String args[]) { // First we create a new SimpleWindow object via the new // operator and store it as a variable named program SimpleWindow program = new SimpleWindow(); // Now we can create a button with appropriate label to be used later JButton goButton = new JButton(); goButton.setText("Convert"); // Next, we call on the addLabel method of our new object program.addLabel("Fahrenheit: "); // Then we add the double field created earlier program.addField(input); // and now another label and the second field: program.addLabel("Celsius: "); program.addField(output); // Finally we can add the newly created button to our program window program.addButton(goButton); } } The program compiles but when we execute it still nothing happens. But two simple changes will fix that: Our class GUIConverter should extend
Image of page 7
Image of page 8
You've reached the end of this preview.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern