Java's Graphical User Interface Toolkit

Slides:



Advertisements
Similar presentations
CS18000: Problem Solving and Object-Oriented Programming.
Advertisements

Fall 2007CS 225 Graphical User Interfaces Event Handling Appendix C.
Event-Driven Programming Thus far, our programs have been executed one statement after the other However, many programs depend on user actions to dictate.
Slides prepared by Rose Williams, Binghamton University Mouse Events + Chapter 17: Applets.
Java GUI Libraries Swing Programming. Swing Components Swing is a collection of libraries that contains primitive widgets or controls used for designing.
Slides prepared by Rose Williams, Binghamton University Chapter 18 Applets.
GUI and Event-Driven Programming Recitation – 3/6/2009 CS 180 Department of Computer Science, Purdue University.
Creating a GUI with Swing. Introduction Very useful link: Swing – is a part of JFC (Java Foundation.
Applets. What is an Applet?  According to Sun “An applet is a small program that is intended not to be run on its own, but rather to be embedded inside.
Scott Grissom, copyright 2006Ch 11: GUI Slide 1 Graphical User Interfaces (Ch 11) Careful design of a graphical user interface is key to a viable software.
Unit 11 Object-oriented programming: Graphical user interface Jin Sa.
CPSC150 Week 12 Graphical User Interfaces Chapter 11.
PROGRAMMING REVIEW Lab 2 EECS 448 Dr Fengjun Li and Meenakshi Mishra.
Chapter 8: Graphical User Interfaces Objectives - by the end of this chapter, you should be able to do the following: –write a simple graphical user interface.
Chapter 9: Applets Jim Burns Fall Outline Learn about applets Learn about applets Write an HTML doc to host an applet Write an HTML doc to host.
Introduction to GUI Java offers a great number of pre-defined classes to support the development of graphical user interfaces –These are broken down into.
OOP (Java): Layout/ OOP Objectives – –describe the basic layout managers for GUIs Semester 2, GUI Layout.
1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
JAPPLET.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
CSE 219 Computer Science III Graphical User Interface.
MIT AITI 2003 Lecture 17. Swing - Part II. The Java Event Model Up until now, we have focused on GUI's to present information (with one exception) Up.
Java GUI CSCE 190 – Java Instructor: Joel Gompert Mon, July 26, 2004.
GUI programming Graphical user interface-based programming.
Java GUI building with Swing. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
Introduction to Swing Components Chapter 14.  Part of the Java Foundation Classes (JFC)  Provides a rich set of GUI components  Used to create a Java.
1 Why layout managers Can we perform layout without them? –Yes. A container’s layout property can be set to null. Absolute positioning: specify size and.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
MSc Workshop - © S. Kamin, U. ReddyLect 3 - GUI -1 Lecture 3 - Graphical User Interfaces r GUI toolkits in Java API r JFrame r GUI components.
CompSci 100E 35.1 Graphical User Interfaces: GUIs  Components  Flat Layouts  Hierarchical Layouts  Designing a GUI  Coding a GUI.
Applets and Frames. Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI Slide 2 Applets Usually.
Lesson 39: More wrapup on GUIs. 1.This presentation will focus on the decisions around software architecture and the decisions that will drive the code.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
 GUI – Graphic User Interface  Up to now in the programs we have written all output has been sent to the standard output device i.e.: the DOS console.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
1 GUIs, Layout, Drawing Rick Mercer. 2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces (GUIs)
Creating Applets. What is an applet? What is an applet? A Java program that runs in a web browser. A Java program that runs in a web browser. An applet.
1 GUI programming Graphical user interface-based programming Chapter G1 (pages )
1 Applets. 2 Design of Applets 3 Sun wrote Java to be executable within a hosting application browser The applications are applets. An applet is downloaded.
1 Applications & Applets Standalone applications & Java applets Peter Mozelius DSV/UCSC.
Computer Science 209 GUIs Model/View/Controller Layouts.
1 Chapter 16: Creating Windows. 2 Basics of GUI Programming How to create a Window. TryWindow.java You always need a JFrame component before creating.
Creating a Window. A basic window in Java is represented by an object of the class Window in the package java.awt.
Event-Driven Programming CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
1 Event Driven Programs with a Graphical User Interface Rick Mercer.
1 Layout Managers Layout managers –Provided for arranging GUI components –Provide basic layout capabilities –Processes layout details –Programmer can concentrate.
Event Listeners ActionListener –button,list AdjustmentListener-scroll bar ComponentListener-when component hidden…. ContainerListener-comp added or removed.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7-3 ( Book Chapter 14) GUI and Event-Driven Programming.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Graphical User Interface (GUI)
Menus and Toolbars CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Applets. 9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L12: Applets Slide 2 Applets Usually.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
Dept. of CSIE, National University of Tainan 10/21/2012 Arranging Components on a User Interface.
Chapter 14 Applets. Applets: Introduction Java programs are divided into two main categories, applets and applications An application is an ordinary Java.
Dept. of CSIE, National University of Tainan 10/21/2012 Responding to User Input.
GUI.1 Graphical User Interfaces GUIs. GUI.2 The Plan Components Flat Layouts Hierarchical Layouts Designing a GUI Coding a GUI.
Chapter 6 Building Java GUIs. MVC Model View Controller The model passes its data to the view for rendering The view determines which events are passed.
GUIs and Events Rick Mercer.
Java Applet What is a Java Applet? How is applet compiled?
Java Applets.
Object-Orientated Analysis, Design and Programming
Graphical User Interfaces
Graphical User Interfaces -- GUIs
GUIs Model/View/Controller Layouts
Course Outcomes of Advanced Java Programming AJP (17625, C603)
Graphical user interface-based programming
Java Applets.
Steps to Creating a GUI Interface
Presentation transcript:

Java's Graphical User Interface Toolkit Windows, Applets and all that Swings.

What is Swing? A Swing Tour

The Agenda A Tour of Swing Display A Simple Window from main() Display A Window from an Object Display an Object that is a Window Display an Applet Object Laying out Display Components Making Buttons and Stuff Work

Starting with an Example The Basic Window

The Basic JFrame import javax.swing.*; class BasicJFrame { public static void main(String[] args) { JFrame jf = new JFrame("The Basic JFrame"); jf.setSize(250,250); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.setVisible(true); }

The Basic JFrame import javax.swing.*; class BasicJFrame { public static void main(String[] args) { JFrame jf = new JFrame("The Basic JFrame"); jf.setSize(250,250); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.setVisible(true); }

jf = new JFrame("The Basic JFrame") Setting the title with the constructor.

jf.setSize(250, 250) 250 pixels 250 pixels

jf.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE); Causes the thread for the JFrame to terminate when window closed.

jf.setVisible(true);

Adding The JPanel import javax.swing.*; class DoJPanel { public static void main(String[] args) { JFrame jf = new JFrame("The Basic JFrame"); jf.setSize(250,250); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel jp = new JPanel(); jf.getContentPane().add(jp); jf.setVisible(true); } import javax.swing.*; class DoJPanel { public static void main(String[] args) { JFrame jf = new JFrame("The Basic JFrame"); jf.setSize(250,250); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel jp = new JPanel(); jf.getContentPane().add(jp); jf.setVisible(true); } import javax.swing.*; class BasicJFrame { public static void main(String[] args) { JFrame jf = new JFrame("The Basic JFrame"); jf.setSize(250,250); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.setVisible(true); }

jf.getContentPane()

Jpanel jp = new Jpanel(); jf.getContentPane().add(jp);

Top Level Containers JFrame JDialog JApplet JWindow JInternalFrame

How do I Build a Display Object? An Object That Displays a Window

MyWindow public class DoMyWindow { public static void main(String[] args) { new MyWindow(); } public class MyWindow { • }

MyWindow Required but not shown. import javax.swing.*; public class MyWindow { } Required but not shown. import javax.swing.*; import java.awt.event.*;

objects and their references MyWindow public class MyWindow { MyWindow() { } Create all the new GUI objects and their references as members here. Assemble the GUI objects in the constructor.

MyWindow public class MyWindow { JFrame jf = new JFrame("MyWindow"); }

MyWindow public class MyWindow { JFrame jf = new JFrame("MyWindow"); jf.setVisible(true); }

MyWindow public class MyWindow { JFrame jf = new JFrame("MyWindow"); jf.setSize(500, 100); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.setVisible(true); }

MyWindow public class MyWindow { JFrame jf = new JFrame("MyWindow"); JMenuBar jmb = new JMenuBar(); MyWindow() { jf.setSize(500, 100); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.setVisible(true); }

MyWindow public class MyWindow { JFrame jf = new JFrame("MyWindow"); JMenuBar jmb = new JMenuBar(); MyWindow() { jf.setSize(500, 100); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.setJMenuBar(jmb); jf.setVisible(true); }

MyWindow public class MyWindow { JFrame jf = new JFrame("MyWindow"); JMenuBar jmb = new JMenuBar(); JMenu jm = new JMenu("File"); MyWindow() { jf.setSize(500, 100); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.setJMenuBar(jmb); jf.setVisible(true); }

MyWindow public class MyWindow { JFrame jf = new JFrame("MyWindow"); JMenuBar jmb = new JMenuBar(); JMenu jm = new JMenu("File"); MyWindow() { jf.setSize(500, 100); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.setJMenuBar(jmb); jmb.add(jm); jf.setVisible(true); }

MyWindow public class MyWindow { JFrame jf = new JFrame("MyWindow"); JMenuBar jmb = new JMenuBar(); JMenu jm = new JMenu("File"); JMenuItem jmi = new JMenuItem("Print Something Cute"); MyWindow() { jf.setSize(500, 100); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.setJMenuBar(jmb); jmb.add(jm); jf.setVisible(true); }

MyWindow public class MyWindow { JFrame jf = new JFrame("MyWindow"); JMenuBar jmb = new JMenuBar(); JMenu jm = new JMenu("File"); JMenuItem jmi = new JMenuItem("Print Something Cute"); MyWindow() { jf.setSize(500, 100); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.setJMenuBar(jmb); jmb.add(jm); jm.add(jmi); jf.setVisible(true); }

MyWindow public class MyWindow { JFrame jf = new JFrame("MyWindow"); JMenuBar jmb = new JMenuBar(); JMenu jm = new JMenu("File"); JMenuItem jmi = new JMenuItem("Print Something Cute"); JPanel jp = new JPanel(); MyWindow() { jf.setSize(500, 100); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.setJMenuBar(jmb); jmb.add(jm); jm.add(jmi); jf.setVisible(true); }

MyWindow public class MyWindow { JFrame jf = new JFrame("MyWindow"); JMenuBar jmb = new JMenuBar(); JMenu jm = new JMenu("File"); JMenuItem jmi = new JMenuItem("Print Something Cute"); JPanel jp = new JPanel(); MyWindow() { jf.setSize(500, 100); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.setJMenuBar(jmb); jmb.add(jm); jm.add(jmi); jf.getContentPane().add(jp); jf.setVisible(true); }

MyWindow public class MyWindow { JFrame jf = new JFrame("MyWindow"); JMenuBar jmb = new JMenuBar(); JMenu jm = new JMenu("File"); JMenuItem jmi = new JMenuItem("Print Something Cute"); JPanel jp = new JPanel(); JTextField jtf = new JTextField("Try the File Menu"); MyWindow() { jf.setSize(500, 100); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.setJMenuBar(jmb); jmb.add(jm); jm.add(jmi); jf.getContentPane().add(jp); jf.setVisible(true); }

MyWindow public class MyWindow { JFrame jf = new JFrame("MyWindow"); JMenuBar jmb = new JMenuBar(); JMenu jm = new JMenu("File"); JMenuItem jmi = new JMenuItem("Print Something Cute"); JPanel jp = new JPanel(); JTextField jtf = new JTextField("Try the File Menu"); MyWindow() { jf.setSize(500, 100); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.setJMenuBar(jmb); jmb.add(jm); jm.add(jmi); jf.getContentPane().add(jp); jp.add(jtf); jf.setVisible(true); }

MyWindow public class MyWindow { JFrame jf = new JFrame("MyWindow"); JMenuBar jmb = new JMenuBar(); JMenu jm = new JMenu("File"); JMenuItem jmi = new JMenuItem("Print Something Cute"); JPanel jp = new JPanel(); JTextField jtf = new JTextField("Try the File Menu"); MyWindow() { jf.setSize(500, 100); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.setJMenuBar(jmb); jmb.add(jm); jm.add(jmi); jf.getContentPane().add(jp); jp.add(jtf); jf.setVisible(true); }

MyWindow public class MyWindow { JFrame jf = new JFrame("MyWindow"); JMenuBar jmb = new JMenuBar(); JMenu jm = new JMenu("File"); JMenuItem jmi = new JMenuItem("Print Something Cute"); JPanel jp = new JPanel(); JTextField jtf = new JTextField("Try the File Menu"); class DoIt implements ActionListener { public void actionPerformed(ActionEvent ae) { System.out.println("Something Cute"); } MyWindow() { jf.setSize(500, 100); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.setJMenuBar(jmb); jmb.add(jm); jm.add(jmi); jf.getContentPane().add(jp); jp.add(jtf); jf.setVisible(true);

MyWindow public class MyWindow { JFrame jf = new JFrame("MyWindow"); JMenuBar jmb = new JMenuBar(); JMenu jm = new JMenu("File"); JMenuItem jmi = new JMenuItem("Print Something Cute"); JPanel jp = new JPanel(); JTextField jtf = new JTextField("Try the File Menu"); class DoIt implements ActionListener { public void actionPerformed(ActionEvent ae) { System.out.println("Something Cute"); } MyWindow() { jf.setSize(500, 100); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.setJMenuBar(jmb); jmb.add(jm); jm.add(jmi); jmi.addActionListener(new DoIt()); jf.getContentPane().add(jp); jp.add(jtf); jf.setVisible(true);

MyWindow public class MyWindow { JFrame jf = new JFrame("MyWindow"); JMenuBar jmb = new JMenuBar(); JMenu jm = new JMenu("File"); JMenuItem jmi = new JMenuItem("Print Something Cute"); JPanel jp = new JPanel(); JTextField jtf = new JTextField("Try the File Menu"); int count = 0; class DoIt implements ActionListener { public void actionPerformed(ActionEvent ae) { System.out.println("Something Cute"); jtf.setText("Count = " + ++count); } MyWindow() { jf.setSize(500, 100); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.setJMenuBar(jmb); jmb.add(jm); jm.add(jmi); jmi.addActionListener(new DoIt()); jf.getContentPane().add(jp); jp.add(jtf); jf.setVisible(true);

How Do I Make an Object Visible? Making an Object Display Itself

A Change in Structure MyWindow MyWindow2 Creates a JFrame Object Fills it With Other GUI Objects Displays the Finished JFrame MyWindow2 Is a JFrame Object Fill Itself With Other GUI Objects MyWindow Displays Itself

MyWindow2 public class DoMyWindow2 { public static void main(String[] args) { new MyWindow2(); } public class MyWindow2 { • }

MyWindow2 public class MyWindow2 extends JFrame { //JFrame jf = new JFrame("MyWindow2"); JMenuBar jmb = new JMenuBar(); JMenu jm = new JMenu("File"); JMenuItem jmi = new JMenuItem("Print Something Cute"); JPanel jp = new JPanel(); JTextField jtf = new JTextField("Try the File Menu"); int count = 0; class DoIt implements ActionListener { public void actionPerformed(ActionEvent ae) { System.out.println("Something Cute"); jtf.setText("Count = " + ++count); } MyWindow2() { setSize(500, 100); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setJMenuBar(jmb); jmb.add(jm); jm.add(jmi); jmi.addActionListener(new DoIt()); getContentPane().add(jp); jp.add(jtf); setVisible(true); public class MyWindow2 extends JFrame { //JFrame jf = new JFrame("MyWindow2"); JMenuBar jmb = new JMenuBar(); JMenu jm = new JMenu("File"); JMenuItem jmi = new JMenuItem("Print Something Cute"); JPanel jp = new JPanel(); JTextField jtf = new JTextField("Try the File Menu"); int count = 0; class DoIt implements ActionListener { public void actionPerformed(ActionEvent ae) { System.out.println("Something Cute"); jtf.setText("Count = " + ++count); } MyWindow2() { jf.setSize(500, 100); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.setJMenuBar(jmb); jmb.add(jm); jm.add(jmi); jmi.addActionListener(new DoIt()); jf.getContentPane().add(jp); jp.add(jtf); jf.setVisible(true); public class MyWindow2 extends JFrame { //JFrame jf = new JFrame("MyWindow2"); JMenuBar jmb = new JMenuBar(); JMenu jm = new JMenu("File"); JMenuItem jmi = new JMenuItem("Print Something Cute"); JPanel jp = new JPanel(); JTextField jtf = new JTextField("Try the File Menu"); int count = 0; class DoIt implements ActionListener { public void actionPerformed(ActionEvent ae) { System.out.println("Something Cute"); jtf.setText("Count = " + ++count); } MyWindow2() { jf.setSize(500, 100); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.setJMenuBar(jmb); jmb.add(jm); jm.add(jmi); jmi.addActionListener(new DoIt()); jf.getContentPane().add(jp); jp.add(jtf); jf.setVisible(true); public class MyWindow2 { JFrame jf = new JFrame("MyWindow2"); JMenuBar jmb = new JMenuBar(); JMenu jm = new JMenu("File"); JMenuItem jmi = new JMenuItem("Print Something Cute"); JPanel jp = new JPanel(); JTextField jtf = new JTextField("Try the File Menu"); int count = 0; class DoIt implements ActionListener { public void actionPerformed(ActionEvent ae) { System.out.println("Something Cute"); jtf.setText("Count = " + ++count); } MyWindow2() { jf.setSize(500, 100); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.setJMenuBar(jmb); jmb.add(jm); jm.add(jmi); jmi.addActionListener(new DoIt()); jf.getContentPane().add(jp); jp.add(jtf); jf.setVisible(true); public class MyWindow2 { //JFrame jf = new JFrame("MyWindow2"); JMenuBar jmb = new JMenuBar(); JMenu jm = new JMenu("File"); JMenuItem jmi = new JMenuItem("Print Something Cute"); JPanel jp = new JPanel(); JTextField jtf = new JTextField("Try the File Menu"); int count = 0; class DoIt implements ActionListener { public void actionPerformed(ActionEvent ae) { System.out.println("Something Cute"); jtf.setText("Count = " + ++count); } MyWindow2() { jf.setSize(500, 100); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.setJMenuBar(jmb); jmb.add(jm); jm.add(jmi); jmi.addActionListener(new DoIt()); jf.getContentPane().add(jp); jp.add(jtf); jf.setVisible(true); public class MyWindow2 { JFrame jf = new JFrame("MyWindow2"); JMenuBar jmb = new JMenuBar(); JMenu jm = new JMenu("File"); JMenuItem jmi = new JMenuItem("Print Something Cute"); JPanel jp = new JPanel(); JTextField jtf = new JTextField("Try the File Menu"); int count = 0; class DoIt implements ActionListener { public void actionPerformed(ActionEvent ae) { System.out.println("Something Cute"); jtf.setText("Count = " + ++count); } MyWindow2() { jf.setSize(500, 100); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.setJMenuBar(jmb); jmb.add(jm); jm.add(jmi); jmi.addActionListener(new DoIt()); jf.getContentPane().add(jp); jp.add(jtf); jf.setVisible(true);

How Do I Display an Object On the Web? Building an Applet

What the Browser Expects in an Applet public void init() public void start() public void stop() public void destroy() First (one) Time Initialization Called when the Applet becomes visible Called when the Applet becomes hidden Called when Applet is unloaded

MyWindow2 SimpleApplet Required but not shown. import javax.swing.*; public class SimpleApplet extends JApplet { //JFrame jf = new JFrame("MyWindow2"); JMenuBar jmb = new JMenuBar(); JMenu jm = new JMenu("File"); JMenuItem jmi = new JMenuItem("Print Something Cute"); JPanel jp = new JPanel(); JTextField jtf = new JTextField("Try the File Menu"); int count = 0; class DoIt implements ActionListener { public void actionPerformed(ActionEvent ae) { System.out.println("Something Cute"); jtf.setText("Count = " + ++count); } public void init() { //setSize(500, 100); //setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setJMenuBar(jmb); jmb.add(jm); jm.add(jmi); jmi.addActionListener(new DoIt()); getContentPane().add(jp); add(jtf); //setVisible(true); public class SimpleApplet extends JApplet { //JFrame jf = new JFrame("MyWindow2"); JMenuBar jmb = new JMenuBar(); JMenu jm = new JMenu("File"); JMenuItem jmi = new JMenuItem("Print Something Cute"); JPanel jp = new JPanel(); JTextField jtf = new JTextField("Try the File Menu"); int count = 0; class DoIt implements ActionListener { public void actionPerformed(ActionEvent ae) { System.out.println("Something Cute"); jtf.setText("Count = " + ++count); } public void init() { //setSize(500, 100); //setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setJMenuBar(jmb); jmb.add(jm); jm.add(jmi); jmi.addActionListener(new DoIt()); getContentPane().add(jp); add(jtf); setVisible(true); public class SimpleApplet extends JApplet { //JFrame jf = new JFrame("MyWindow2"); JMenuBar jmb = new JMenuBar(); JMenu jm = new JMenu("File"); JMenuItem jmi = new JMenuItem("Print Something Cute"); JPanel jp = new JPanel(); JTextField jtf = new JTextField("Try the File Menu"); int count = 0; class DoIt implements ActionListener { public void actionPerformed(ActionEvent ae) { System.out.println("Something Cute"); jtf.setText("Count = " + ++count); } public void init() { //setSize(500, 100); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setJMenuBar(jmb); jmb.add(jm); jm.add(jmi); jmi.addActionListener(new DoIt()); getContentPane().add(jp); add(jtf); setVisible(true); public class SimpleApplet extends JFrame { //JFrame jf = new JFrame("MyWindow2"); JMenuBar jmb = new JMenuBar(); JMenu jm = new JMenu("File"); JMenuItem jmi = new JMenuItem("Print Something Cute"); JPanel jp = new JPanel(); JTextField jtf = new JTextField("Try the File Menu"); int count = 0; class DoIt implements ActionListener { public void actionPerformed(ActionEvent ae) { System.out.println("Something Cute"); jtf.setText("Count = " + ++count); } MyWindow2() { setSize(500, 100); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setJMenuBar(jmb); jmb.add(jm); jm.add(jmi); jmi.addActionListener(new DoIt()); getContentPane().add(jp); add(jtf); setVisible(true); public class MyWindow2 extends JFrame { //JFrame jf = new JFrame("MyWindow2"); JMenuBar jmb = new JMenuBar(); JMenu jm = new JMenu("File"); JMenuItem jmi = new JMenuItem("Print Something Cute"); JPanel jp = new JPanel(); JTextField jtf = new JTextField("Try the File Menu"); int count = 0; class DoIt implements ActionListener { public void actionPerformed(ActionEvent ae) { System.out.println("Something Cute"); jtf.setText("Count = " + ++count); } MyWindow2() { setSize(500, 100); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setJMenuBar(jmb); jmb.add(jm); jm.add(jmi); jmi.addActionListener(new DoIt()); getContentPane().add(jp); add(jtf); setVisible(true); public class SimpleApplet extends JApplet { //JFrame jf = new JFrame("MyWindow2"); JMenuBar jmb = new JMenuBar(); JMenu jm = new JMenu("File"); JMenuItem jmi = new JMenuItem("Print Something Cute"); JPanel jp = new JPanel(); JTextField jtf = new JTextField("Try the File Menu"); int count = 0; class DoIt implements ActionListener { public void actionPerformed(ActionEvent ae) { System.out.println("Something Cute"); jtf.setText("Count = " + ++count); } MyWindow2() { setSize(500, 100); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setJMenuBar(jmb); jmb.add(jm); jm.add(jmi); jmi.addActionListener(new DoIt()); getContentPane().add(jp); add(jtf); setVisible(true); public class SimpleApplet extends JApplet { //JFrame jf = new JFrame("MyWindow2"); JMenuBar jmb = new JMenuBar(); JMenu jm = new JMenu("File"); JMenuItem jmi = new JMenuItem("Print Something Cute"); JPanel jp = new JPanel(); JTextField jtf = new JTextField("Try the File Menu"); int count = 0; class DoIt implements ActionListener { public void actionPerformed(ActionEvent ae) { System.out.println("Something Cute"); jtf.setText("Count = " + ++count); } public void init() { setSize(500, 100); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setJMenuBar(jmb); jmb.add(jm); jm.add(jmi); jmi.addActionListener(new DoIt()); getContentPane().add(jp); add(jtf); setVisible(true); Required but not shown. import javax.swing.*; import java.awt.event.*;

Running The Simple Applet Run the Applet in a JFrame Run the Applet in the Appletviewer Build appropriate html file C:\Chap13>appletviewer SimpleApplet.html Run the Applet in a Web Browser Open html file with Browser

MyWindow2 public class DoMyWindow2 { public static void main(String[] args) { new MyWindow2(); } import javax.swing.*; import java.awt.event.*; public class SimpleApplet extends JApplet { • }

An Applet Display Case import javax.swing.*; public class AppletDisplayCase { public static void main(String[] args) { JFrame jf = new JFrame("Applet Display Case"); jf.setSize(500, 100); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JApplet app = new SimpleApplet(); jf.getContentPane().add(app); app.init(); app.start(); jf.setVisible(true); }

Running The Simple Applet Run the Applet in a JFrame Run the Applet in the Appletviewer Build appropriate html file C:\Chap13>appletviewer SimpleApplet.html Run the Applet in a Web Browser Open html file with Browser

VerySimpleApplet.html <html><head><title>SimpleApplet</title></head> <H3>Demonstrating SimpleApplet</H3> <hr> <APPLET code="SimpleApplet.class" width="500" height="100" align="baseline" codebase="." > No Java 2 support for APPLET!! </APPLET> <hr></body></html>

<html><head><title>SimpleApplet</title></head> <H3>Demonstrating SimpleApplet</H3> <hr> <OBJECT classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93" width="500" height="100" align="baseline" codebase="http://java.sun.com/products/plugin/1.3/ jinstall-13-win32.cab#Version=1,3,0,0"> <PARAM NAME="code" VALUE="SimpleApplet.class"> <PARAM NAME="codebase" VALUE="."> <PARAM NAME="type" VALUE="application/x-java-applet;version=1.3"> <COMMENT> <EMBED type= "application/x-java-applet;version=1.3" code="SimpleApplet.class" codebase="." pluginspage="http://java.sun.com/products/plugin/1.3/ plugin-install.html"> <NOEMBED> </COMMENT> No Java 2 support for APPLET!! </NOEMBED> </EMBED> </OBJECT> <hr></body></html> <html><head><title>SimpleApplet</title></head> <H3>Demonstrating SimpleApplet</H3> <hr> <OBJECT classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93" width="500" height="100" align="baseline" codebase="http://java.sun.com/products/plugin/1.3/ jinstall-13-win32.cab#Version=1,3,0,0"> <PARAM NAME="code" VALUE="SimpleApplet.class"> <PARAM NAME="codebase" VALUE="."> <PARAM NAME="type" VALUE="application/x-java-applet;version=1.3"> <COMMENT> <EMBED type= "application/x-java-applet;version=1.3" code="SimpleApplet.class" codebase="." pluginspage="http://java.sun.com/products/plugin/1.3/ plugin-install.html"> <NOEMBED> </COMMENT> No Java 2 support for APPLET!! </NOEMBED> </EMBED> </OBJECT> <hr></body></html> SimpleApplet.html

<html><head><title>SimpleApplet</title></head> <H3>Demonstrating SimpleApplet</H3> <hr> <OBJECT classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93" width="500" height="100" align="baseline" codebase="http://javaweb.eng/plugin/ jre-1_3-win.exe#Version=1,3,0,0"> <PARAM NAME="code" VALUE="SimpleApplet.class"> <PARAM NAME="codebase" VALUE="."> <PARAM NAME="type" VALUE="application/x-java-applet;version=1.3"> <COMMENT> <EMBED type="application/x-java-applet;version=1.3" width="500" height="100" align="baseline" code="SimpleApplet.class" codebase="." pluginspage="http://javaweb.eng/plugin/plugin-install.html"> <NOEMBED></COMMENT> No JDK 1.3 support for APPLET!! </NOEMBED> </EMBED> </OBJECT> <hr></body></html> <html><head><title>SimpleApplet</title></head> <H3>Demonstrating SimpleApplet</H3> <hr> <OBJECT classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93" width="500" height="100" align="baseline" codebase="http://javaweb.eng/plugin/ jre-1_3-win.exe#Version=1,3,0,0"> <PARAM NAME="code" VALUE="SimpleApplet.class"> <PARAM NAME="codebase" VALUE="."> <PARAM NAME="type" VALUE="application/x-java-applet;version=1.3"> <COMMENT> <EMBED type="application/x-java-applet;version=1.3" width="500" height="100" align="baseline" code="SimpleApplet.class" codebase="." pluginspage="http://javaweb.eng/plugin/plugin-install.html"> <NOEMBED></COMMENT> No JDK 1.3 support for APPLET!! </NOEMBED> </EMBED> </OBJECT> <hr></body></html> SimpleApplet.html

All About Running Applets In a Web Browser http://java.sun.com/products/plugin/1.3/docs/intranet.html http://java.sun.com/products/plugin/1.3/docs/tags.html

Running The Simple Applet Run the Applet in a JFrame Run the Applet in the Appletviewer Build appropriate html file C:\Chap13>appletviewer SimpleApplet.html Run the Applet in a Web Browser Open html file with Browser

C:\Chap13>appletviewer SimpleApplet C:\Chap13>appletviewer SimpleApplet.html or C:\Chap13>appletviewer VerySimpleApplet.html

Running The Simple Applet Run the Applet in a JFrame Run the Applet in the Appletviewer Build appropriate html file C:\Chap13>appletviewer SimpleApplet.html Run the Applet in a Web Browser Open html file with Browser

Applet Limitations Can't Read or Write Local Disk Download Every Use Should package in a .jar file Can only communicate with host that is the source of the Applet.

Applet Strengths Distributes Processing Load to Client No Installation Issues Mistakes can't cause damage to client

How Do I Put Stuff Where I Want It? About Layout Managers

Layout Managers Dynamic A Callback JDK Provides six Layout Managers A method performs positioning A Callback You provide reference to class that implements an interface JDK Provides six Layout Managers FlowLayout GridLayout BorderLayout BoxLayout GridBagLayout Absolute Positioning (null Layout Manager)

FlowLayout class FlowDemo extends JFrame { JPanel jp = new JPanel(new FlowLayout()); JButton b1 = new JButton("Button One"); JButton b2 = new JButton("Button Two"); JButton b3 = new JButton("Button Three"); JButton b4 = new JButton("Button Four"); JButton b5 = new JButton("Button Five"); JButton b6 = new JButton("Button Six"); FlowDemo() { setSize(500, 500); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); getContentPane().add(jp); jp.add(b1); jp.add(b2); jp.add(b3); jp.add(b4); jp.add(b5); jp.add(b6); setVisible(true); } Remember Imports: import javax.swing.*; import java.awt.*; public class DoFlowDemo { public static void main(String[] args) { new FlowDemo(); }

GridLayout class GridDemo extends JFrame { JPanel jp = new JPanel(new GridLayout(2, 3)); JButton b1 = new JButton("Button One"); JButton b2 = new JButton("Button Two"); JButton b3 = new JButton("Button Three"); JButton b4 = new JButton("Button Four"); JButton b5 = new JButton("Button Five"); JButton b6 = new JButton("Button Six"); GridDemo() { setSize(500, 500); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); getContentPane().add(jp); jp.add(b1); jp.add(b2); jp.add(b3); jp.add(b4); jp.add(b5); jp.add(b6); setVisible(true); } class GridDemo extends JFrame { JPanel jp = new JPanel(new FlowLayout()); JButton b1 = new JButton("Button One"); JButton b2 = new JButton("Button Two"); JButton b3 = new JButton("Button Three"); JButton b4 = new JButton("Button Four"); JButton b5 = new JButton("Button Five"); JButton b6 = new JButton("Button Six"); FlowDemo() { setSize(500, 500); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); getContentPane().add(jp); jp.add(b1); jp.add(b2); jp.add(b3); jp.add(b4); jp.add(b5); jp.add(b6); setVisible(true); } public class DoGridDemo { public static void main(String[] args) { new GridDemo(); }

GridLayout class GridDemo2 extends JFrame { JPanel jp = new JPanel(new GridLayout(2, 3)); JScrollPane jsp1 = new JScrollPane(new JTextArea()); JScrollPane jsp2 = new JScrollPane(new JTextArea()); JScrollPane jsp3 = new JScrollPane(new JTextArea()); JScrollPane jsp4 = new JScrollPane(new JTextArea()); JScrollPane jsp5 = new JScrollPane(new JTextArea()); JScrollPane jsp6 = new JScrollPane(new JTextArea()); GridDemo2() { setSize(500, 500); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); getContentPane().add(jp); jp.add(jsp1); jp.add(jsp2); jp.add(jsp3); jp.add(jsp4); jp.add(jsp5); jp.add(jsp6); setVisible(true); } class GridDemo extends JFrame { JPanel jp = new JPanel(new GridLayout(2, 3)); JButton b1 = new JButton("Button One"); JButton b2 = new JButton("Button Two"); JButton b3 = new JButton("Button Three"); JButton b4 = new JButton("Button Four"); JButton b5 = new JButton("Button Five"); JButton b6 = new JButton("Button Six"); GridDemo() { setSize(500, 500); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); getContentPane().add(jp); jp.add(b1); jp.add(b2); jp.add(b3); jp.add(b4); jp.add(b5); jp.add(b6); setVisible(true); } public class DoGridDemo2 { public static void main(String[] args) { new GridDemo2(); }

GridLayout class GridDemo3 extends JFrame { JPanel jp = new JPanel(new GridLayout(2, 3)); JScrollPane jsp1 = new JScrollPane(new JTextArea()); //JScrollPane jsp2 = new JScrollPane(new JTextArea()); JScrollPane jsp3 = new JScrollPane(new JTextArea()); JScrollPane jsp4 = new JScrollPane(new JTextArea()); JScrollPane jsp5 = new JScrollPane(new JTextArea()); JScrollPane jsp6 = new JScrollPane(new JTextArea()); JPanel jp2 = new JPanel(new GridLayout(3,1)); JButton jb1 = new JButton("One"); JButton jb2 = new JButton("Two"); JButton jb3 = new JButton("Three"); GridDemo3() { setSize(500, 500); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); getContentPane().add(jp); jp.add(jsp1); jp.add(jp2); jp2.add(jb1); jp2.add(jb2); jp2.add(jb3); jp.add(jsp3); jp.add(jsp4); jp.add(jsp5); jp.add(jsp6); setVisible(true); } class GridDemo3 extends JFrame { JPanel jp = new JPanel(new GridLayout(2, 3)); JScrollPane jsp1 = new JScrollPane(new JTextArea()); JScrollPane jsp2 = new JScrollPane(new JTextArea()); JScrollPane jsp3 = new JScrollPane(new JTextArea()); JScrollPane jsp4 = new JScrollPane(new JTextArea()); JScrollPane jsp5 = new JScrollPane(new JTextArea()); JScrollPane jsp6 = new JScrollPane(new JTextArea()); JPanel jp2 = new JPanel(new GridLayout(3,1)); JButton jb1 = new JButton("One"); JButton jb2 = new JButton("Two"); JButton jb3 = new JButton("Three"); GridDemo3() { setSize(500, 500); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); getContentPane().add(jp); jp.add(jsp1); jp.add(jp2); jp2.add(jb1); jp2.add(jb2); jp2.add(jb3); jp.add(jsp3); jp.add(jsp4); jp.add(jsp5); jp.add(jsp6); setVisible(true); } class GridDemo3 extends JFrame { JPanel jp = new JPanel(new GridLayout(2, 3)); JScrollPane jsp1 = new JScrollPane(new JTextArea()); JScrollPane jsp2 = new JScrollPane(new JTextArea()); JScrollPane jsp3 = new JScrollPane(new JTextArea()); JScrollPane jsp4 = new JScrollPane(new JTextArea()); JScrollPane jsp5 = new JScrollPane(new JTextArea()); JScrollPane jsp6 = new JScrollPane(new JTextArea()); JPanel jp2 = new JPanel(new GridLayout(3,1)); JButton jb1 = new JButton("One"); JButton jb2 = new JButton("Two"); GridDemo3() { setSize(500, 500); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); getContentPane().add(jp); jp.add(jsp1); jp.add(jp2); jp2.add(jb1); jp2.add(jb2); jp.add(jsp3); jp.add(jsp4); jp.add(jsp5); jp.add(jsp6); setVisible(true); } class GridDemo3 extends JFrame { JPanel jp = new JPanel(new GridLayout(2, 3)); JScrollPane jsp1 = new JScrollPane(new JTextArea()); JScrollPane jsp2 = new JScrollPane(new JTextArea()); JScrollPane jsp3 = new JScrollPane(new JTextArea()); JScrollPane jsp4 = new JScrollPane(new JTextArea()); JScrollPane jsp5 = new JScrollPane(new JTextArea()); JScrollPane jsp6 = new JScrollPane(new JTextArea()); JPanel jp2 = new JPanel(new GridLayout(3,1)); GridDemo3() { setSize(500, 500); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); getContentPane().add(jp); jp.add(jsp1); jp.add(jp2); jp.add(jsp3); jp.add(jsp4); jp.add(jsp5); jp.add(jsp6); setVisible(true); } class GridDemo3 extends JFrame { JPanel jp = new JPanel(new GridLayout(2, 3)); JScrollPane jsp1 = new JScrollPane(new JTextArea()); JScrollPane jsp2 = new JScrollPane(new JTextArea()); JScrollPane jsp3 = new JScrollPane(new JTextArea()); JScrollPane jsp4 = new JScrollPane(new JTextArea()); JScrollPane jsp5 = new JScrollPane(new JTextArea()); JScrollPane jsp6 = new JScrollPane(new JTextArea()); JPanel jp2 = new JPanel(new GridLayout(3,1)); JButton jb1 = new JButton("One"); GridDemo3() { setSize(500, 500); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); getContentPane().add(jp); jp.add(jsp1); jp.add(jp2); jp2.add(jb1); jp.add(jsp3); jp.add(jsp4); jp.add(jsp5); jp.add(jsp6); setVisible(true); } class GridDemo2 extends JFrame { JPanel jp = new JPanel(new GridLayout(2, 3)); JScrollPane jsp1 = new JScrollPane(new JTextArea()); JScrollPane jsp2 = new JScrollPane(new JTextArea()); JScrollPane jsp3 = new JScrollPane(new JTextArea()); JScrollPane jsp4 = new JScrollPane(new JTextArea()); JScrollPane jsp5 = new JScrollPane(new JTextArea()); JScrollPane jsp6 = new JScrollPane(new JTextArea()); GridDemo2() { setSize(500, 500); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); getContentPane().add(jp); jp.add(jsp1); jp.add(jsp2); jp.add(jsp3); jp.add(jsp4); jp.add(jsp5); jp.add(jsp6); setVisible(true); } public class DoGridDemo3 { public static void main(String[] args) { new GridDemo3(); }

BorderLayout class BorderDemo extends JFrame { JPanel jp = new JPanel(new BorderLayout()); JButton b1 = new JButton("Button One"); JButton b2 = new JButton("Button Two"); JButton b3 = new JButton("Button Three"); JButton b4 = new JButton("Button Four"); JButton b5 = new JButton("Button Five"); BorderDemo() { setSize(500, 500); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); getContentPane().add(jp); jp.add(b1, BorderLayout.CENTER); jp.add(b2, BorderLayout.NORTH); jp.add(b3, BorderLayout.SOUTH); jp.add(b4, BorderLayout.EAST); jp.add(b5, BorderLayout.WEST); setVisible(true); } class GridDemo extends JFrame { JPanel jp = new JPanel(new FlowLayout()); JButton b1 = new JButton("Button One"); JButton b2 = new JButton("Button Two"); JButton b3 = new JButton("Button Three"); JButton b4 = new JButton("Button Four"); JButton b5 = new JButton("Button Five"); JButton b6 = new JButton("Button Six"); FlowDemo() { setSize(500, 500); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); getContentPane().add(jp); jp.add(b1); jp.add(b2); jp.add(b3); jp.add(b4); jp.add(b5); jp.add(b6); setVisible(true); } public class DoBorderDemo { public static void main(String[] args) { new BorderDemo(); }

What Happens When You Click? Handling GUI Events

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

DisplayEvents class DisplayEvents extends JFrame { JPanel jp = new JPanel( new BorderLayout() ); JTextArea ta = new JTextArea(); JScrollPane jsp = new JScrollPane(ta); JButton jb = new JButton("The Button"); DisplayEvents() { setSize(600, 500); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); getContentPane().add(jp); ta.setFont(new Font("SansSerif", Font.PLAIN, 18)); jb.setFont(new Font("SansSerif", Font.PLAIN, 18)); jp.add(jsp, BorderLayout.CENTER); jp.add(jb, BorderLayout.SOUTH); jb.setBackground(Color.yellow); setVisible(true); } DisplayEvents

DoDisplayEvents public class DoDisplayEvents { public static void main(String[] args) { new DisplayEvents(); }

Run Program > cd Events1 > java DoDisplayEvents

Class ML Implements MouseListener public void mouseClicked(MouseEvent e) { ta.append("mouseClicked - " + e.paramString() + "\n"); } public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void mousePressed(MouseEvent e) {} public void mouseReleased(MouseEvent e) {}

Place ML class definition here. class DisplayEvents extends JFrame { JPanel jp = new JPanel( new BorderLayout() ); JTextArea ta = new JTextArea(); JScrollPane jsp = new JScrollPane(ta); JButton jb = new JButton("The Button"); DisplayEvents() { setSize(600, 500); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); getContentPane().add(jp); ta.setFont(new Font("SansSerif", Font.PLAIN, 18)); jb.setFont(new Font("SansSerif", Font.PLAIN, 18)); jp.add(jsp, BorderLayout.CENTER); jp.add(jb, BorderLayout.SOUTH); jb.setBackground(Color.yellow); setVisible(true); } class DisplayEvents extends JFrame { JPanel jp = new JPanel( new BorderLayout() ); JTextArea ta = new JTextArea(); JScrollPane jsp = new JScrollPane(ta); JButton jb = new JButton("The Button"); DisplayEvents() { setSize(600, 500); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); getContentPane().add(jp); ta.setFont(new Font("SansSerif", Font.PLAIN, 18)); jb.setFont(new Font("SansSerif", Font.PLAIN, 18)); jp.add(jsp, BorderLayout.CENTER); jp.add(jb, BorderLayout.SOUTH); jb.setBackground(Color.yellow); jb.addMouseListener(new ML() ); setVisible(true); } DisplayEvents Place ML class definition here.

Run Program > cd Events2 > java DoDisplayEvents

Class ML Implements MouseListener public void mouseClicked(MouseEvent e) { ta.append("mouseClicked - " + e.paramString() + "\n"); } public void mouseEntered(MouseEvent e) { ta.append("mouseEntered - " + e.paramString() + "\n"); public void mouseExited(MouseEvent e) { ta.append("mouseExited - " + e.paramString() + "\n"); public void mousePressed(MouseEvent e) {} public void mouseReleased(MouseEvent e) {} class ML implements MouseListener { public void mouseClicked(MouseEvent e) { ta.append("mouseClicked - " + e.paramString() + "\n"); } public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void mousePressed(MouseEvent e) {} public void mouseReleased(MouseEvent e) {}

Place new ML class definition here. class DisplayEvents extends JFrame { JPanel jp = new JPanel( new BorderLayout() ); JTextArea ta = new JTextArea(); JScrollPane jsp = new JScrollPane(ta); JButton jb = new JButton("The Button"); DisplayEvents() { setSize(600, 500); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); getContentPane().add(jp); ta.setFont(new Font("SansSerif", Font.PLAIN, 18)); jb.setFont(new Font("SansSerif", Font.PLAIN, 18)); jp.add(jsp, BorderLayout.CENTER); jp.add(jb, BorderLayout.SOUTH); jb.setBackground(Color.yellow); jb.addMouseListener(new ML() ); setVisible(true); } DisplayEvents No changes needed. Place new ML class definition here.

Run Program > cd Events3 > java DoDisplayEvents

Class ML Implements MouseListener public void mouseClicked(MouseEvent e) { ta.append("mouseClicked - " + e.paramString() + "\n"); } public void mouseEntered(MouseEvent e) { ta.append("mouseEntered - " + e.paramString() + "\n"); public void mouseExited(MouseEvent e) { ta.append("mouseExited - " + e.paramString() + "\n"); public void mousePressed(MouseEvent e) { ta.append("mousePressed - " + e.paramString() + "\n"); public void mouseReleased(MouseEvent e) { ta.append("mouseReleased - " + e.paramString() + "\n"); class ML implements MouseListener { public void mouseClicked(MouseEvent e) { ta.append("mouseClicked - " + e.paramString() + "\n"); } public void mouseEntered(MouseEvent e) { ta.append("mouseEntered - " + e.paramString() + "\n"); public void mouseExited(MouseEvent e) { ta.append("mouseExited - " + e.paramString() + "\n"); public void mousePressed(MouseEvent e) {} public void mouseReleased(MouseEvent e) {}

Place new ML class definition here. class DisplayEvents extends JFrame { JPanel jp = new JPanel( new BorderLayout() ); JTextArea ta = new JTextArea(); JScrollPane jsp = new JScrollPane(ta); JButton jb = new JButton("The Button"); DisplayEvents() { setSize(600, 500); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); getContentPane().add(jp); ta.setFont(new Font("SansSerif", Font.PLAIN, 18)); jb.setFont(new Font("SansSerif", Font.PLAIN, 18)); jp.add(jsp, BorderLayout.CENTER); jp.add(jb, BorderLayout.SOUTH); jb.setBackground(Color.yellow); jb.addMouseListener(new ML() ); setVisible(true); } DisplayEvents No changes needed. Place new ML class definition here.

Run Program > cd Events4 > java DoDisplayEvents

Class FL Implements FocusListener public void focusGained(FocusEvent e) { ta.append("focusGained - " + e.paramString() + "\n"); } public void focusLost(FocusEvent e) { ta.append("focusLost - " + e.paramString() + "\n");

Add new FL class definition here. class DisplayEvents extends JFrame { JPanel jp = new JPanel( new BorderLayout() ); JTextArea ta = new JTextArea(); JScrollPane jsp = new JScrollPane(ta); JButton jb = new JButton("The Button"); DisplayEvents() { setSize(600, 500); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); getContentPane().add(jp); ta.setFont(new Font("SansSerif", Font.PLAIN, 18)); jb.setFont(new Font("SansSerif", Font.PLAIN, 18)); jp.add(jsp, BorderLayout.CENTER); jp.add(jb, BorderLayout.SOUTH); jb.setBackground(Color.yellow); jb.addMouseListener(new ML() ); setVisible(true); } class DisplayEvents extends JFrame { JPanel jp = new JPanel( new BorderLayout() ); JTextArea ta = new JTextArea(); JScrollPane jsp = new JScrollPane(ta); JButton jb = new JButton("The Button"); DisplayEvents() { setSize(600, 500); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); getContentPane().add(jp); ta.setFont(new Font("SansSerif", Font.PLAIN, 18)); jb.setFont(new Font("SansSerif", Font.PLAIN, 18)); jp.add(jsp, BorderLayout.CENTER); jp.add(jb, BorderLayout.SOUTH); jb.setBackground(Color.yellow); jb.addMouseListener(new ML() ); jb.addFocusListener( new FL() ); setVisible(true); } DisplayEvents Add new FL class definition here.

Run Program > cd Events5 > java DoDisplayEvents

Class KL Implements KeyListener public void keyPressed(KeyEvent e) { ta.append("keyPressed - " + e.paramString() + "\n"); } public void keyReleased(KeyEvent e) { ta.append("keyReleased - " + e.paramString() + "\n"); public void keyTyped(KeyEvent e) { ta.append("keyTyped - " + e.paramString() + "\n");

Add new KL class definition here. class DisplayEvents extends JFrame { JPanel jp = new JPanel( new BorderLayout() ); JTextArea ta = new JTextArea(); JScrollPane jsp = new JScrollPane(ta); JButton jb = new JButton("The Button"); DisplayEvents() { setSize(600, 500); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); getContentPane().add(jp); ta.setFont(new Font("SansSerif", Font.PLAIN, 18)); jb.setFont(new Font("SansSerif", Font.PLAIN, 18)); jp.add(jsp, BorderLayout.CENTER); jp.add(jb, BorderLayout.SOUTH); jb.setBackground(Color.yellow); jb.addMouseListener(new ML() ); jb.addFocusListener( new FL() ); setVisible(true); } class DisplayEvents extends JFrame { JPanel jp = new JPanel( new BorderLayout() ); JTextArea ta = new JTextArea(); JScrollPane jsp = new JScrollPane(ta); JButton jb = new JButton("The Button"); DisplayEvents() { setSize(600, 500); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); getContentPane().add(jp); ta.setFont(new Font("SansSerif", Font.PLAIN, 18)); jb.setFont(new Font("SansSerif", Font.PLAIN, 18)); jp.add(jsp, BorderLayout.CENTER); jp.add(jb, BorderLayout.SOUTH); jb.setBackground(Color.yellow); jb.addMouseListener(new ML() ); jb.addFocusListener( new FL() ); jb.addKeyListener( new KL() ); setVisible(true); } DisplayEvents Add new KL class definition here.

Run Program > cd Events6 > java DoDisplayEvents

Class MML Implements MouseMotionListener public void mouseDragged(MouseEvent e) { ta.append("mouseDragged - " + e.paramString() + "\n"); } public void mouseMoved(MouseEvent e) { ta.append("mouseMoved - " + e.paramString() + "\n");

Add new MML class definition here. class DisplayEvents extends JFrame { JPanel jp = new JPanel( new BorderLayout() ); JTextArea ta = new JTextArea(); JScrollPane jsp = new JScrollPane(ta); JButton jb = new JButton("The Button"); DisplayEvents() { setSize(600, 500); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); getContentPane().add(jp); ta.setFont(new Font("SansSerif", Font.PLAIN, 18)); jb.setFont(new Font("SansSerif", Font.PLAIN, 18)); jp.add(jsp, BorderLayout.CENTER); jp.add(jb, BorderLayout.SOUTH); jb.setBackground(Color.yellow); jb.addMouseListener(new ML() ); jb.addFocusListener( new FL() ); jb.addKeyListener( new KL() ); setVisible(true); } class DisplayEvents extends JFrame { JPanel jp = new JPanel( new BorderLayout() ); JTextArea ta = new JTextArea(); JScrollPane jsp = new JScrollPane(ta); JButton jb = new JButton("The Button"); DisplayEvents() { setSize(600, 500); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); getContentPane().add(jp); ta.setFont(new Font("SansSerif", Font.PLAIN, 18)); jb.setFont(new Font("SansSerif", Font.PLAIN, 18)); jp.add(jsp, BorderLayout.CENTER); jp.add(jb, BorderLayout.SOUTH); jb.setBackground(Color.yellow); jb.addMouseListener(new ML() ); jb.addFocusListener( new FL() ); jb.addKeyListener( new KL() ); jb.addMouseMotionListener(new MML() ); setVisible(true); } DisplayEvents Add new MML class definition here.

Run Program > cd Events7 > java DoDisplayEvents

Summary Creating A Window to Work With JFrame Applet Positioning Object in the Window Using Layout Managers Laying out Layout Managers ... Capturing and Using User Events Mouse Keyboard

A Postscript

End of Content