© 2001 by Ashby M. Woolf Revision 2 Java's Graphical User Interface Toolkit Windows, Applets and...

86
© 2001 by Ashby M. Woolf Revision 2 Java's Graphical User Interface Toolkit Windows, Applets and all that Swings.

Transcript of © 2001 by Ashby M. Woolf Revision 2 Java's Graphical User Interface Toolkit Windows, Applets and...

Page 1: © 2001 by Ashby M. Woolf Revision 2 Java's Graphical User Interface Toolkit Windows, Applets and all that Swings.

© 2001 by Ashby M. Woolf Revision 2

Java'sGraphical User Interface

Toolkit

Windows, Applets and all that Swings.

Page 2: © 2001 by Ashby M. Woolf Revision 2 Java's Graphical User Interface Toolkit Windows, Applets and all that Swings.

© 2001 by Ashby M. Woolf Revision 2

What is Swing?

A

Swing

Tour

Page 3: © 2001 by Ashby M. Woolf Revision 2 Java's Graphical User Interface Toolkit Windows, Applets and all that Swings.

© 2001 by Ashby M. Woolf Revision 2

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

Page 4: © 2001 by Ashby M. Woolf Revision 2 Java's Graphical User Interface Toolkit Windows, Applets and all that Swings.

© 2001 by Ashby M. Woolf Revision 2

Starting with an Example

The

Basic

Window

Page 5: © 2001 by Ashby M. Woolf Revision 2 Java's Graphical User Interface Toolkit Windows, Applets and all that Swings.

© 2001 by Ashby M. Woolf Revision 2

The Basic JFrameimport 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); }}

Page 6: © 2001 by Ashby M. Woolf Revision 2 Java's Graphical User Interface Toolkit Windows, Applets and all that Swings.

© 2001 by Ashby M. Woolf Revision 2

The Basic JFrameimport 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); }}

Page 7: © 2001 by Ashby M. Woolf Revision 2 Java's Graphical User Interface Toolkit Windows, Applets and all that Swings.

© 2001 by Ashby M. Woolf Revision 2

jf = new JFrame("The Basic JFrame")

Setting the title with the constructor.

Page 8: © 2001 by Ashby M. Woolf Revision 2 Java's Graphical User Interface Toolkit Windows, Applets and all that Swings.

© 2001 by Ashby M. Woolf Revision 2

jf.setSize(250, 250)

250 pixels

250

pixe

ls

Page 9: © 2001 by Ashby M. Woolf Revision 2 Java's Graphical User Interface Toolkit Windows, Applets and all that Swings.

© 2001 by Ashby M. Woolf Revision 2

jf.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);

Causes the thread for the JFrameto terminate when window closed.

Page 10: © 2001 by Ashby M. Woolf Revision 2 Java's Graphical User Interface Toolkit Windows, Applets and all that Swings.

© 2001 by Ashby M. Woolf Revision 2

jf.setVisible(true);

Page 11: © 2001 by Ashby M. Woolf Revision 2 Java's Graphical User Interface Toolkit Windows, Applets and all that Swings.

© 2001 by Ashby M. Woolf Revision 2

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); }}

Adding The JPanelimport 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); }}

Page 12: © 2001 by Ashby M. Woolf Revision 2 Java's Graphical User Interface Toolkit Windows, Applets and all that Swings.

© 2001 by Ashby M. Woolf Revision 2

jf.getContentPane()

Page 13: © 2001 by Ashby M. Woolf Revision 2 Java's Graphical User Interface Toolkit Windows, Applets and all that Swings.

© 2001 by Ashby M. Woolf Revision 2

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

JPanel

Page 14: © 2001 by Ashby M. Woolf Revision 2 Java's Graphical User Interface Toolkit Windows, Applets and all that Swings.

© 2001 by Ashby M. Woolf Revision 2

Top Level Containers

• JFrame

• JDialog

• JApplet

• JWindow

• JInternalFrame

Page 15: © 2001 by Ashby M. Woolf Revision 2 Java's Graphical User Interface Toolkit Windows, Applets and all that Swings.

© 2001 by Ashby M. Woolf Revision 2

How do I Build a Display Object?

An Object That

Displays a

Window

Page 16: © 2001 by Ashby M. Woolf Revision 2 Java's Graphical User Interface Toolkit Windows, Applets and all that Swings.

© 2001 by Ashby M. Woolf Revision 2

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

MyWindow

public class MyWindow {

• • •

}

