CiS 260: App Dev I Chapter 6: GUI and OOD
GUI Components GUI stands for __________ user interface. A single user interface is much better than individual input and output windows. Here are some of the GUI components: JFrame: window that holds other components JTextField: area for either input or output JLabel: text that indicates what the user should do JButton: component that causes an action Components are place in the content _____ of the window. Clicking a button is an _________.
Classes and Objects GUI components are instances (________) of classes. A class is a category of things that have common attributes and behaviors. In Java, the attributes and behaviors are called data members (fields) and _________. A window object from the JFrame class has data members like _______, height, and width, and methods like _________. In Java, a class is used to define the fields and methods of a type of object provide a method to create these objects
Inheritance and Constructors ___________ is the ability of one class to inherit the fields and methods of another class. The keyword ________ is how you implement inheritance in Java. public class RectangleProgram extends JFrame The class that inherits is called the _________ and the class which provides the fields and methods is called the _________. A ___________ is a method in a class that creates an object. The name of a constructor is the same as the name of the _______.
Creating a Simple Window //Java program to create a simple window. import javax.swing.*; public class RectangleProgramOne extends JFrame { private static final int WIDTH = 400; private static final int HEIGHT = 300; public RectangleProgramOne() setTitle("Area and Perimeter of a Rectangle"); setSize(WIDTH,HEIGHT); setDefaultCloseOperation(EXIT_ON_CLOSE); setVisible(true); } public static void main( String[] args) RectangleProgramOne rectProg = new RectangleProgramOne();
The Content Pane A content pane is the inner area of a window. To get access to the content pane, you need to first create a __________ object: Container pane = getContentPane(); The class Container is in the package _________. To create a layout (5 rows, 2 columns) for the object pane Pane.setLayout(new GridLayout(5,2)); You must set the layout before adding ___________.
JLabel The following statement declares four JLabel objects private JLabel lengthL, widthL, areaL, perimeterL; The following statement creates a JLabel object lengthL = new JLabel("Enter the length: ", SwingConstants.RIGHT); The following statement adds a label object to the content pane pane.add(lengthL);
Creating a Window With Labels I //Java program to create a window and place four labels. import javax.swing.*; import java.awt.*; public class RectangleProgramTwo extends JFrame { private static final int WIDTH = 400; private static final int HEIGHT = 300; private JLabel lengthL, widthL, areaL, perimeterL; public RectangleProgramTwo() setTitle("Area and Perimeter of a Rectangle"); lengthL = new JLabel("Enter the length: ",SwingConstants.RIGHT); widthL = new JLabel("Enter the width: ",SwingConstants.RIGHT); areaL = new JLabel("Area: ",SwingConstants.RIGHT); perimeterL = new JLabel("Perimeter: ",SwingConstants.RIGHT);
Creating a Window With Labels II Container pane = getContentPane(); pane.setLayout(new GridLayout(4,1)); pane.add(lengthL); pane.add(widthL); pane.add(areaL); pane.add(perimeterL); setSize(WIDTH, HEIGHT); setVisible(true); setDefaultCloseOperation(EXIT_ON_CLOSE); } public static void main(String[] args) { RectangleProgramTwo rectProg = new RectangleProgramTwo();
JTextField The following statement declares four JTextField objects private JTextField lengthTF, widthTF, areaTF, perimeterTF; The following statement creates a JTextField object lengthTF = new JTextField(10); The following statement adds a label object to the content pane pane.add(lengthTF);
Window With Labels and Text Fields
JButton The following statements create two ________ objects JButton calculateB, exitB; calculateB = new JButton("Calculate"); exitB = new JButton("Exit"); The following statements add the button objects to the ________ pane pane.add(calculateB); pane.add(exitB); The GridLayout now needs 5 rows, 2 columns pane.setLayout(new GridLayout(5,2)); Placement of components in the grid is from left to right, then top to bottom.
Window w/ Labels, Text Fields, Buttons
Handling Events When a button is clicked, an ActionEvent object is created, which calls the actionPerformed() method of an ActionListener object. The class ActionListener is called an __________, a class that contains only method declarations, such as actionPerformed(). For each JButton, you must _________ a particular ActionListener object by creating a class to handle the action event. You must then define the actionPerformed() method within the handler class that you created.
Steps To Deploy Button Handlers I Within the application class, declare an action handler object for a button CalculateButtonHandler calculateHandler; After creating the JButton object, create the action handler object calculateHandler = new CalculateButtonHandler(); Then register (associate) the button handler object with the button calculateB.addActionListener(calculateHandler);
Steps To Deploy Button Handlers II Create the class that handles the action event private class CalculateButtonHandler implements ActionListener { public void actionPerformed(ActionEvent e) { double width, length, area, perimeter; length = Double.parseDouble(lengthTF.getText()); width = Double.parseDouble(widthTF.getText()); area = length * width; perimeter = 2 * (length + width); areaTF.setText("" + area); perimeterTF.setText("" + perimeter); }
Completed Application (pp. 287-289)
More About OO Design (OOD) Objects are things that have attributes and __________ are instances of a particular class A _______ is the definition of the attributes and behaviors of things that belong to it. A class variable is the software implementation of a class attribute. A class method is a method that changes the value of a class variable the software implementation of a class behavior _____________ is the act of combining variables and methods in a software class.
A Simplified OOD Methodology Write a detailed description of the problem Calculate the area and perimeter of a rectangle Identify all relevant nouns and _______. Rectangle, area, perimeter From the nouns, select _________. Rectangle Identify the significant attributes of the objects. Length, width From the verbs, select the __________. Calculate area, calculate perimeter