SampleApp.java. Page 1

Similar documents
COMP16121 Sample Code Lecture 1

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

Graphical User Interface

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

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

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

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.

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

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

Based on slides by Prof. Burton Ma

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

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

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

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

CMP 326 Midterm Fall 2015

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

More about GUIs GEEN163

Example: Building a Java GUI

Example: Building a Java GUI

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.

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

MIT AITI Swing Event Model Lecture 17

DAFTAR LAMPIRAN. Source Code Java Aplikasi Keyword to Image Renamer Split

APPENDIX. public void cekroot() { System.out.println("nilai root : "+root.data); }

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

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

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

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.

CS 209 Programming in Java #10 Exception Handling

Dr. Hikmat A. M. AbdelJaber

Graphical User Interfaces in Java - SWING

// autor igre Ivan Programerska sekcija package mine;

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

Handout 14 Graphical User Interface (GUI) with Swing, Event Handling

Dr. Hikmat A. M. AbdelJaber

JFrame & JLabel. By Iqtidar Ali

AP CS Unit 11: Graphics and Events

import javax.swing.*; import java.awt.*; import java.awt.event.*;

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

CMP 326 Final Spring There is a blank page at the end of the exam if you need more room to answer a question.

RAIK 183H Examination 2 Solution. November 11, 2013

Introduction to the JAVA UI classes Advanced HCI IAT351

Section Basic graphics

SINGLE EVENT HANDLING

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

RAIK 183H Examination 2 Solution. November 10, 2014

Swing - JButton. Adding buttons to the main window

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

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

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

CS 180 Final Exam Review 12/(11, 12)/08

CSIS 10A Assignment 7 SOLUTIONS

Graphical User Interfaces. Comp 152

Solution register itself

Object-Oriented Software Engineering Re-exam, 2012 (Also Object-Oriented Analysis, Design and Programming, Re-exam, 2012)

G51PGP Programming Paradigms. Lecture 008 Inner classes, anonymous classes, Swing worker thread

Graphic User Interfaces. - GUI concepts - Swing - AWT

package As7BattleShip;

/* Write a Program implementing GUI based Calculator using Swing */

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

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

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

Midterm assessment - MAKEUP Fall 2010