Page 17: © 2001 by Ashby M. Woolf Revision 2 Java's Graphical User Interface Toolkit Windows, Applets and all that Swings.

© 2001 by Ashby M. Woolf Revision 2

MyWindowpublic class MyWindow { }

Required but not shown. import javax.swing.*; import java.awt.event.*;

Page 18: © 2001 by Ashby M. Woolf Revision 2 Java's Graphical User Interface Toolkit Windows, Applets and all that Swings.

© 2001 by Ashby M. Woolf Revision 2

MyWindowpublic class MyWindow { MyWindow() { }}

Create all the new GUIobjects and their references

as members here.

Assemble the GUI objects in theconstructor.

Page 19: © 2001 by Ashby M. Woolf Revision 2 Java's Graphical User Interface Toolkit Windows, Applets and all that Swings.

© 2001 by Ashby M. Woolf Revision 2

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

Page 20: © 2001 by Ashby M. Woolf Revision 2 Java's Graphical User Interface Toolkit Windows, Applets and all that Swings.

© 2001 by Ashby M. Woolf Revision 2

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

Page 21: © 2001 by Ashby M. Woolf Revision 2 Java's Graphical User Interface Toolkit Windows, Applets and all that Swings.

© 2001 by Ashby M. Woolf Revision 2

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

Page 22: © 2001 by Ashby M. Woolf Revision 2 Java's Graphical User Interface Toolkit Windows, Applets and all that Swings.

© 2001 by Ashby M. Woolf Revision 2

MyWindowpublic 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); }}

Page 23: © 2001 by Ashby M. Woolf Revision 2 Java's Graphical User Interface Toolkit Windows, Applets and all that Swings.

© 2001 by Ashby M. Woolf Revision 2

MyWindowpublic 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); }}

Page 24: © 2001 by Ashby M. Woolf Revision 2 Java's Graphical User Interface Toolkit Windows, Applets and all that Swings.

© 2001 by Ashby M. Woolf Revision 2

MyWindowpublic 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); }}

Page 25: © 2001 by Ashby M. Woolf Revision 2 Java's Graphical User Interface Toolkit Windows, Applets and all that Swings.

© 2001 by Ashby M. Woolf Revision 2

MyWindowpublic 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); }}

Page 26: © 2001 by Ashby M. Woolf Revision 2 Java's Graphical User Interface Toolkit Windows, Applets and all that Swings.

© 2001 by Ashby M. Woolf Revision 2

MyWindowpublic 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); }}

Page 27: © 2001 by Ashby M. Woolf Revision 2 Java's Graphical User Interface Toolkit Windows, Applets and all that Swings.

© 2001 by Ashby M. Woolf Revision 2

MyWindowpublic 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); }}

Page 28: © 2001 by Ashby M. Woolf Revision 2 Java's Graphical User Interface Toolkit Windows, Applets and all that Swings.

© 2001 by Ashby M. Woolf Revision 2

MyWindowpublic 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); }}

Page 29: © 2001 by Ashby M. Woolf Revision 2 Java's Graphical User Interface Toolkit Windows, Applets and all that Swings.

© 2001 by Ashby M. Woolf Revision 2

MyWindowpublic 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); }}

Page 30: © 2001 by Ashby M. Woolf Revision 2 Java's Graphical User Interface Toolkit Windows, Applets and all that Swings.

© 2001 by Ashby M. Woolf Revision 2

MyWindowpublic 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); }}

Page 31: © 2001 by Ashby M. Woolf Revision 2 Java's Graphical User Interface Toolkit Windows, Applets and all that Swings.

© 2001 by Ashby M. Woolf Revision 2

MyWindowpublic 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); }}

Page 32: © 2001 by Ashby M. Woolf Revision 2 Java's Graphical User Interface Toolkit Windows, Applets and all that Swings.

© 2001 by Ashby M. Woolf Revision 2

MyWindowpublic 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); }}

Page 33: © 2001 by Ashby M. Woolf Revision 2 Java's Graphical User Interface Toolkit Windows, Applets and all that Swings.

© 2001 by Ashby M. Woolf Revision 2

MyWindowpublic 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); }}

Page 34: © 2001 by Ashby M. Woolf Revision 2 Java's Graphical User Interface Toolkit Windows, Applets and all that Swings.

© 2001 by Ashby M. Woolf Revision 2

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); }}

MyWindow

Page 35: © 2001 by Ashby M. Woolf Revision 2 Java's Graphical User Interface Toolkit Windows, Applets and all that Swings.

© 2001 by Ashby M. Woolf Revision 2

