Graphics User Defined Forms, Part I

Similar documents
GUI Forms and Events, Part II

GUI, Events and Applets from Applications, Part III

Repetitive Program Execution

Repetition Using the End of File Condition

Arrays & Classes. Problem Statement and Specifications

Sequential Program Execution

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7

Frames, GUI and events. Introduction to Swing Structure of Frame based applications Graphical User Interface (GUI) Events and event handling

GUI and its COmponent Textfield, Button & Label. By Iqtidar Ali

Java Programming Lecture 6

Lecture 5: Java Graphics

CS 251 Intermediate Programming GUIs: Components and Layout

Editing, Compiling, Executing and Submitting Programs

AP CS Unit 11: Graphics and Events

Window Interfaces Using Swing Objects

Object-Oriented Programming: Revision. Revision / Graphics / Subversion. Ewan Klein. Inf1 :: 2008/09

Part 3: Graphical User Interface (GUI) & Java Applets

CSEN401 Computer Programming Lab. Topics: Graphical User Interface Window Interfaces using Swing

Starting Out with Java: From Control Structures Through Objects Sixth Edition

CS Exam 1 Review Suggestions

Window Interfaces Using Swing Objects

First Name: AITI 2004: Exam 2 July 19, 2004

Swing from A to Z Some Simple Components. Preface

SINGLE EVENT HANDLING

Midterm assessment - MAKEUP Fall 2010

Introduction to the JAVA UI classes Advanced HCI IAT351

Lecture 3: Java Graphics & Events

Java Swing. Recitation 11/(20,21)/2008. CS 180 Department of Computer Science, Purdue University

2IS45 Programming

Topic 9: Swing. Swing is a BIG library Goal: cover basics give you concepts & tools for learning more

Topic 9: Swing. Why are we studying Swing? GUIs Up to now: line-by-line programs: computer displays text user types text. Outline. 1. Useful & fun!

CSC 1214: Object-Oriented Programming

Class 14: Introduction to the Swing Toolkit

Swing. By Iqtidar Ali

Packages: Putting Classes Together

The JFrame Class Frame Windows GRAPHICAL USER INTERFACES. Five steps to displaying a frame: 1) Construct an object of the JFrame class

17 GUI API: Container 18 Hello world with a GUI 19 GUI API: JLabel 20 GUI API: Container: add() 21 Hello world with a GUI 22 GUI API: JFrame: setdefau

First Name: AITI 2004: Exam 2 July 19, 2004

Graphical User Interface

Here are the steps to get the files for this project after logging in on acad/bill.

Graphics programming. COM6516 Object Oriented Programming and Design Adam Funk (originally Kirill Bogdanov & Mark Stevenson)

Systems Programming. Bachelor in Telecommunication Technology Engineering Bachelor in Communication System Engineering Carlos III University of Madrid

Here are the steps to get the files for this project after logging in on acad/bill.

Final Examination Semester 2 / Year 2011

1.00 Lecture 14. Lecture Preview

Hanley s Survival Guide for Visual Applications with NetBeans 2.0 Last Updated: 5/20/2015 TABLE OF CONTENTS

Control Flow: Overview CSE3461. An Example of Sequential Control. Control Flow: Revisited. Control Flow Paradigms: Reacting to the User

JFrame In Swing, a JFrame is similar to a window in your operating system

CSCI 201L Midterm Written SOLUTION Fall % of course grade

Introduction to Graphical Interface Programming in Java. Introduction to AWT and Swing

Chapter 12 Advanced GUIs and Graphics

H212 Introduction to Software Systems Honors

CS Fall Homework 5 p. 1. CS Homework 5

Building a GUI in Java with Swing. CITS1001 extension notes Rachel Cardell-Oliver

Arrays, Exception Handling, Interfaces, Introduction To Swing

MIT AITI Swing Event Model Lecture 17

Queen s University Faculty of Arts and Science School of Computing CISC 124 Final Examination December 2004 Instructor: M. Lamb

Give one example where you might wish to use a three dimensional array

Chapter 13. Applets and HTML. HTML Applets. Chapter 13 Java: an Introduction to Computer Science & Programming - Walter Savitch 1

Summary Chapter 25 GUI Components: Part 2

CSE 143. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

Java Help Files. by Peter Lavin. May 22, 2004

CSIS 10A Assignment 7 SOLUTIONS

Introduction to Java

Computer Science 210: Data Structures. Intro to Java Graphics

Outline. Topic 9: Swing. GUIs Up to now: line-by-line programs: computer displays text user types text AWT. A. Basics

