Simple GUI Input. String input Use showinputdialog() of JOptionPane class

Similar documents
CS 335 Lecture 06 Java Programming GUI and Swing

Using A Frame for Output

Mouse Event Handling (cont.)

GUI Components: Part 2

Programming with Java GUI components

The Abstract Windowing Toolkit. Java Foundation Classes. Swing. In April 1997, JavaSoft announced the Java Foundation Classes (JFC).

GUIs with Swing. Principles of Software Construction: Objects, Design, and Concurrency. Jonathan Aldrich and Charlie Garrod Fall 2012

Graphical User Interfaces

INTRODUCTION TO COMPUTER PROGRAMMING. Richard Pierse. Class 7: Object-Oriented Programming. Introduction

5.17 GUI. Xiaoyi Jiang Informatik I Grundlagen der Programmierung

public class Application extends JFrame implements ChangeListener, WindowListener, MouseListener, MouseMotionListener {

How to Convert an Application into an Applet.

The Basic Java Applet and JApplet

GUI Event-Driven Programming

Java Mouse and Keyboard Methods

Swing. A Quick Tutorial on Programming Swing Applications

core 2 Handling Mouse and Keyboard Events

Advanced Network Programming Lab using Java. Angelos Stavrou


Fondamenti di Java. Introduzione alla costruzione di GUI (graphic user interface)

public class Craps extends JFrame implements ActionListener { final int WON = 0,LOST =1, CONTINUE = 2;

How To Build A Swing Program In Java.Java.Netbeans.Netcode.Com (For Windows) (For Linux) (Java) (Javax) (Windows) (Powerpoint) (Netbeans) (Sun) (

Homework/Program #5 Solutions

Principles of Software Construction: Objects, Design and Concurrency. GUIs with Swing. toad Spring 2013

Extending Desktop Applications to the Web

Essentials of the Java(TM) Programming Language, Part 1

Handout 3 cs180 - Programming Fundamentals Spring 15 Page 1 of 6. Handout 3. Strings and String Class. Input/Output with JOptionPane.

Simple Line Drawing. Description of the Simple Line Drawing program

How To Write A Program For The Web In Java (Java)

11. Applets, normal window applications, packaging and sharing your work

// Correntista. //Conta Corrente. package Banco; public class Correntista { String nome, sobrenome; int cpf;

Remote Method Invocation

Callbacks. Callbacks Copyright 2007 by Ken Slonneger 1

CSS 543 Program 3: Online Tic-Tac-Toe Game Professor: Munehiro Fukuda Due date: see the syllabus

ICOM 4015: Advanced Programming

JiST Graphical User Interface Event Viewer. Mark Fong

Java GUI Programming. Building the GUI for the Microsoft Windows Calculator Lecture 2. Des Traynor 2005

Karsten Lentzsch JGoodies SWING WITH STYLE

Event processing in Java: what happens when you click?

Assignment # 2: Design Patterns and GUIs

Essentials of the Java Programming Language

LAYOUT MANAGERS. Layout Managers Page 1. java.lang.object. java.awt.component. java.awt.container. java.awt.window. java.awt.panel

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

How Scala Improved Our Java

CaptainCasa. CaptainCasa Enterprise Client. CaptainCasa Enterprise Client. Feature Overview

JIDE Action Framework Developer Guide

Course/Year W080/807 Expected Solution Subject: Software Development to Question No: 1

Java Interview Questions and Answers

file://c:\dokumente und Einstellungen\Marco Favorito\Desktop\ScanCmds.html

Skills and Topics for TeenCoder: Java Programming

Java applets. SwIG Jing He

@ - Internal # - Online TH PR OR TW TOTAL HOURS # @ 175

Tutorial Reference Manual. Java WireFusion 4.1

Syllabus for CS 134 Java Programming

LAB4 Making Classes and Objects

Object Oriented Programming with Java. School of Computer Science University of KwaZulu-Natal

1 Hour, Closed Notes, Browser open to Java API docs is OK

Here's the code for our first Applet which will display 'I love Java' as a message in a Web page

Construction of classes with classes

Event-Driven Programming

Model-View-Controller (MVC) Design Pattern

Nexawebホワイトペーパー. Developing with Nexaweb ~ Nexaweb to Improve Development Productivity and Maintainability

Create a Poster Using Publisher

Java is commonly used for deploying applications across a network. Compiled Java code

Lab 1A. Create a simple Java application using JBuilder. Part 1: make the simplest Java application Hello World 1. Start Jbuilder. 2.

method is never called because it is automatically called by the window manager. An example of overriding the paint() method in an Applet follows:

CS506 Web Design and Development Solved Online Quiz No. 01

Introduction to Java Applets (Deitel chapter 3)

Java Appletek II. Applet GUI

D06 PROGRAMMING with JAVA

INTRODUCTION TO DESKTOP PUBLISHING

Computing Concepts with Java Essentials

CS 106 Introduction to Computer Science I

TATJA: A Test Automation Tool for Java Applets

Fundamentals of Java Programming

Sample Table. Columns. Column 1 Column 2 Column 3 Row 1 Cell 1 Cell 2 Cell 3 Row 2 Cell 4 Cell 5 Cell 6 Row 3 Cell 7 Cell 8 Cell 9.

Task Force on Technology / EXCEL

Gestation Period as a function of Lifespan

Tutorial: Time Of Day Part 2 GUI Design in NetBeans

Microsoft Word Quick Reference Guide. Union Institute & University

core. Volume I - Fundamentals Seventh Edition Sun Microsystems Press A Prentice Hall Title ULB Darmstadt

How to create buttons and navigation bars

Android Basic XML Layouts

Cours de Java. Sciences-U Lyon. Java - Introduction Java - Fondamentaux Java Avancé.

Interactive Voting System. IVS-Basic IVS-Professional 4.4

java.applet Reference

Part 1 Foundations of object orientation

Excel 2007 Basic knowledge

Fachbereich Informatik und Elektrotechnik Java Swing. Advanced Java. Java Swing Programming. Programming in Java, Helmut Dispert

Using Microsoft Word. Working With Objects

Software Design: Figures

Dev Articles 05/25/07 11:07:33

A TOOL FOR DATA STRUCTURE VISUALIZATION AND USER-DEFINED ALGORITHM ANIMATION

Transcription:

CS1706 Intro to Object Oriented Dev II - Fall 04 Announcements Week 11 Project 2 due 11/01 Project 3 posted in a few days Exam 2 next week Material Java Graphical User Interfaces (GUI)

Simple GUI Input String input Use showinputdialog() of JOptionPane class import javax.swing.joptionpane; public class InputTest public static void main(string[] args) String input = JOptionPane.showInputDialog("How old are you?"); if (input!= null) int age = Integer.parseInt(input); age++; JOptionPane.showMessageDialog(null, "Next year, you'll be " + age); System.exit(0); null indicates the dialog has no parent window

Dialog Icon Message dialog icon Use overloaded showmessagedialog(): import javax.swing.*; public class InputTest public static void main(string[] args) JOptionPane.showMessageDialog( null, //parent window "Kernel Crash, Bummer Man!", //message "System Shutdown", //dialog title JOptionPane.INFORMATION_MESSAGE, //msg type new ImageIcon("biohazard.gif")); //icon System.exit(0);

Icon Interface Icon type is an interface ImageIcon implements Icon interface Method Summary int int geticonheight() Returns the icon's height. geticonwidth() Returns the icon's width. void painticon(component c, Graphics g, int x, int y) Draw the icon at the specified location.

Anonymous Classes Typical in Graphical User Interfaces Lots of interfaces to be implemented Often declared as inner classes Lets see some examples

Anonymous Icon class Anonymous inner class import java.awt.*; import java.awt.geom.*; import javax.swing.*; public class InputTest public static void main(string[] args) JOptionPane.showMessageDialog( null, //parent window "Kernel Crash, Bummer Man!", //message "System Shutdown", //dialog title JOptionPane.INFORMATION_MESSAGE, //msg type new Icon() public int geticonwidth() return size; public int geticonheight() return size; Instantiate anonymous object from an anonymous class

Anonymous Icon class Continued public void painticon(component c, Graphics g, int x, int y) Graphics2D g2 = (Graphics2D) g; Ellipse2D.Double elipse = Graphics object passed by Java is a context for drawing. Actual object passed is a newer more powerful Graphics2D object. // new Ellipse2D.Double(x, y, size, size); g2.setcolor(color.yellow); g2.fill(elipse); //g2.draw(elipse); for outline only Line2D.Double bang = new Line2D.Double(new Point2D.Double(25, 5), new Point2D.Double(25, 35)); Line2D.Double bangbang = new Line2D.Double(new Point2D.Double(25, 40), new Point2D.Double(25, 45)); g2.setcolor(color.red); g2.draw(bang); g2.draw(bangbang); private final int size = 50; ); //icon Interface type cannot contain implementation, but an interface definition can.

Windows Frames - implement windows Frame window has decorations (e.g. title bar, close box, etc.) Frames are provided by the windowing system import javax.swing.*; JFrame frame = new JFrame(); frame.pack(); //minimize frame size to hold compoent //frame.setsize(frame_width, FRAME_HEIGHT); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.show(); Must be set to terminate the spawned process.

Components Objects you add to a window and often provide some specialized behavior (like buttons) Construct components JButton hellobutton = new JButton("Say Hello"); Set content pane layout Container contentpane = frame.getcontentpane(); container.setlayout(new FlowLayout()); Content pane is window area below title bar. Holds window components. Add components to content pane contentpane.add(hellobutton); Content pane layout manager determines positioning of components. FlowLayout aligns components adjacently.

How to handle buttons What should happen when a button is pressed? How should we build code that does some action when the button is pressed? The Java designers did not know what each button would do Hence => User Interface Actions

User Interface Actions By default, program's buttons don't have any effect Attach listener object(s) to button A listener object belongs to class implementing the ActionListener interface type public interface ActionListener int actionperformed(actionevent event); ActionEvent passed by JVM, contains event information (occurrence time, keys pressed, etc.). Not used in many ActionListener objects. Listeners are notified by JVM when button is clicked

Adding Listeners ActionListener objects are usually from anonymous classes: hellobutton.addactionlistener(new ActionListener() public void actionperformed(actionevent event) do something here ); More than just cutesy use of anonymous class Listener (inner class) can access variables in enclosing scope, particularly instance fields Local varibles of enclosing scope must be declared as final variables.

Listener Example Horstmann: ActionTest.java import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ActionTest public static void main(string[] args) JFrame frame = new JFrame(); final int FIELD_WIDTH = 20; final JTextField textfield = new JTextField(FIELD_WIDTH); textfield.settext("click a button!"); JButton hellobutton = new JButton("Say Hello"); hellobutton.addactionlistener( new ActionListener() public void actionperformed(actionevent event) textfield.settext("hello, World!"); );

Listener Eg. -continued Horstmann: ActionTest.java JButton goodbyebutton = new JButton("Say Goodbye"); goodbyebutton.addactionlistener(new ActionListener() public void actionperformed(actionevent event) textfield.settext("goodbye, World!"); ); Container contentpane = frame.getcontentpane(); contentpane.setlayout(new FlowLayout()); contentpane.add(hellobutton); contentpane.add(goodbyebutton); contentpane.add(textfield); After main() exits, window and component objects still exist. show() spawns a thread process the executes until users closes the window. frame.setdefaultcloseoperation(jframe.exit_on_close); frame.pack(); frame.show();

Simple Factory Method Action Listeners with similar actions can be constructed by a factory method: import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ActionTest2 public static void main(string[] args) JFrame frame = new JFrame(); final int FIELD_WIDTH = 20; textfield = new JTextField(FIELD_WIDTH); textfield.settext("click a button!"); JButton hellobutton = new JButton("Say Hello"); Factory (helper) method returns anonymous object of the anonymous class. hellobutton.addactionlistener( creategreetingbuttonlistener("hello, World!")); JButton goodbyebutton = new JButton("Say Goodbye");

Factory Method - continued ActionListener- factory construction goodbyebutton.addactionlistener( creategreetingbuttonlistener("goodbye, World!")); Container contentpane = frame.getcontentpane(); contentpane.setlayout(new FlowLayout()); contentpane.add(hellobutton); contentpane.add(goodbyebutton); contentpane.add(textfield); Factory (helper) method returns anonymous object of the anonymous class. frame.setdefaultcloseoperation(jframe.exit_on_close); frame.pack(); frame.show();

Factory Method - continued ActionListener- factory construction method public static ActionListener creategreetingbuttonlistener( final String message) return new ActionListener() ; The parameter to the Factory (helper) method must be declared as final to be able to access it in the anonymous class. public void actionperformed(actionevent event) textfield.settext(message); private static JTextField textfield;

Layout Managers in java User interfaces are made up of components Components are placed in containers Container needs to arrange the components Swing doesn't use hard-coded pixel coordinates Advantages: Can switch "look and feel" Can internationalize strings Layout manager controls arrangement

Layout Managers FlowLayout: left to right, start new row when full BoxLayout: left to right or top to bottom (no new rows or columns) BorderLayout: 5 areas North East Center West South GridLayout: rectangular grid, all components have same size GridBagLayout: complex, like HTML table

Layout examples

BorderLayout Example BorderLayout Code import java.awt.*; import javax.swing.*; public class BorderLayoutEg public static void main(string[] args) JFrame frame = new JFrame(); Container contentpane = frame.getcontentpane(); //contentpane.setlayout(new BorderLayout()); Button centerbtn; Button northbtn; Button westbtn; Button eastbtn; Button southbtn; BorderLayout is the default layout manager of the content pane of every JFrame. centerbtn = new Button("Center Button"); northbtn = new Button("North Button"); westbtn = new Button("West Button"); eastbtn = new Button("East Button"); southbtn = new Button("South Button");

BorderLayout Example BorderLayout Code (continued) // always says where the component should be placed when adding // Options are center,east,west,north and South contentpane.add(centerbtn); //,"Center" contentpane.add(northbtn,"north"); contentpane.add(westbtn,"west"); contentpane.add(eastbtn,"east"); contentpane.add(southbtn,"south"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.pack(); frame.show(); The string add parameter determines the placement of the object, default is,"center" Not all areas must be occupied.

Mouse related Listeners Two interfaces are mouse event handlers MouseListener public void mouseclicked(mouseevent arg0) public void mousepressed(mouseevent arg0) public void mousereleased(mouseevent arg0) public void mouseentered(mouseevent arg0) public void mouseexited(mouseevent arg0) MouseMotionListener public void mousedragged(mouseevent arg0) public void mousemoved(mouseevent arg0) Listens for mouse clicks. Listens for mouse movement. Expensive execution. Invoked every time the user moves the mouse.

MouseEvent Class Mouse events occur in a component only if mouse cursor is over the un-obscured part of the component's bounds when the action occurs. Method Summary int int static String Point int int boolean String getbutton() Returns which, if any, of the mouse buttons has changed state. getclickcount() Returns the number of mouse clicks associated with this event. getmousemodifierstext(int modifiers) Returns a String describing the modifier keys and mouse buttons that were down during the event, such as "Shift", or "Ctrl+Shift". getpoint() Returns the x,y position of the event relative to the source component. getx() Returns the horizontal x position of the event relative to the source component. gety() Returns the vertical y position of the event relative to the source component. ispopuptrigger() Returns whether or not this mouse event is the popup menu trigger event for the platform. paramstring() Returns a parameter string identifying this event. void translatepoint(int x, int y) Translates the event's coordinates to a new position by adding specified x (horizontal) and y (vertical) offsets.

MouseAdapters What if you wanted to implement only the mouseclick part of a mouse listener? MouseAdapter looks like this public class MouseAdapter implements MouseListener public void mouseclicked(mouseevent event) public void mousepressed(mouseevent event) public void mousereleased(mouseevent event) public void mouseentered(mouseevent event) public void mouseexited(mouseevent event) Component adds the listener like this: addmouselistener(new MouseAdapter() public void mousepressed(mouseevent event) mouse action goes here ); Analogous MouseMotionAdapter class implements MouseMotionListener with do nothing methods. Anonymous object of new MouseAdapter class implicitly extends MouseAdapter. Only necessary methods are over-ridden.

Simple MouseEvent Example Bang! Icon -> BioHazard gif when clicked import java.awt.*; import java.awt.geom.*; import java.awt.event.*; import javax.swing.*; public class BangBioHaz public static void main(string[] args) JFrame frame = new JFrame(); frame.setdefaultcloseoperation(jframe.exit_on_close); Container contentpane = frame.getcontentpane(); contentpane.add(new WarnPanel()); frame.setsize(frame_width, FRAME_HEIGHT); frame.show(); private static final int FRAME_WIDTH = 200; private static final int FRAME_HEIGHT = 200;

BangShape Class BangShape constructor import java.awt.*; import java.awt.geom.*; public class BangShape /** Constructs a Bang shape! @param x the left of the bounding rectangle @param y the top of the bounding rectangle @param awidth the width of the bounding rectangle */ public BangShape(int x, int y, int awidth) this.x = x; this.y = y; width = awidth;

BangShape Class BangShape drawing public void draw(graphics2d g2) Ellipse2D.Double elipse = new Ellipse2D.Double(x, y, width, width); g2.setcolor(color.yellow); g2.fill(elipse); //g2.draw(elipse); for outline only Line2D.Double bang = new Line2D.Double( new Point2D.Double(width/2, 5), new Point2D.Double(width/2, width-15)); Line2D.Double bangbang = new Line2D.Double( new Point2D.Double(width/2, width-10), new Point2D.Double(width/2, width-5)); g2.setcolor(color.red); g2.draw(bang); g2.draw(bangbang);

BangShape Class BangShape helper & instance fields public boolean contains(point2d p) return x <= p.getx() && p.getx() <= x + width && y <= p.gety() && p.gety() <= y + width; private int x; private int y; private int width;

WarnPanel Class WarnPanel inherits from JPanel import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import javax.swing.*; import java.util.*; public class WarnPanel extends JPanel public WarnPanel() final WarnPanel wpanel = this; bangimage = true; mousepoint = null; //save for inner class //initial Bang! display bang = new BangShape(0, 0, 50); bioicon = new ImageIcon("biohazard.gif"); //attach mouselistener addmouselistener(new MouseAdapter() MouseAdapter class implements MouseListener with do nothing methods.

WarnPanel Class WarnPanel drawing public void mouseclicked(mouseevent event) mousepoint = event.getpoint(); String strimage = null; strimage = (bangimage)? "Bang!" : "Biohazard."; if (bangimage) if (bang.contains(mousepoint)) bangimage =!bangimage; repaint(); else JOptionPane.showMessageDialog(wPanel, "Click on " + strimage); else //biohazard displayed if (!bang.contains(mousepoint)) repaint() inherited from JPanel (which bangimage =!bangimage; inherited it from java.awt.component). repaint(); Results in a call to this component's paint method. else JOptionPane.showMessageDialog(wPanel, "Click on " + strimage); );//mouseadapter //WarnPanel()

WarnPanel Class WarnPanel drawing public void paintcomponent(graphics g) super.paintcomponent(g); Graphics2D g2 = (Graphics2D) g; if (bangimage) bang.draw(g2); else bioicon.painticon(this, g, 20, 20); paintcomponent() must call superclass paintcomponent() to correctly draw the panel background before doing any drawing. private BangShape bang; private Point mousepoint; private boolean bangimage; private ImageIcon bioicon; this JPanel extends JComponent which is the drawing component needed by painticon.