AppBisect > PrBisect > class Functie. AppBisect > PrBisect > class Punct. public class Functie { double x(double t) { return t;

GUI Forms and Events, Part II

Abstract, Interface, GUIs. Ch. 11 & 16

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

Introduction. Introduction

H212 Introduction to Software Systems Honors

Java Interfaces Part 1 - Events Version 1.1

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

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

We are on the GUI fast track path

ITEC 120 4/14/11. Review. Need. Objectives. GUI basics JFrame JPanel JLabel, JButton, JTextField Layout managers. Lecture 38 GUI Interactivity

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

class BankFilter implements Filter { public boolean accept(object x) { BankAccount ba = (BankAccount) x; return ba.getbalance() > 1000; } }

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

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

1.00/1.001 Introduction to Computers and Engineering Problem Solving. Quiz 2: April 16, 2004

import java.applet.applet; import java.applet.audioclip; import java.net.url; public class Vjesala2 {

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

The class definition is not a program by itself. It can be used by other programs in order to create objects and use them.

Window Interfaces Using Swing Objects

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

HomeWork 3. In this homework, an ArchJava application that allows one to query one s scores in a networked environment is presented.

COMP16121 Notes on Mock Exam Questions

Graphical User Interfaces

Java Never Ends MULTITHREADING 958 Example: A Nonresponsive GUI 959

PART1: Choose the correct answer and write it on the answer sheet:

Part I: Learn Common Graphics Components

Lecture 9. Lecture

FirstSwingFrame.java Page 1 of 1

Chapter 10 Inheritance. Big Java by Cay Horstmann Copyright 2009 by John Wiley & Sons. All rights reserved.

CSCI 201L Midterm Written Fall % of course grade

PIC 20A GUI with swing

Window Interfaces Using Swing Objects

OOP Assignment V. For example, the scrolling text (moving banner) problem without a thread looks like:

DEMYSTIFYING PROGRAMMING: CHAPTER SIX METHODS (TOC DETAILED) CHAPTER SIX: METHODS 1

Final Examination Semester 2 / Year 2011

Transcription:

SampleApp.java 1 package msoe.se2030.sequence; 2 3 /** 4 * This app creates a UI and processes data 5 * @author hornick 6 */ 7 public class SampleApp { 8 private UserInterface ui; // the UI for this program 9 /** 10 * @param args not used 11 */ 12 public static void main(string[] args) { 13 new SampleApp(); 14 15 16 17 public SampleApp() { ui = new UserInterface(this, "My Sample App"); // create the UI 18 19 20 /** 21 * This method does some processing of data collected from the UI 22 * @param input the string entered 23 * @return nothing 24 */ 25 public void notify( String input ) { 26 27 CalculationHelper helper = new CalculationHelper(); int n = helper.countchars(input); 28 // 29 int n = helper.countdigits(input); 30 31 if( n == -1 ) { // an error occurred ui.displayerror("input is empty or null!"); 32 else { ui.displaynumberofchars(n); // tell the UI to display the result 34 // ui.displaynumberofdigits(n); // tell the UI to display the result 35 36 37 38

1 /** 4 package msoe.se2030.sequence; 5 6 import java.awt.container; 7 import java.awt.dimension; 8 import java.awt.flowlayout; 9 import java.awt.event.actionevent; 10 import java.awt.event.actionlistener; 11 12 import javax.swing.jbutton; 13 import javax.swing.jframe; 14 import javax.swing.jlabel; 15 import javax.swing.jtextfield; 16 17 18 /** 19 * This class creates a UI and processes events 20 * @author hornick 21 */ 22 public class UserInterface { 23 private JFrame ui; // the encapsulated JFrame that displays this UI 24 private SampleApp controller; // the app that creates this UI 25 private JButton button1; // a button on the UI 26 private JLabel output; // a field for displaying results 27 private JTextField input; // a input field for input 28 29 private class ButtonHandler implements ActionListener { 30 31 @Override 32 // respond to button presses by notifying the controller public void actionperformed(actionevent arg0) { 34 35 String s = input.gettext(); // retrieve the input from the JTextField 36 37 38 if( arg0.getsource() == button1) { // the button was pressed... controller.notify( s ); // just tell the app/controller about it; this UI is too dumb to know what to do about it... 39 40 41 42 43 /** 44 * Constructor for the UI 45 * @param c the parent, or controller class that creates this UI 46 * @param t the title of the window 47 */ 48 public UserInterface(SampleApp c, String t) { 49 50 controller = c; ui = new JFrame(t);

Page 2 51 52 ui.setsize(300, 500); Container pane = ui.getcontentpane(); 53 pane.setlayout(new FlowLayout() ); 54 55 JLabel instructions = new JLabel("Enter some characters into the input field and press the button."); 56 pane.add(instructions); 57 58 input = new JTextField(""); // initial value in input field 59 input.setpreferredsize(new Dimension(200, 25)); // need this to set a fixed width; otherwise, the input field will be too small. 60 pane.add(input); 61 62 output = new JLabel(""); // initial output in output field 63 output.setpreferredsize(new Dimension(200, 25)); 64 pane.add(output); 65 66 ButtonHandler handler = new ButtonHandler(); 67 68 button1 = new JButton("Compute!"); 69 pane.add(button1); 70 71 button1.addactionlistener(handler); // listen for button presses 72 73 74 // other code not shown 75 76 ui.pack(); ui.setvisible(true); 77 78 79 80 /** * This method displays the number of characters entered in the input field 81 82 * @param v the value to display */ 83 public void displaynumberofchars( int v ) { 84 85 output.settext("#chars = " + v); 86 87 88 /** * This method displays the number of digits entered in the input field 89 * @param v the value to display 90 */ 91 public void displaynumberofdigits( int v ) { 92 output.settext("#digits = " + v); 93 94 95 /** 96 * This method displays an error message in the output 97 98 * @param msg the message to display */

Page 3 99 public void displayerror( String msg ) { 100 output.settext( msg ); 101 102 103

CalculationHelper.java 1 package msoe.se2030.sequence; 2 3 /** 4 * This class implements methods that analyze a given String 5 * @author hornick 6 */ 7 public class CalculationHelper { 8 public CalculationHelper() { 9 // nothing to initialize 10 11 12 13 /** 14 * This method counts chars in a String 15 * @param s a String 16 * @return number of chars, -1 if the String is empty or null 17 */ 18 public int countchars( String s ) { 19 if( s == null s.isempty() ) // input error check 20 return -1; 21 22 return s.length(); 23 24 25 26 /** 27 * This method counts digits in a String 28 * @param s a String 29 * @return number of digits, -1 if the String is empty or null 30 */ 31 public int countdigits( String s ) { 32 int ndigits = 0; 34 if( s == null s.isempty() ) // input error check 35 return -1; 36 37 for( int index=0; index<s.length(); index++ ) { 38 Character ch = s.charat(index); 39 if( Character.isDigit(ch) ) { 40 ndigits++; 41 42 43 return ndigits; 44 45 46 47