Activity_4B - COMP 1210 Activity 4B: Graphical User...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: COMP 1210 Activity 4B: Graphical User Interface Due: Monday, September 21, 2009 Note: In order to complete this activity, you will need to have the Thermostat class that you created in Activity 4A. Program Description: In this activity you will create a class called ThermostatGUI, which will represent an interface through which users can interact with your Thermostat class from Activity 4A. The following figure shows an example of the GUI that you will create. When users press the update button, input is taken from the two text fields and displayed next to the update button. Directions: Step 1: Create your class and add comments • Create a class that represents the panel designed to hold the GUI. ÏÏÏimport java.awt.*; ÏÏÏimport java.awt.event.*; ÏÏÏimport javax.swing.*; Represents the panel in which your GUI components will be placed. ÏÕÖ×public class ThermostatGUI extends JPanel{ Ïϧ //declare instance variables Ïϧ Constructor creates ÏϧÏÞßàpublic ThermostatGUI(){ your components and ÏϧÏϧ //instantiate Thermostat object Thermostat object. ÏϧÏϧ //instantiate button and text fields ÏϧÏϧ //instantiate JLabels ÏϧÏϧ //add action listener to button Main method creates an ÏϧÏϧ //add components to the panel instance of your GUI ÏϧÏϧ //set the panel characteristics and displays it ÏϧÏÏ©} Ïϧ ÏϧÏÞßàpublic static void main(String args){ ÏϧÏϧ //create a frame for the panel COMP 1210 | Fall 2009 1 COMP 1210 Activity 4B: Graphical User Interface Determines what ÏϧÏϧ //initialize ThermostatGUI panel happens when ÏϧÏϧ //add panel to frame and begin button is clicked ÏϧÏÏ©} Ïϧ ÏϧÏÕÖ×private class ChangeListener implements ActionListener{ ÏϧÏϧÏÞßàpublic void actionPerformed(ActionEvent event){ ÏϧÏϧÏϧ //get input from text fields ÏϧÏϧÏϧ //add values to thermostat object ÏϧÏϧÏϧ //display thermostat information ÏϧÏϧÏÏ©} ÏϧÏÏ©} ÏÏ©} • Step 2: Declare Variables Go to the “Declare instance variables” comment. Declare the following variables : o JButton called updateButton o 2x JTextField called roomInput and tempInput o 3x JLabel named roomInfo, tempInfo, and displayInfo o A Thermostat object named thermo with Shelby 3137 as the initial room value. roomInfo thermo updateButton roomInput tempInfo displayInfo tempInput Hint: variables are declared without using the new operator. For example: ÏϧÏíÏprivate JButton updateButton; ÏϧÏíÏprivate JTextField roomInput, tempInput; . . . COMP 1210 | Fall 2009 2 COMP 1210 • Activity 4B: Graphical User Interface Step 3: Set Up ThermostatGUI Constructor Find your 3 “instantiate” comments in the constructor for the ThermostatGUI class. o Initialize your Thermostat object with the room “Shelby 3137” (see Activity 4A if you need help instantiating a Thermostat object). o Initialize a JButton with the text “Update Information” Hint: JButtons are initialized with the button text represented as a String. exampleButton = new JButton(“Press Me”); o Initialize roomInput and tempInput with 40 columns each Hint: Send the number of columns to the JTextField constructor as an int value. exampleField = new JTextField(40); o Create JLabels with the following Strings as inputs to the JLabel constructor: roomInfo = new JLabel(“Enter Room:”); tempInfo = new JLabel(“Enter Temperature:”); displayInfo = new JLabel(thermo.toString()); //contains Thermostat Information o Add an action listener to the JButton. You will have to call the addActionListener method of updateButton and send (new ChangeListener()) as a parameter. Hint: exampleButton.addActionListener(new ChangeListener()); • Go to the add “components to the panel” comment o Add components to the panel in the following order: roomInfo, roomInput, tempInfo, tempInput, updateButton, displayInfo. Hint: a component is added by using this.add(component); this.add(roomInfo); this.add(roomInput); … o Add the following lines of code after the “set panel characteristics” comment: ÏϧÏϨ¹¹Ïthis.setBackground(Color.cyan); ÏϧÏϨ¹¹Ïthis.setPreferredSize(new Dimension(500, 150)); COMP 1210 | Fall 2009 3 COMP 1210 • Activity 4B: Graphical User Interface Step 4: Set Up Event for Button Press Go to the “get input from text fields” comment in the ChangeListener class and get input from the two text fields. ¨¹íÏdouble newTemp = Double.parseDouble(tempInput.getText()); ¨¹íÏString newRoom = roomInput.getText(); The static class parseDouble of the Double class is used to turn the String from the tempInput text field into a double value. • Go to “add values to thermostat object”. Now that you have the input for temperature (newTemp) and room (newRoom), you can pass these values to the setRoomAndTemp method of your Thermostat object. See Activity4A if you need help with Thermostat. • Go to “display thermostat information” and send thermo.toString() to the setText method of displayInfo (which is a JLabel). • Step 5: Main Method Go to the “create a frame for the panel” comment and create a frame named frame in which to place your Thermostat GUI. JFrame frame = new JFrame("Thermostat"); COMP 1210 | Fall 2009 4 COMP 1210 • Activity 4B: Graphical User Interface Now create an instance of your Thermostat GUI. ThermostatGUI guicontents = new ThermostatGUI(); • Finally, add the Thermostati GUI Panel to the JFrame that you created and set the frame characteristics using the following lines. ÏÏframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); ÏÏframe.getContentPane().add(guicontents); ÏÏframe.pack(); ÏÏframe.setVisible(true); • You are now ready to run your ThermostatGUI class. You should be able to enter a room and a temperature and press the Update Information button to change the Thermostat text. COMP 1210 | Fall 2009 5 ...
View Full Document

This note was uploaded on 10/12/2009 for the course COMP 1210 taught by Professor Cross during the Spring '07 term at Auburn University.

Ask a homework question - tutors are online