Multiple Choice Questions: Identify the choice that best completes the statement or answers the question. (15 marks)

Swing - JTextField. Adding a text field to the main window (with tooltips and all)

Swing I CHAPTER EVENT-DRIVEN PROGRAMMING 921 Events and Listeners 921

Swing I Event-Driven Programming Buttons, Events, and Other Swing Basics Containers and Layout Managers 946

CSC 161 SPRING 17 LAB 2-1 BORDERLAYOUT, GRIDLAYOUT, AND EVENT HANDLING

Overview. Lecture 7: Inheritance and GUIs. Inheritance. Example 9/30/2008

Chapter 13 Lab Advanced GUI Applications Lab Objectives. Introduction. Task #1 Creating a Menu with Submenus

Do not turn to the next page until the start of the exam.

JRadioButton account_type_radio_button2 = new JRadioButton("Current"); ButtonGroup account_type_button_group = new ButtonGroup();

University of Cape Town Department of Computer Science. Computer Science CSC117F Solutions

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Graphical User Interfaces. Swing. Jose Jesus García Rueda

TTTK Program Design and Problem Solving Tutorial 3 (GUI & Event Handlings)

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

To gain experience using GUI components and listeners.

11/6/15. Objec&ves. RouleQe. Assign 8: Understanding Code. Assign 8: Bug. Assignment 8 Ques&ons? PROGRAMMING PARADIGMS

JFrame & JLabel. By Iqtidar Ali

CS 335 Lecture 02 Java Programming

CS 209 Spring, 2006 Lab 8: GUI Development Instructor: J.G. Neal

More about GUIs GEEN163

Graphical User Interfaces (GUIs)

Lecture (06) Java Forms

Chapter 12 GUI Basics

Command-Line Applications. GUI Libraries GUI-related classes are defined primarily in the java.awt and the javax.swing packages.

: the User (owner) for this file (your cruzid, when you do it) Position: directory flag. read Group.

Graphical User Interfaces. Comp 152

CPS109 Lab 1. i. To become familiar with the Ryerson Computer Science laboratory environment.

Graphical User Interface (GUI) components in Java Applets. With Abstract Window Toolkit (AWT) we can build an applet that has the basic GUI

Graphic User Interfaces. - GUI concepts - Swing - AWT

Swing from A to Z Using Focus in Swing, Part 2. Preface

Lesson 2: First Java Programs

CS 170 Java Programming 1. Week 9: Learning about Loops

CS 2334: Programming Structures and Abstractions Exam 2 November 4, Please wait to open this exam booklet until you are told to do so.

Lab 4. D0010E Object-Oriented Programming and Design. Today s lecture. GUI programming in

Transcription:

Graphics User Defined Forms, Part I Quick Start Compile step once always mkdir labs javac PropertyTax5.java cd labs mkdir 5 Execute step cd 5 java PropertyTax5 cp /samples/csc/156/labs/5/*. cp PropertyTax1.java PropertyTax5.java Submit step emacs PropertyTax5.java & submit csc156abc 5 Problem Statement and Specifications These next two programming assignments will be in two parts. The first part of each will focus on the development of a graphic user interface (GUI) that generates a form for the user to interact with. Human Computer Interaction (HCI) is often regarded as the intersection of computer science, behavioral science, design and additional fields of study. It occurs at the user interface and the Java language has a number of tools available to use graphics in a GUI. The second part of the next two exercises will survey the use of methods and introduce you to classes in the Java language. Consequently, there will not be a significant amount of algorithmic development in the first parts of these exercises as the GUI design does have some requirements that some objects be generated before others, but the dependency relationships that we ll pursue will not be as complicated as the other software tools that we ve pursued up to this point. Assignment 5 Statement: Write a program in a file named PropertyTax5.java that will display the form shown in Figure 1 for the user to enter data in the appropriate fields to calculate local property and school district taxes. None of the JTextFields nor JButtons needs to work for this assignment, but the display with the appropriate labels must be identical to Figure 1. Assignment5 Specifications: Set JFrame parameters to determine window Output JLabels to identify JTextField I/O opportunities Output JButton labels to identify event options Object Analysis and Algorithmic Development When Java was first introduced, it supported an Abstract Window Toolkit to manipulate graphics that would provide portability for GUI applications, but was rather challenging to use. The Java Swing Toolkit 1

Figure 1: Lab 5 Display was designed to provide a less challenging interface to the more popular GUI elements. These objects are best used within a JFrame that can be interpreted as a window with specified size and banner title. The contents of PropertyTax1.java already have these lines coded within the file so we only need to add lines to that file. Let s examine the contents of Figure 2. The import statement in green makes the various Swing objects available to us. We ll describe those in more detail below. The class PropertyTax1 in magenta is declared as a subclass of JFrame. Object-oriented languages use an inheritance mechanism so that sub-classes can be created from existing classes. The sub-classes inherit available data attributes and methods that function within the ancestor or super class. For this exercise, we simply need to know that the JFrame sub-class (or PropertyTax1) can control aspects of the window that we ll be displaying our information in. The next statement declares symbolic constants WIDTH and HEIGHT within the class, but outside any method. We ll recommend that any other variables and constants be declared in this area. Then comes the declaration of a special method known as a constructor in black. This method is how we can create instances of a class. Note that it has the same name as the class name (which is the same name as the file name) and has no return value. This is typical of a constructor, but it is not required that constructors do not have parameters as this example shows. All of the code that we ll be writing for this exercise will occur in the constructor above the lines that are already there. The lines that are in the constructor 1) determine the title of the window that will be displayed in the banner with the settitle() method, 2) determine the size of the window with the setsize() method, 3) establish the visibility of the window with the setvisible() method and 4) determine that clicking on the boxed x in the upper right-hand corner of the window will close the window. After the constructor comes the main method in cyan which simply invokes the constructor to instantiate an object of type PropertyTax1 and store it in a variable named proptax. Examining the contents of Figure 1, we can see that it is organized into 7 rows where each row has 2 columns. The last row is easiest to discuss in that both objects in the last row are JButton objects. JButtons have labels that provide directions to the user indicating what will happen when the user uses the mouse to click on the JButton. In this manner, the JButton is an output object as it directs the user what will happen if the JButton is clicked and it is also an input object as it is only activated if the user uses the mouse and clicks on it. The six objects in the left-hand column above the JButton are JLabel objects. These are simply output objects that are used to describe other objects that are near them. Each JLabel object in the left-hand column of the first six rows corresponds to a JTextField object in the right-hand column of the first six rows. Each of the JTextField objects can be used for textual input and output either by the user or the program. In this particular application, the top three JTextField objects are intended for the user to enter information regarding a home s assessed value, regional school tax rate and county tax rate. The bottom three JTextField objects are used for the calculated school taxes, county taxes and total taxes based upon the input that the user had 2

import javax.swing.*; public class PropertyTax1 extends JFrame { private static final int WIDTH = 400, HEIGHT = 300; public PropertyTax1() { settitle("calculation of Property Taxes"); setsize(width, HEIGHT); setvisible(true); setdefaultcloseoperation(exit ON CLOSE); } // end of the constructor method public static void main(string[] args) { PropertyTax1 proptax = new PropertyTax1(); } // end of the main method } // end of the class Figure 2: Lab 5 JFrame Shell entered in the top three JTextField areas. We ll identify those objects that we need as we describe our algorithm for this program (note that this algorithm will accommodate the next two assignments.) Let s start where we always have, with the IPO model. 1. Input 2. Process 3. Output As was the case with the JOptionPane methods, all of the input and output will be interpreted as String values, so we ll need to utilize some of the same numeric conversion and formatting methods that we did in previous exercises. 1. display the form and wait for the user to create a JButton event 2. process if the user clicks on the exit JButton (a) process to shut down the window 3. process if the user clicks on the calculate JButton (a) input the 3 String values from the JTextField objects that represent the assessed value, the regional school tax rate and the county tax rate (b) process to convert the 3 JTextField String objects to double (c) process to calculate the regional school tax, county tax and total tax (d) display those three tax values in the appropriate JTextField objects 3

To clarify, step 2.(a) only occurs if the exit JButton is clicked. Likewise, steps 3.(a), 3.(b), 3.(c) and 3.(d) will only execute if the calculate JButton is clicked. The tax values will be recalculated each time that the calculate JButton is clicked on. Of course, once the exit JButton is clicked on, the window shuts down. This algorithm will be explored more fully in the next assignment. Our goal for this lab is to discuss step 1., how to create the form and display it. Line that are displayed in blue represent areas where code will need to be written or modified to complete the assignment. Pseudo code for Creating and Displaying the GUI Form Statement Data objects 1) declare & initialize 6 labels JLabel, String & Swing.Constants objects 2) declare & initialize 6 text I/O fields JTextField & int objects 3) declare & initialize 2 button objects JButton & String objects 4) declare & initialize a Container Container object & getcontentpane() method 5) configure the Container Container.setLayout() method, GridLayout & int objects 6) place GUI objects in the Container JLabel, JTextField, JButton objects and Container.add() method 7) set the dimensions of the window setsize() method & int objects 8) set the title of the window settitle() method & String object 9) set the visibility of the window setvisible() method & boolean object 10) set the default close operation setdefaultcloseoperation() method & int objects Coding for Compilation Logon to your csc.oakton.edu account and create a new subdirectory of the labs directory called 5 to work on your assignment. cd labs mkdir 5 cd 5 A shell of a main program has already been included in the file /samples/csc/156/labs/5/propertytax1.java that you can copy over to your local directory by issuing the following commands. cp /samples/csc/156/labs/5/*. cp PropertyTax1.java PropertyTax5.java emacs PropertyTax5.java & The contents of this file appear in Figure 2. After you change every occurrence of PropertyTax1 to PropertyTax5, compile and execute your program with the following commands. javac java PropertyTax5.java PropertyTax5 The output of this should match the window shown in Figure 3. Declaration of Variables The scope of a symbol is the set of lines that can access that symbol. To maximize scope, it will make sense in this instance to declare the variables within the class, but outside the constructor method. 4

Figure 3: Lab 5 Original Output Step 1 // declare and initialize 6 JLabels There are 6 JLabel fields that we must accommodate and the text that they display should be right-justified. So, in the area within the class we could write the following declaration. JLabel assessl = new JLabel("Assessment Home Value", SwingConstants.RIGHT); Do this for the other 5 JLabel objects as well. Step 2 // declare and initialize 6 JTextFields In the area where you ve declared the JLabel objects, declare the JTextFields in a manner similar to below. JTextField assesstf = new JTextField(10); Similarly, you ll need to do this for 5 additional JTextFields. Step 3 // declare and initialize 2 JButtons Once again in the area where the JLabel and the JTextField declarations are, place the following declaration. JButton exit = new JButton("Exit"); Similarly, you ll need to do this for a JButton that should be named calculate. Steps 4-5 // declare and initialize a Container // set the container layout These are statements that do not rely upon the new operator. They will retrieve the content pane and introduce a design pattern so that we can build the GUI. Consequently, it is inappropriate to place this code above the 5

Figure 4: Lab 5 Half-filled Content Pane constructor, so we ll place it within the constructor, near the top. It is important to note that anything that we intend to display must be executed before the setvisible(true) command is executed. In order to use the Container class, it will be necessary to place a import java.awt.*; statement at the top of your file. It is the GridLayout() method invocation that creates the 7 rows by 2 columns pattern. Container pane = getcontentpane(); pane.setlayout(new GridLayout(7,2)); Step 6 // set GUI objects in the container This is simply the execution of the following command repeatedly for each GUI object. pane.add(assessl); Each pair of the repetitions of the add() method will fill a row. When you have added 8 of the 14 GUI objects to be displayed, we might verify that our GUI construction is proceeding in the manner that we expect. Checkpoint Although we re almost done, at this point, it might be a good idea to compile this much before we build more. Save your program to disk by choosing the Save command from the Files menu of your emacs session. Then, compile your program by choosing the Compile... command from the Tools menu and change the make -k that is displayed to javac PropertyTax5.java. Compiler errors can be parsed with the keystroke C-x and need to be repaired before your program can execute. When your program has compiled, click on your xterm window to access your command line prompt, and issue the command java PropertyTax5. The output displayed should look in the same manner as Figure 4. Once you are sure that the you re on the right track, you should use the add() method to place the remaining objects into the content pane. Steps 7-10 // set the dimensions of the window // set the title of the window 6

// set the visibility of the window // set the default close operation. CSC 156 - Assignment 5 PropertyTax These statements were already in the original PropertyTax1.java file. After you ve loaded all of the GUI objects into the content pane, your output should look like that of Figure 1. Note that it may be necessary for you to halt your program using the ps and kill commands as you did in the last exercise. Testing for errors At this point, there is not a great amount that you can do with your program as it only displays the GUI. We ll have more opportunity to test this code in the next exercise. There is a correctly functioning version of the program for PropertyTax5.java at this link. Printing and submitting Once you are satisfied with the correctness of your program, print it as you did with previous assignments by using the following command that assumes that you are working in the room 1234 at Oakton. Retrieve your copy from the printer. printer 1234 PropertyTax5.java Finally, submit your program with the following command that assumes that you are registered in section abc of CSC 156. submit csc156abc 5 7