How Do I Make an Object Visible?

Making an

Object

Display Itself

Page 36: © 2001 by Ashby M. Woolf Revision 2 Java's Graphical User Interface Toolkit Windows, Applets and all that Swings.

© 2001 by Ashby M. Woolf Revision 2

A Change in Structure

• MyWindow– 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

Page 37: © 2001 by Ashby M. Woolf Revision 2 Java's Graphical User Interface Toolkit Windows, Applets and all that Swings.

© 2001 by Ashby M. Woolf Revision 2

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

MyWindow2

public class MyWindow2 {

• • •

}

Page 38: © 2001 by Ashby M. Woolf Revision 2 Java's Graphical User Interface Toolkit Windows, Applets and all that Swings.

© 2001 by Ashby M. Woolf Revision 2

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); }}

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 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); }}

MyWindow2

Page 39: © 2001 by Ashby M. Woolf Revision 2 Java's Graphical User Interface Toolkit Windows, Applets and all that Swings.

© 2001 by Ashby M. Woolf Revision 2

How Do I Display an Object On the Web?

Building

an

Applet

Page 40: © 2001 by Ashby M. Woolf Revision 2 Java's Graphical User Interface Toolkit Windows, Applets and all that Swings.

© 2001 by Ashby M. Woolf Revision 2

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

Page 41: © 2001 by Ashby M. Woolf Revision 2 Java's Graphical User Interface Toolkit Windows, Applets and all that Swings.

© 2001 by Ashby M. Woolf Revision 2

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 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); }}

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); }}

MyWindow2SimpleApplet

Required but not shown. import javax.swing.*; import java.awt.event.*;

Page 42: © 2001 by Ashby M. Woolf Revision 2 Java's Graphical User Interface Toolkit Windows, Applets and all that Swings.

© 2001 by Ashby M. Woolf Revision 2

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– Build appropriate html file– Open html file with Browser

Page 43: © 2001 by Ashby M. Woolf Revision 2 Java's Graphical User Interface Toolkit Windows, Applets and all that Swings.

© 2001 by Ashby M. Woolf Revision 2

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

MyWindow2

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

public class SimpleApplet extends JApplet { • • •

}

Page 44: © 2001 by Ashby M. Woolf Revision 2 Java's Graphical User Interface Toolkit Windows, Applets and all that Swings.

© 2001 by Ashby M. Woolf Revision 2

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); }}

Page 45: © 2001 by Ashby M. Woolf Revision 2 Java's Graphical User Interface Toolkit Windows, Applets and all that Swings.

© 2001 by Ashby M. Woolf Revision 2

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– Build appropriate html file– Open html file with Browser

Page 46: © 2001 by Ashby M. Woolf Revision 2 Java's Graphical User Interface Toolkit Windows, Applets and all that Swings.

© 2001 by Ashby M. Woolf Revision 2

<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>

VerySimpleApplet.html

Page 47: © 2001 by Ashby M. Woolf Revision 2 Java's Graphical User Interface Toolkit Windows, Applets and all that Swings.

© 2001 by Ashby M. Woolf Revision 2

<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

Page 48: © 2001 by Ashby M. Woolf Revision 2 Java's Graphical User Interface Toolkit Windows, Applets and all that Swings.

© 2001 by Ashby M. Woolf Revision 2

All AboutRunning 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

Page 49: © 2001 by Ashby M. Woolf Revision 2 Java's Graphical User Interface Toolkit Windows, Applets and all that Swings.

© 2001 by Ashby M. Woolf Revision 2

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– Build appropriate html file– Open html file with Browser

Page 50: © 2001 by Ashby M. Woolf Revision 2 Java's Graphical User Interface Toolkit Windows, Applets and all that Swings.

© 2001 by Ashby M. Woolf Revision 2

C:\Chap13>appletviewer SimpleApplet.htmlor

C:\Chap13>appletviewer VerySimpleApplet.html

Page 51: © 2001 by Ashby M. Woolf Revision 2 Java's Graphical User Interface Toolkit Windows, Applets and all that Swings.

© 2001 by Ashby M. Woolf Revision 2

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– Build appropriate html file– Open html file with Browser

Page 52: © 2001 by Ashby M. Woolf Revision 2 Java's Graphical User Interface Toolkit Windows, Applets and all that Swings.

© 2001 by Ashby M. Woolf Revision 2

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.

Page 53: © 2001 by Ashby M. Woolf Revision 2 Java's Graphical User Interface Toolkit Windows, Applets and all that Swings.

