Hands-on Introduction to Object-Oriented Programming CIS 3100

Simplewindow ie be a particular simplewindow in the

Info icon This preview shows pages 7–8. Sign up to view the full content.

View Full Document Right Arrow Icon
SimpleWindow , i.e. be a particular SimpleWindow In the main method we need to now create an object of type GUIConverter , no longer of type SimpleWindow The two simple but subtle – and final – changes to the code are highlighted in bold below: import javax.swing.JButton; import java.awt.event.ActionEvent;
Image of page 7

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
// We add the keywords extends to our class definition as follows: public class GUIConverter extends SimpleWindow { // Similarly, we create two objects of type DoubleField 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[]) { // We now create a new GUIConverter object via the new // operator and store it as a variable named program GUIConverter program = new GUIConverter (); // 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); } } Now when we execute our final program (by right-clicking and selecting main), our program looks as before but now it actually works! Finally!
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

{[ 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