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

Similar documents
I.1 Introduction Matisse GUI designer I.2 GroupLayout Basics Sequential and Parallel Arrangements sequential horizontal orientation

Answer on question #61311, Programming & Computer Science / Java

AP CS Unit 11: Graphics and Events

Graphical User Interface (GUI)

* To change this license header, choose License Headers in Project Properties.

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7

Java Programming Lecture 6

SampleApp.java. Page 1

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

Window Interfaces Using Swing. Chapter 12

Graphical User Interface

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

CS 251 Intermediate Programming GUIs: Event Listeners

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

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

Graphical User Interfaces. Comp 152

Part I: Learn Common Graphics Components

Packages: Putting Classes Together

CSC 160 LAB 8-1 DIGITAL PICTURE FRAME. 1. Introduction

Programming graphics

Window Interfaces Using Swing Objects

Lecture (06) Java Forms

More about GUIs GEEN163

Introduction to Graphical User Interfaces (GUIs) Lecture 10 CS2110 Fall 2008

Java Swing. based on slides by: Walter Milner. Java Swing Walter Milner 2005: Slide 1

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

DEMYSTIFYING PROGRAMMING: CHAPTER FOUR

Graphical User Interface (GUI)

To gain experience using GUI components and listeners.

Window Interfaces Using Swing Objects

Course Status Networking GUI Wrap-up. CS Java. Introduction to Java. Andy Mroczkowski

Lecture 9. Lecture

CS 2113 Software Engineering

Introduction to the JAVA UI classes Advanced HCI IAT351

Java GUI Design: the Basics

CS 251 Intermediate Programming GUIs: Components and Layout

Graphical User Interface (GUI)

DM503 Programming B. Peter Schneider-Kamp.

Datenbank-Praktikum. Universität zu Lübeck Sommersemester 2006 Lecture: Swing. Ho Ngoc Duc 1

2IS45 Programming

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

MIT AITI Swing Event Model Lecture 17

Goals. Lecture 7 More GUI programming. The application. The application D&D 12. CompSci 230: Semester JFrame subclass: ListOWords

Java: Graphical User Interfaces (GUI)

Lecture 5: Java Graphics

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

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

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

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

Graphic User Interfaces. - GUI concepts - Swing - AWT

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming

Parts of a Contract. Contract Example. Interface as a Contract. Wednesday, January 30, 13. Postcondition. Preconditions.