© 2001 by Ashby M. Woolf Revision 2

Applet Strengths

• Distributes Processing Load to Client

• No Installation Issues

• Mistakes can't cause damage to client

Page 54: © 2001 by Ashby M. Woolf Revision 2 Java's Graphical User Interface Toolkit Windows, Applets and all that Swings.

© 2001 by Ashby M. Woolf Revision 2

How Do I Put Stuff Where I Want It?

About

Layout

Managers

Page 55: © 2001 by Ashby M. Woolf Revision 2 Java's Graphical User Interface Toolkit Windows, Applets and all that Swings.

© 2001 by Ashby M. Woolf Revision 2

Layout Managers• Dynamic

– 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)

Page 56: © 2001 by Ashby M. Woolf Revision 2 Java's Graphical User Interface Toolkit Windows, Applets and all that Swings.

© 2001 by Ashby M. Woolf Revision 2

FlowLayoutclass 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); }}

public class DoFlowDemo { public static void main(String[] args) { new FlowDemo(); }}

Remember Imports:import javax.swing.*;import java.awt.*;

Page 57: © 2001 by Ashby M. Woolf Revision 2 Java's Graphical User Interface Toolkit Windows, Applets and all that Swings.

© 2001 by Ashby M. Woolf Revision 2

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); }}

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 DoGridDemo { public static void main(String[] args) { new GridDemo(); }}

GridLayout

Page 58: © 2001 by Ashby M. Woolf Revision 2 Java's Graphical User Interface Toolkit Windows, Applets and all that Swings.

© 2001 by Ashby M. Woolf Revision 2

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 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); }}

GridLayout

public class DoGridDemo2 { public static void main(String[] args) { new GridDemo2(); }}

Page 59: © 2001 by Ashby M. Woolf Revision 2 Java's Graphical User Interface Toolkit Windows, Applets and all that Swings.

© 2001 by Ashby M. Woolf Revision 2

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 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 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)); 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); }}

public class DoGridDemo3 { public static void main(String[] args) { new GridDemo3(); }}

GridLayout

Page 60: © 2001 by Ashby M. Woolf Revision 2 Java's Graphical User Interface Toolkit Windows, Applets and all that Swings.

© 2001 by Ashby M. Woolf Revision 2

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); }}

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); }}

public class DoBorderDemo { public static void main(String[] args) { new BorderDemo(); }}

BorderLayout

Page 61: © 2001 by Ashby M. Woolf Revision 2 Java's Graphical User Interface Toolkit Windows, Applets and all that Swings.

© 2001 by Ashby M. Woolf Revision 2

What Happens When You Click?

Handling

GUI

Events

Page 62: © 2001 by Ashby M. Woolf Revision 2 Java's Graphical User Interface Toolkit Windows, Applets and all that Swings.

© 2001 by Ashby M. Woolf Revision 2

DisplayEvents

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

• • •

Page 63: © 2001 by Ashby M. Woolf Revision 2 Java's Graphical User Interface Toolkit Windows, Applets and all that Swings.

© 2001 by Ashby M. Woolf Revision 2

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

Page 64: © 2001 by Ashby M. Woolf Revision 2 Java's Graphical User Interface Toolkit Windows, Applets and all that Swings.

© 2001 by Ashby M. Woolf Revision 2

public class DoDisplayEvents {public static void main(String[] args) {

new DisplayEvents();}

}

DoDisplayEvents

Page 65: © 2001 by Ashby M. Woolf Revision 2 Java's Graphical User Interface Toolkit Windows, Applets and all that Swings.

© 2001 by Ashby M. Woolf Revision 2

Run Program

> cd Events1> java DoDisplayEvents

Page 66: © 2001 by Ashby M. Woolf Revision 2 Java's Graphical User Interface Toolkit Windows, Applets and all that Swings.

© 2001 by Ashby M. Woolf Revision 2

Class ML Implements MouseListener

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) {}}

Page 67: © 2001 by Ashby M. Woolf Revision 2 Java's Graphical User Interface Toolkit Windows, Applets and all that Swings.

© 2001 by Ashby M. Woolf Revision 2

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); }} Place ML class definition here.

DisplayEvents

Page 68: © 2001 by Ashby M. Woolf Revision 2 Java's Graphical User Interface Toolkit Windows, Applets and all that Swings.

© 2001 by Ashby M. Woolf Revision 2

Run Program

> cd Events2> java DoDisplayEvents

