Hands-on Introduction to Object-Oriented Programming CIS 3100

Finally repeat the process adding another label

This preview shows page 4 - 7 out of 8 pages.

Finally, repeat the process adding another label “Celsius: “, then another field, using the output object as parameter. The window on your desktop should now look as follows: The program looks good, but does not yet do anything. Still, it shows how to step by step create objects and exercise their methods. The bad news, however, is that we would have to do these manual steps each time again. But we can also program these steps into a new class (program) so that they get executed automatically. To do that, we create another class as usual, which we name GUIConverter . It should contain the standard main method, making our class into a program, and the method should simply perform the steps above automatically. Here is the listing of the new class: public class GUIConverter { 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(); // Similarly, we create two objects of type DoubleField DoubleField input = new DoubleField(); DoubleField output = new DoubleField(); // 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);
Image of page 4

Subscribe to view the full document.

// and now another label and the second field: program.addLabel("Celsius: "); program.addField(output); } } Now we can right-click on the GUIConverter class to select the standard main method to execute our program – voila, with one swoop the complete Window appears, as before, except we don’t have to do any “manual” work. Of course our program does not yet do anything (other than look good) because we have not (yet) added the appropriate methods to the appropriate classes. To finish everything up, we first add a button to our program. But to do that, we must first create a button using the new operator, but we don’t really have a ready-made ‘Button’ class. Alas, Java comes with literally thousands of pre-manufactured classes, among them a JButton class appropriate for our purpose. But before we can use that class we must import it from an appropriate location. The necessary, new, lines are indicated in bold (including the lines at the top of the program listing) // We need to import (make available) the JButton class before we can use it // We use an ‘import’ statement for that: import javax.swing.JButton; public class GUIConverter { 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(); // Similarly, we create two objects of type DoubleField DoubleField input = new DoubleField(); DoubleField output = new DoubleField();
Image of page 5
// 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);
Image of page 6

Subscribe to view the full document.

Image of page 7

{[ snackBarMessage ]}

Get FREE access by uploading your study materials

Upload your study materials now and get free access to over 25 million documents.

Upload now for FREE access Or pay now for instant access
Christopher Reinemann
"Before using Course Hero my grade was at 78%. By the end of the semester my grade was at 90%. I could not have done it without all the class material I found."
— Christopher R., University of Rhode Island '15, Course Hero Intern

Ask a question for free

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