COMP-202 Unit 10: Basics of GUI Programming (Non examinable) (Caveat: Dan is not an expert in GUI programming, so don't take this for gospel :) )

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

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

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

Section Basic graphics

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

CompSci 125 Lecture 17. GUI: Graphics, Check Boxes, Radio Buttons

Event Driven Programming

1 Using the NetBeans IDE

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

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

Graphics User Defined Forms, Part I

KF5008 Program Design & Development. Lecture 1 Usability GUI Design and Implementation

SINGLE EVENT HANDLING

Java Programming Summer 2008 LAB. Thursday 8/21/2008

Prototyping a Swing Interface with the Netbeans IDE GUI Editor

Forte for Java (Community Edition) QuickStart Guide

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!

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

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

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

G51PGP Programming Paradigms. Lecture 009 Concurrency, exceptions

This exam is closed textbook(s) and closed notes. Use of any electronic device (e.g., for computing and/or communicating) is NOT permitted.

Throughout the exam, write concisely and underline key words or phrases. Have fun! Exam 2. Week 7 (Winter 2013). Dr. Yoder. Sec 031.

IT101. Graphical User Interface

Lecture 3: Java Graphics & Events

Assignment 1. Application Development

Final Examination Semester 2 / Year 2010

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

Swing from A to Z Some Simple Components. Preface

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

Assignment 2. Application Development

Commands. Written commands can be reused, cataloged, etc. Sometimes they also contain "undo" commands, too.

UI Software Organization

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

Event Driven Programming

GUI (Graphic User Interface) Programming. Part 2 (Chapter 8) Chapter Goals. Events, Event Sources, and Event Listeners. Listeners

Swing - JLabel. Adding a text (and HTML) labels to a GUI

/** Creates new form NewJFrame */ public NewJFrame() { initcomponents(); initblogsearch(); //initializes Index List box }

Page 1 of 16. 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 (GUIs)

Graphical User Interfaces 2

PIC 20A GUI with swing

Block I Unit 2. Basic Constructs in Java. AOU Beirut Computer Science M301 Block I, unit 2 1

Graphical User Interfaces in Java - SWING

Java Graphical User Interfaces AWT (Abstract Window Toolkit) & Swing

Based on slides by Prof. Burton Ma

Transcription:

Hanley s Survival Guide for Visual Applications with NetBeans 2.0 Last Updated: 5/20/2015 TABLE OF CONTENTS Glossary of Terms 2-4 Step by Step Instructions 4-7 HWApp 8 HWFrame 9 Never trust a computer you can't throw out a window. Steve Wozniak

Overview: Visual applications allow a user to interact using a mouse or keyboard to click on buttons, pull down menus, select radio buttons, tabbed panes and other items. Thanks to Xerox Palo Alto Research Center and Steve Jobs(who saw these ideas at Xerox and brought them to computers!) Building Visual applications is more complex than building console applications, but the rewards are greater also. You should know some terms when dealing with GUIS (Graphical User Interfaces). class: a class is a software blueprint, it is a category of things. It is a cookie cutter for objects. Example JButton object: an object is an instance of a class. It is a running software component that lives inside the memory of a computer. They often have data and actions (methods) that they can perform. Example comparebut is the name of the JButton in the above JFrame that triggers the program to compute and compare the prices of the sodas input. It is an instance of the JButton class. Running programs are made up of objects, not classes. Much like a house is the reality of a blue-print. It is the instanciation. JFrame: a software blueprint that represents a window that the user can interact with JLabel: a phrase or series of characters in a JFrame or Dialog 2

event: something that happens during the run of a program that could be user initiated or system initiated Examples mouseclicked, menuitemselected, timer event, keytyped, keypressed, keyreleased actionlistener: a program that can respond to events private void initcomponents(): a method that is used by visual applications to initialize the window public HellowWordFrame(){ : this method is known as a constructor. Constructors are special methods that are starting points for objects. Each java object is the result of a constructor setbounds(): this method establishes the bounds of a particular graphical element inside the window new JLabel() or new JTextField() or new JButton(): the new command is what calls a constructor and instanciates an object. This is how objects begin occupying the computer s memory ActionEvent: a software blueprint that represents an event that can occur during the running of a program to a JFrame. Examples see above for event String: a variable type that can store any sequence of characters. Examples this is a string yy$%he -----Title---- 789 % double: a primitive variable type that can store numbers which may include decimal parts Examples 3.14159 4000.1-100.67896 16 Double: a software blueprint that allows a programmer to use and format double math values in various ways Double.parseDouble(): this command takes an existing String and attempts to convert it to a double math value. Example Double.parseDouble( 134.78 ) yields 134.78 the numeric value Double.parseDouble( 134ea ) yields an exception (a run time error) int: a primitive variable type that can store numbers which can be integers and cannot have decimal parts Integer: see Double above Integer.parseInt(): see Double.parseDouble() above LayoutManager: a software blueprint that represents a component which manages the placement of visual objects in a window. Example GridLayout places components in a window by starting at the upper left and then working its way across to the end of a row before moving to the next row. Finishes at lower right. null means that all visual objects will be placed absolutely. 3

private void anstfactionperformed(java.awt.event.actionevent evt) { // TODO add your handling code here: method that is called when an event occurs. Also known as an event handler. Gets activated for mouse events, timer events and menu events. Does not get activated for keyboard events! focus: a component that has the focus is active and able to receive key events. Only one component can have the focus at a time. Typical Java Frame (0,0) Title Bar All coordinates in the window are x,y where x and y are relative to the upper left corner of the window. (The title bar usually takes about 30 pixels and each border takes roughly 4 pixels) (width-1, height-1) Rectangle: a software blueprint that manages a rectangle using 4 numbers, the upper left x coord, upper left y coord, the width and the height. new: one of the most important command in Java and C++. This command allows a programmer to create a new software object from a software 4

blueprint. Example JLabel hellolbl = new JLabel(); NOTE: JLabel is a class, hellolbl is an object addactionlistener(): java method that sets up a program to listen for events that are directed at a specific component this: a way of referring to the current object you are dealing with, in our case it will refer to the current frame Swing: a collection of user interface components that were developed to replace some of the original components which really didn t work that well. A typical swing project: HelloWordApp.java this class contains a main method which creates an instance of the HelloWordFrame class. This instance is set to visible and thus controls the running of the application. HelloWorldFrame.java this class extends the JFrame class, which means that it inherits all of the features of the JFrame class. This class contains lots of code. The NetBeans Visual Designer will generate much of the code for you. To create a visual application: 1. Create a new Project within NetBeans (HelloWorld Project) 2. Choose New..File and then select Swing GUI Forms JFrame form 3. Type in the name HellowWorldFrame and choose Finish 4. RIGHT CLICK IN THE MIDDLE OF THE WINDOW AND SET LAYOUT TO NULL!!! 5

5. Drag a JLabel out and set its name to titlelbl by clicking on the Code button on the right hand side and setting the Variable name property 6. Click back on the Properties button to set the Font for this label 7. Keep adding components as needed 8. Double click on your button NetBeans will generate a method for you like; private void hwbutactionperformed(java.awt.event.actionevent evt){ 6

// TODO add your handling code here: 9. Put any logic needed inside your button. You can access global variables, set text fields, change colors, etc. //For example private void hwbutactionperformed(java.awt.event.actionevent evt){ hwtf.settext( Hello World ); 10. Create a new class called HelloWorldApp(see the attached HelloWorldApp.java) 11. Delete the main from your Frame 12. Run your app NOT YOUR FRAME 7

/ --------------------------------------------------------------- S-h-e-n-e-n-d-e-h-o-w-a--H-i-g-h--S-c-h-o-o-l--T-e-c-h--D-e-p-t --------------------------------------------------------------- FILE: HWApp.java DATE: 5/20/2015 PURPOSE: Demonstrate simple app for starting up frame @author mr Hanley @version 1.0 --------------------------------------------------------------- h-a-n-l-e-y.c-o-.-n-r------t-e-a-m-2-0-.-c-o-m----------------- / public class HWApp { public static void main(string[] args) { HWFrame f = new HWFrame(); f.setbounds(100,100,600,400); //upper left x, upper left y, width, height f.setvisible(true); 8

/ --------------------------------------------------------------- S-h-e-n-e-n-d-e-h-o-w-a--H-i-g-h--S-c-h-o-o-l--T-e-c-h--D-e-p-t --------------------------------------------------------------- FILE: HWFrame.java DATE: 5/20/2015 PURPOSE: Demonstrate GUI Building @author mr Hanley @version 1.0 -------------------------------------------------------------- h-a-n-l-e-y.c-o-.-n-r------t-e-a-m-2-0-.-c-o-m---------------- / public class HWFrame extends javax.swing.jframe { / Creates new form HWFrame / public HWFrame() { initcomponents(); / This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this code. The content of this method is always regenerated by the Form Editor. / @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="generated Code"> private void initcomponents() { //THIS CODE IS USUALLY FOLDED -> DO NOT CHANGE THIS STUFF private void hwbutactionperformed(java.awt.event.actionevent evt) { //Students -> Add Handling Code Here hwtf.settext("hola Mundo!!"); //NOTE: I DELETED THE MAIN METHOD THAT NETBEANS GENERATED FOR ME!!! // Variables declaration - do not modify private javax.swing.jbutton hwbut; private javax.swing.jtextfield hwtf; private javax.swing.jlabel titlelbl; // End of variables declaration 9