Page 69: © 2001 by Ashby M. Woolf Revision 2 Java's Graphical User Interface Toolkit Windows, Applets and all that Swings.

© 2001 by Ashby M. Woolf Revision 2

Class ML Implements MouseListener

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) {}}

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) {}}

Page 70: © 2001 by Ashby M. Woolf Revision 2 Java's Graphical User Interface Toolkit Windows, Applets and all that Swings.

© 2001 by Ashby M. Woolf Revision 2

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); }} Place new ML class definition here.

DisplayEvents

No changes needed.

Page 71: © 2001 by Ashby M. Woolf Revision 2 Java's Graphical User Interface Toolkit Windows, Applets and all that Swings.

© 2001 by Ashby M. Woolf Revision 2

Run Program

> cd Events3> java DoDisplayEvents

Page 72: © 2001 by Ashby M. Woolf Revision 2 Java's Graphical User Interface Toolkit Windows, Applets and all that Swings.

© 2001 by Ashby M. Woolf Revision 2

Class ML Implements MouseListener

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) { 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"); }}

Page 73: © 2001 by Ashby M. Woolf Revision 2 Java's Graphical User Interface Toolkit Windows, Applets and all that Swings.

© 2001 by Ashby M. Woolf Revision 2

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); }} Place new ML class definition here.

DisplayEvents

No changes needed.

Page 74: © 2001 by Ashby M. Woolf Revision 2 Java's Graphical User Interface Toolkit Windows, Applets and all that Swings.

© 2001 by Ashby M. Woolf Revision 2

Run Program

> cd Events4> java DoDisplayEvents

Page 75: © 2001 by Ashby M. Woolf Revision 2 Java's Graphical User Interface Toolkit Windows, Applets and all that Swings.

© 2001 by Ashby M. Woolf Revision 2

Class FL Implements FocusListener

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"); }}

Page 76: © 2001 by Ashby M. Woolf Revision 2 Java's Graphical User Interface Toolkit Windows, Applets and all that Swings.

© 2001 by Ashby M. Woolf Revision 2

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); }} Add new FL class definition here.

DisplayEvents

Page 77: © 2001 by Ashby M. Woolf Revision 2 Java's Graphical User Interface Toolkit Windows, Applets and all that Swings.

© 2001 by Ashby M. Woolf Revision 2

Run Program

> cd Events5> java DoDisplayEvents

Page 78: © 2001 by Ashby M. Woolf Revision 2 Java's Graphical User Interface Toolkit Windows, Applets and all that Swings.

© 2001 by Ashby M. Woolf Revision 2

Class KL Implements KeyListener

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"); }}

Page 79: © 2001 by Ashby M. Woolf Revision 2 Java's Graphical User Interface Toolkit Windows, Applets and all that Swings.

© 2001 by Ashby M. Woolf Revision 2

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); }} Add new KL class definition here.

DisplayEvents

Page 80: © 2001 by Ashby M. Woolf Revision 2 Java's Graphical User Interface Toolkit Windows, Applets and all that Swings.

© 2001 by Ashby M. Woolf Revision 2

Run Program

> cd Events6> java DoDisplayEvents

Page 81: © 2001 by Ashby M. Woolf Revision 2 Java's Graphical User Interface Toolkit Windows, Applets and all that Swings.

© 2001 by Ashby M. Woolf Revision 2

Class MML Implements MouseMotionListener

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"); }}

Page 82: © 2001 by Ashby M. Woolf Revision 2 Java's Graphical User Interface Toolkit Windows, Applets and all that Swings.

© 2001 by Ashby M. Woolf Revision 2

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); }} Add new MML class definition here.

DisplayEvents

Page 83: © 2001 by Ashby M. Woolf Revision 2 Java's Graphical User Interface Toolkit Windows, Applets and all that Swings.

© 2001 by Ashby M. Woolf Revision 2

Run Program

> cd Events7> java DoDisplayEvents

Page 84: © 2001 by Ashby M. Woolf Revision 2 Java's Graphical User Interface Toolkit Windows, Applets and all that Swings.

© 2001 by Ashby M. Woolf Revision 2

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

Page 85: © 2001 by Ashby M. Woolf Revision 2 Java's Graphical User Interface Toolkit Windows, Applets and all that Swings.

© 2001 by Ashby M. Woolf Revision 2

A Postscript

Page 86: © 2001 by Ashby M. Woolf Revision 2 Java's Graphical User Interface Toolkit Windows, Applets and all that Swings.

© 2001 by Ashby M. Woolf Revision 2

End of Content