COMP16121 Sample Code Lecture 1

Similar documents
Graphical User Interfaces in Java

17 GUI API: Container 18 Hello world with a GUI 19 GUI API: JLabel 20 GUI API: Container: add() 21 Hello world with a GUI 22 GUI API: JFrame: setdefau

SampleApp.java. Page 1

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

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

PIC 20A GUI with swing

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

Example: Building a Java GUI

Example: Building a Java GUI

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

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

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

Agenda. Container and Component

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

Graphical User Interfaces in Java - SWING

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

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

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

Building Java Programs Bonus Slides

69 Section 7: Example:Single times table with exception catching 70 Aim 74 Single times table with exception catching

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

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

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

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

COMP16121 Notes on Mock Exam Questions

Window Interfaces Using Swing. Chapter 12

CSCI 201L Midterm Written Summer % of course grade

AP CS Unit 11: Graphics and Events

Part I: Learn Common Graphics Components

Window Interfaces Using Swing Objects

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

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

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

Introduction. Introduction

MIT AITI Swing Event Model Lecture 17

CS 251 Intermediate Programming GUIs: Event Listeners

CSIS 10A Assignment 7 SOLUTIONS

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

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

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

Dr. Hikmat A. M. AbdelJaber

GUI Applications. Let s start with a simple Swing application in Java, and then we will look at the same application in Jython. See Listing 16-1.

Window Interfaces Using Swing Objects

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

Graphical User Interface

CMP-326 Exam 2 Spring 2018 Solutions Question 1. Version 1. Version 2

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

CSCI 201L Midterm Written SOLUTION Summer % of course grade

Graphic User Interfaces. - GUI concepts - Swing - AWT

JAVA NOTES GRAPHICAL USER INTERFACES

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

More about GUIs GEEN163

// autor igre Ivan Programerska sekcija package mine;

User interfaces and Swing

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

Calculator Class. /** * Create a new calculator and show it. */ public Calculator() { engine = new CalcEngine(); gui = new UserInterface(engine); }

Systems Programming Graphical User Interfaces

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

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

CS 251 Intermediate Programming GUIs: Components and Layout

Graphics programming. COM6516 Object Oriented Programming and Design Adam Funk (originally Kirill Bogdanov & Mark Stevenson)

CMP 326 Midterm Fall 2015

Chapter 9 Designing Graphical User Interfaces (GUIs)

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

Chapter 13 Lab Advanced GUI Applications Lab Objectives. Introduction. Task #1 Creating a Menu with Submenus

University of Cape Town Department of Computer Science. Computer Science CSC117F Solutions

GUI Forms and Events, Part II

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

Class 16: The Swing Event Model

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

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

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

Java. GUI building with the AWT

AnimatedImage.java. Page 1

Command-Line Applications. GUI Libraries GUI-related classes are defined primarily in the java.awt and the javax.swing packages.

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

Based on slides by Prof. Burton Ma

Section Basic graphics

Java Just in Time: Collected concepts after chapter 18

Graphical User Interfaces. Swing. Jose Jesus García Rueda

Swing I CHAPTER EVENT-DRIVEN PROGRAMMING 921 Events and Listeners 921

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

Swing I Event-Driven Programming Buttons, Events, and Other Swing Basics Containers and Layout Managers 946

PART 22. Java GUI Library SWT GUI Library SWING GUI Library Swing First Program

What Is an Event? Some event handler. ActionEvent. actionperformed(actionevent e) { }

Java Never Ends MULTITHREADING 958 Example: A Nonresponsive GUI 959

SE1021 Exam 2. When returning your exam, place your note-sheet on top. Page 1: This cover. Page 2 (Multiple choice): 10pts

Points Missed on Page page 1 of 8

Graphical User Interfaces. Comp 152

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

Chapter 13 Lab Advanced GUI Applications

Final Exam CS 251, Intermediate Programming December 13, 2017

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

An array is a type of variable that is able to hold more than one piece of information under a single variable name.

Chapter 7: A First Look at GUI Applications

CS 134 Programming Exercise 7:

Java Swing. Lists Trees Tables Styled Text Components Progress Indicators Component Organizers

Part 3: Graphical User Interface (GUI) & Java Applets

Class 14: Introduction to the Swing Toolkit

1.00 Lecture 14. Lecture Preview

Transcription:

COMP16121 Sample Code Lecture 1 Sean Bechhofer, University of Manchester, Manchester, UK sean.bechhofer@manchester.ac.uk 1 SimpleFrame 1 import javax.swing.jframe; 2 3 public class SimpleFrame { 4 5 /* Main method to drive the program */ 6 public static void main( String [] args ) { 7 JFrame theframe = new JFrame(); 8 theframe.settitle( "A Frame" ); 9 theframe.setsize( 300, 200 ); 10 theframe.setvisible( true ); 11 } // main 12 13 } // SimpleFrame 1

2 HelloWorld 1 import java.awt.container; 2 import javax.swing.jlabel; 3 import javax.swing.jframe; 4 5 public class HelloWorld extends JFrame { 6 7 /* Constructor */ 8 public HelloWorld() { 9 settitle( "Hello World" ); 10 /* Get the contents of the frame and add a label to it */ 11 Container contents = getcontentpane(); 12 contents.add( new JLabel( "Hello World!" ) ); 13 14 /* Specify appropriate behaviour when the window is closed */ 15 setdefaultcloseoperation( EXIT_ON_CLOSE ); 16 17 /* pack the frame */ 18 pack(); 19 } // HelloWorld 20 21 /* Main method to drive the program */ 22 public static void main( String [] args ) { 23 HelloWorld thehelloworld = new HelloWorld(); 24 thehelloworld.setvisible( true ); 25 } // main 26 27 } // HelloWorld 2

3 HelloSolarSystem 1 import java.awt.container; 2 import java.awt.flowlayout; 3 import javax.swing.jlabel; 4 import javax.swing.jframe; 5 6 public class HelloSolarSystem extends JFrame { 7 8 /* Constructor */ 9 public HelloSolarSystem() { 10 settitle( "Hello SolarSystem" ); 11 /* Get the contents of the frame. */ 12 Container contents = getcontentpane(); 13 /* Set the Layout Manager */ 14 contents.setlayout( new FlowLayout( FlowLayout.LEFT, 10, 10 ) ); 15 /* Add labels */ 16 contents.add( new JLabel( "Hello Mercury!" ) ); 17 contents.add( new JLabel( "Hello Venus!" ) ); 18 contents.add( new JLabel( "Hello Earth!" ) ); 19 contents.add( new JLabel( "Hello Mars!" ) ); 20 contents.add( new JLabel( "Hello Jupiter!" ) ); 21 contents.add( new JLabel( "Hello Saturn!" ) ); 22 contents.add( new JLabel( "Hello Uranus!" ) ); 23 contents.add( new JLabel( "Hello Neptune!" ) ); 24 contents.add( new JLabel( "Hello Pluto!" ) ); 25 26 /* Specify appropriate behaviour when the window is closed */ 27 setdefaultcloseoperation( EXIT_ON_CLOSE ); 28 29 /* pack the frame */ 30 pack(); 31 } // HelloSolarSystem 32 33 /* Main method to drive the program */ 34 public static void main( String [] args ) { 35 HelloSolarSystem thehellosolarsystem = 36 new HelloSolarSystem(); 37 thehellosolarsystem.setvisible( true ); 38 } // main 39 40 } // HelloSolarSystem 3

4 HelloSolarSystemGrid 1 import java.awt.container; 2 import java.awt.gridlayout; 3 import javax.swing.jlabel; 4 import javax.swing.jframe; 5 6 public class HelloSolarSystemGrid extends JFrame { 7 8 /* Constructor */ 9 public HelloSolarSystemGrid() { 10 settitle( "Hello SolarSystem" ); 11 /* Get the contents of the frame. */ 12 Container contents = getcontentpane(); 13 /* Set the Layout Manager */ 14 contents.setlayout( new GridLayout( 0, 3, 10, 10 ) ); 15 /* Add labels */ 16 contents.add( new JLabel( "Hello Mercury!" ) ); 17 contents.add( new JLabel( "Hello Venus!" ) ); 18 contents.add( new JLabel( "Hello Earth!" ) ); 19 contents.add( new JLabel( "Hello Mars!" ) ); 20 contents.add( new JLabel( "Hello Jupiter!" ) ); 21 contents.add( new JLabel( "Hello Saturn!" ) ); 22 contents.add( new JLabel( "Hello Uranus!" ) ); 23 contents.add( new JLabel( "Hello Neptune!" ) ); 24 contents.add( new JLabel( "Hello Pluto!" ) ); 25 26 /* Specify appropriate behaviour when the window is closed */ 27 setdefaultcloseoperation( EXIT_ON_CLOSE ); 28 29 /* pack the frame */ 30 pack(); 31 } // HelloSolarSystemGrid 32 33 /* Main method to drive the program */ 34 public static void main( String [] args ) { 35 HelloSolarSystemGrid thehellosolarsystemgrid = 36 new HelloSolarSystemGrid(); 37 thehellosolarsystemgrid.setvisible( true ); 38 } // main 39 40 } // HelloSolarSystemGrid 4

5 HelloSmiley 1 import java.awt.container; 2 3 import javax.swing.imageicon; 4 import javax.swing.jframe; 5 import javax.swing.jlabel; 6 7 public class HelloSmiley extends JFrame { 8 9 /* Constructor */ 10 public HelloSmiley() { 11 settitle( "Hello Smiley" ); 12 /* Get the contents of the frame and add a label to it */ 13 Container contents = getcontentpane(); 14 contents.add( new JLabel( new ImageIcon( "smiley.jpg" ) ) ); 15 16 /* Specify appropriate behaviour when the window is closed */ 17 setdefaultcloseoperation( EXIT_ON_CLOSE ); 18 19 /* pack the frame */ 20 pack(); 21 } // HelloWorld 22 23 /* Main method to drive the program */ 24 public static void main( String [] args ) { 25 HelloSmiley thehellosmiley = new HelloSmiley(); 26 thehellosmiley.setvisible( true ); 27 } // main 28 29 } // HelloSmiley 5

COMP16121 Sample Code Lecture 2 Sean Bechhofer, University of Manchester, Manchester, UK sean.bechhofer@manchester.ac.uk 1 StopClock 1 import javax.swing.jlabel; 2 import java.util.date; 3 import javax.swing.jframe; 4 import java.awt.event.actionlistener; 5 import java.awt.event.actionevent; 6 import javax.swing.jbutton; 7 import java.awt.container; 8 import java.awt.gridlayout; 9 10 /** 11 * Stop Clock example. Button stops and starts the clock, which 12 * records start time, stop time and elapsed time. 13 * @author <a href="mailto:seanb@cs.man.ac.uk">sean Bechhofer</a> 14 */ 15 public class StopClock extends JFrame implements ActionListener { 16 17 /* Local instance variables */ 18 private boolean isrunning = false; 19 private Date startdate; 20 private Date stopdate; 21 private JLabel starttimejlabel = new JLabel("Not started"); 22 private JLabel stoptimejlabel = new JLabel("Not started"); 23 private JLabel elapsedtimejlabel = new JLabel("Not started"); 24 25 /* Constructor */ 26 public StopClock() { 27 settitle( "Stop Clock" ); 28 29 Container contents = getcontentpane(); 30 /* Grid layout with one column */ 31 contents.setlayout( new GridLayout( 0, 1 ) ); 32 33 /* Add some labels */ 34 contents.add(new JLabel("Started at:")); 35 contents.add(starttimejlabel); 36 contents.add(new JLabel("Stopped at:")); 37 contents.add(stoptimejlabel); 38 contents.add(new JLabel("Elapsed time (seconds):")); 39 contents.add(elapsedtimejlabel); 1

40 41 /* Create a button */ 42 JButton startstopjbutton = new JButton("Start / Stop"); 43 /* Register me with the button as a listener */ 44 startstopjbutton.addactionlistener(this); 45 contents.add(startstopjbutton); 46 47 /* Specify appropriate behaviour when the window is closed */ 48 setdefaultcloseoperation(exit_on_close); 49 50 /* pack the frame */ 51 pack(); 52 } // StopClock 53 54 public void actionperformed( ActionEvent evt ) { 55 if (! isrunning) { 56 /* Start the clock */ 57 startdate = new Date(); 58 starttimejlabel.settext("" + startdate); 59 stoptimejlabel.settext("running..."); 60 elapsedtimejlabel.settext("running..."); 61 isrunning = true; 62 // Need to pack again because label size may have changed 63 pack(); 64 } else { 65 /* Stop the clock and show the updated times */ 66 stopdate = new Date(); 67 stoptimejlabel.settext("" + stopdate); 68 long elapsedmilliseconds = 69 (stopdate.gettime() - startdate.gettime()); 70 elapsedtimejlabel.settext("" + elapsedmilliseconds / 1000.0); 71 isrunning = false; 72 // Need to pack again because label size may have changed 73 pack(); 74 } 75 } // actionperformed 76 77 public static void main(string [] args) { 78 StopClock thestopclock = new StopClock(); 79 thestopclock.setvisible( true ); 80 } // main 81 } 2

2 Button 1 import java.awt.container; 2 import javax.swing.jframe; 3 import javax.swing.jbutton; 4 import java.awt.event.actionlistener; 5 import java.awt.event.actionevent; 6 7 /** 8 * Simple frame with a single button. 9 * @author <a href="mailto:seanb@cs.man.ac.uk">sean Bechhofer</a> 10 */ 11 public class Button extends JFrame implements ActionListener { 12 13 private JButton thebutton = new JButton( "Press Me!" ); 14 15 /* Constructor */ 16 public Button() { 17 settitle("one Button"); 18 /* Get the contents of the frame and add a label to it */ 19 Container contents = getcontentpane(); 20 contents.add( thebutton ); 21 thebutton.addactionlistener(this); 22 23 /* Specify appropriate behaviour when the window is closed */ 24 setdefaultcloseoperation(exit_on_close); 25 26 /* pack the frame */ 27 pack(); 28 } // OneButton 29 30 public void actionperformed( ActionEvent evt ) { 31 System.out.println("Pressed!"); 32 } 33 34 /* Main method to drive the program */ 35 public static void main(string [] args) { 36 Button thebutton = new Button(); 37 thebutton.setvisible( true ); 38 } // main 39 40 } // OneButton 3

COMP16121 Sample Code Lecture 3 Sean Bechhofer, University of Manchester, Manchester, UK sean.bechhofer@manchester.ac.uk 1 GCD 1 import javax.swing.jtextfield; 2 import javax.swing.jframe; 3 import java.awt.event.actionlistener; 4 import javax.swing.jbutton; 5 import java.awt.event.actionevent; 6 import java.awt.gridlayout; 7 import javax.swing.jlabel; 8 import java.awt.container; 9 10 /** 11 * Calculates GCD of two integers. Simple GUI. 12 * @author <a href="mailto:seanb@cs.man.ac.uk">sean Bechhofer</a> 13 */ 14 public class GCD extends JFrame implements ActionListener { 15 16 private JTextField number1jtextfield = new JTextField(20); 17 private JTextField number2jtextfield = new JTextField(20); 18 private JTextField resultjtextfield = new JTextField(20); 19 20 public GCD() { 21 settitle("gcd"); 22 Container contents = getcontentpane(); 23 contents.setlayout(new GridLayout(0, 1)); 24 contents.add(new JLabel("Number 1")); 25 contents.add(number1jtextfield); 26 contents.add(new JLabel("Number 2")); 27 contents.add(number2jtextfield); 28 JButton computejbutton = new JButton("Compute"); 29 contents.add(computejbutton); 30 computejbutton.addactionlistener(this); 31 contents.add(new JLabel("GCD of Number 1 and Number 2")); 32 contents.add(resultjtextfield); 33 /* Specify appropriate behaviour when the window is closed */ 34 setdefaultcloseoperation(exit_on_close); 35 36 pack(); 37 } // GCD 38 39 public void actionperformed(actionevent e) { 1

40 int number1 = Integer.parseInt(number1JTextField.getText()); 41 int number2 = Integer.parseInt(number2JTextField.getText()); 42 int thegcd = greatestcommondivisor(number1, number2); 43 resultjtextfield.settext("" + thegcd); 44 } // actionperformed 45 46 private static int greatestcommondivisor(int multiple1ofgcd, 47 int multiple2ofgcd) { 48 // Both multiple1ofgcd and multiple2ofgcd must be non-negative 49 while (multiple1ofgcd!= multiple2ofgcd) 50 if (multiple1ofgcd > multiple2ofgcd) 51 multiple1ofgcd = multiple1ofgcd - multiple2ofgcd; 52 else 53 multiple2ofgcd = multiple2ofgcd - multiple1ofgcd; 54 return multiple1ofgcd; 55 } // greatestcommondivisor 56 57 public static void main(string [] args) { 58 GCD thegcd = new GCD(); 59 thegcd.setvisible( true ); 60 } // main 61 } // class GCD 2

2 GCDDisabled 1 import javax.swing.jtextfield; 2 import javax.swing.jframe; 3 import java.awt.event.actionlistener; 4 import javax.swing.jbutton; 5 import java.awt.event.actionevent; 6 import java.awt.gridlayout; 7 import javax.swing.jlabel; 8 import java.awt.container; 9 10 /** 11 * Calculates GCD of two integers. Simple GUI. 12 * @author <a href="mailto:seanb@cs.man.ac.uk">sean Bechhofer</a> 13 */ 14 public class GCDDisabled extends JFrame implements ActionListener { 15 16 private JTextField number1jtextfield = new JTextField(20); 17 private JTextField number2jtextfield = new JTextField(20); 18 private JTextField resultjtextfield = new JTextField(20); 19 20 public GCDDisabled() { 21 settitle("gcd"); 22 Container contents = getcontentpane(); 23 contents.setlayout(new GridLayout(0, 1)); 24 contents.add(new JLabel("Number 1")); 25 contents.add(number1jtextfield); 26 contents.add(new JLabel("Number 2")); 27 contents.add(number2jtextfield); 28 JButton computejbutton = new JButton("Compute"); 29 contents.add(computejbutton); 30 computejbutton.addactionlistener(this); 31 contents.add(new JLabel("GCD of Number 1 and Number 2")); 32 contents.add(resultjtextfield); 33 resultjtextfield.setenabled( false ); 34 /* Specify appropriate behaviour when the window is closed */ 35 setdefaultcloseoperation(exit_on_close); 36 37 pack(); 38 } // GCDDisabled 39 40 public void actionperformed(actionevent e) { 41 int number1 = Integer.parseInt(number1JTextField.getText()); 42 int number2 = Integer.parseInt(number2JTextField.getText()); 43 int thegcd = greatestcommondivisor(number1, number2); 44 resultjtextfield.settext("" + thegcd); 45 } // actionperformed 46 47 private static int greatestcommondivisor(int multiple1ofgcd, 48 int multiple2ofgcd) { 49 // Both multiple1ofgcd and multiple2ofgcd must be non-negative 3

50 while (multiple1ofgcd!= multiple2ofgcd) 51 if (multiple1ofgcd > multiple2ofgcd) 52 multiple1ofgcd = multiple1ofgcd - multiple2ofgcd; 53 else 54 multiple2ofgcd = multiple2ofgcd - multiple1ofgcd; 55 return multiple1ofgcd; 56 } // greatestcommondivisor 57 58 public static void main(string [] args) { 59 GCDDisabled thegcd = new GCDDisabled(); 60 thegcd.setvisible( true ); 61 } // main 62 } // class GCDDisabled 4

3 TimesTable 1 import javax.swing.jframe; 2 import java.awt.event.actionlistener; 3 import javax.swing.jtextfield; 4 import javax.swing.jtextarea; 5 import java.awt.container; 6 import javax.swing.jbutton; 7 import java.awt.borderlayout; 8 import java.awt.event.actionevent; 9 10 /** 11 * Displays a times table using a {@link JTextArea JTextArea}. 12 * @author <a href="mailto:seanb@cs.man.ac.uk">sean Bechhofer</a> 13 */ 14 public class TimesTable extends JFrame implements ActionListener { 15 16 private JTextField multiplierjtextfield = new JTextField(5); 17 private JTextArea displayjtextarea = new JTextArea(15, 20); 18 19 public TimesTable() { 20 settitle("times Table"); 21 Container contents = getcontentpane(); 22 contents.setlayout(new BorderLayout()); 23 contents.add(multiplierjtextfield, BorderLayout.NORTH); 24 contents.add(displayjtextarea, BorderLayout.CENTER); 25 JButton displayjbutton = new JButton("Display"); 26 contents.add(displayjbutton, BorderLayout.SOUTH); 27 displayjbutton.addactionlistener(this); 28 29 setdefaultcloseoperation(exit_on_close); 30 pack(); 31 } // TimesTable 32 33 public void actionperformed(actionevent e) { 34 displayjtextarea.settext(""); 35 int multiplier = Integer.parseInt(multiplierJTextField.getText()); 36 displayjtextarea.append("--------------------------------\n"); 37 displayjtextarea.append(" Times table for " + multiplier + "\n"); 38 displayjtextarea.append("--------------------------------\n"); 39 for (int thisnumber = 1; 40 thisnumber <= 10; 41 thisnumber = thisnumber + 1) 42 displayjtextarea.append(" " + thisnumber + " x " + 43 multiplier + " = " 44 + thisnumber * multiplier + "\n"); 45 displayjtextarea.append("--------------------------------\n"); 46 } // actionperformed 47 48 public static void main(string [] args) { 49 TimesTable thetimestable = new TimesTable(); 5

50 thetimestable.setvisible( true ); 51 } // main 52 53 } // class TimesTable 54 55 6

4 GCDWithPanels 1 import javax.swing.jtextfield; 2 import javax.swing.jframe; 3 import java.awt.event.actionlistener; 4 import javax.swing.jbutton; 5 import java.awt.event.actionevent; 6 import java.awt.gridlayout; 7 import javax.swing.jlabel; 8 import java.awt.container; 9 import javax.swing.jpanel; 10 11 /** 12 * Calculates GCD of two integers. GUI using nested panels for layout. 13 * @author <a href="mailto:seanb@cs.man.ac.uk">sean Bechhofer</a> 14 */ 15 public class GCDWithPanels extends JFrame implements ActionListener { 16 17 private JTextField number1jtextfield = new JTextField(20); 18 private JTextField number2jtextfield = new JTextField(20); 19 private JTextField resultjtextfield = new JTextField(20); 20 21 public GCDWithPanels() { 22 settitle("gcd"); 23 Container contents = getcontentpane(); 24 contents.setlayout(new GridLayout(0, 1)); 25 26 JPanel numberfieldspanel = new JPanel(); 27 numberfieldspanel.setlayout( new GridLayout( 0, 2 ) ); 28 contents.add( numberfieldspanel ); 29 30 numberfieldspanel.add(new JLabel("Number 1")); 31 numberfieldspanel.add(new JLabel("Number 2")); 32 numberfieldspanel.add(number1jtextfield); 33 numberfieldspanel.add(number2jtextfield); 34 35 JPanel buttonandresultpanel = new JPanel(); 36 buttonandresultpanel.setlayout( new GridLayout( 0, 2 ) ); 37 contents.add( buttonandresultpanel ); 38 39 JButton computejbutton = new JButton("Compute"); 40 buttonandresultpanel.add(computejbutton); 41 computejbutton.addactionlistener(this); 42 43 JPanel resultpanel = new JPanel(); 44 resultpanel.setlayout( new GridLayout( 0, 1 ) ); 45 buttonandresultpanel.add( resultpanel ); 46 47 resultpanel.add(new JLabel("GCD of Number 1 and Number 2")); 48 resultpanel.add(resultjtextfield); 49 /* Specify appropriate behaviour when the window is closed */ 7

50 setdefaultcloseoperation(exit_on_close); 51 52 pack(); 53 } // GCDWithPanels 54 55 public void actionperformed(actionevent e) { 56 int number1 = Integer.parseInt(number1JTextField.getText()); 57 int number2 = Integer.parseInt(number2JTextField.getText()); 58 int thegcd = greatestcommondivisor(number1, number2); 59 resultjtextfield.settext("" + thegcd); 60 } // actionperformed 61 62 private static int greatestcommondivisor(int multiple1ofgcd, 63 int multiple2ofgcd) { 64 // Both multiple1ofgcd and multiple2ofgcd must be non-negative 65 while (multiple1ofgcd!= multiple2ofgcd) 66 if (multiple1ofgcd > multiple2ofgcd) 67 multiple1ofgcd = multiple1ofgcd - multiple2ofgcd; 68 else 69 multiple2ofgcd = multiple2ofgcd - multiple1ofgcd; 70 return multiple1ofgcd; 71 } // greatestcommondivisor 72 73 public static void main(string [] args) { 74 GCDWithPanels thegcd = new GCDWithPanels(); 75 thegcd.setvisible( true ); 76 } // main 77 } // class GCDWithPanels 8

5 TimesTable2 1 import javax.swing.jframe; 2 import java.awt.event.actionlistener; 3 import javax.swing.jtextfield; 4 import javax.swing.jtextarea; 5 import java.awt.container; 6 import javax.swing.jbutton; 7 import java.awt.borderlayout; 8 import java.awt.event.actionevent; 9 import javax.swing.jpanel; 10 import java.awt.gridlayout; 11 import javax.swing.jlabel; 12 13 /** 14 * Displays a times table using a {@link JTextArea JTextArea}. 15 * @author <a href="mailto:seanb@cs.man.ac.uk">sean Bechhofer</a> 16 */ 17 public class TimesTable2 extends JFrame implements ActionListener { 18 19 private JTextField multiplierjtextfield = new JTextField(5); 20 private JTextField tablesizejtextfield = new JTextField(5); 21 private JTextArea displayjtextarea = new JTextArea(15, 20); 22 23 public TimesTable2() { 24 settitle("times Table"); 25 Container contents = getcontentpane(); 26 contents.setlayout(new BorderLayout()); 27 JPanel numberspanel = new JPanel( new GridLayout( 2, 2 ) ); 28 contents.add( numberspanel, BorderLayout.NORTH ); 29 numberspanel.add( new JLabel( "Multiplier:" ) ); 30 numberspanel.add( multiplierjtextfield ); 31 32 numberspanel.add( new JLabel( "Table size:" ) ); 33 numberspanel.add( tablesizejtextfield ); 34 contents.add(displayjtextarea, BorderLayout.CENTER); 35 JButton displayjbutton = new JButton("Display"); 36 contents.add(displayjbutton, BorderLayout.SOUTH); 37 displayjbutton.addactionlistener(this); 38 39 setdefaultcloseoperation(exit_on_close); 40 pack(); 41 } // TimesTable2 42 43 public void actionperformed(actionevent e) { 44 displayjtextarea.settext(""); 45 int multiplier = Integer.parseInt(multiplierJTextField.getText()); 46 int tablesize = Integer.parseInt(tableSizeJTextField.getText()); 47 displayjtextarea.append("--------------------------------\n"); 48 displayjtextarea.append(" Times table for " + multiplier + "\n"); 49 displayjtextarea.append("--------------------------------\n"); 9

50 for (int thisnumber = 1; 51 thisnumber <= tablesize; 52 thisnumber = thisnumber + 1) 53 displayjtextarea.append(" " + thisnumber + " x " + 54 multiplier + " = " 55 + thisnumber * multiplier + "\n"); 56 displayjtextarea.append("--------------------------------\n"); 57 } // actionperformed 58 59 public static void main(string [] args) { 60 TimesTable2 thetimestable = new TimesTable2(); 61 thetimestable.setvisible( true ); 62 } // main 63 64 } // class TimesTable2 65 66 10

6 TimesTableScroll 1 import javax.swing.jframe; 2 import java.awt.event.actionlistener; 3 import javax.swing.jtextfield; 4 import javax.swing.jtextarea; 5 import java.awt.container; 6 import javax.swing.jbutton; 7 import java.awt.borderlayout; 8 import java.awt.event.actionevent; 9 import javax.swing.jpanel; 10 import java.awt.gridlayout; 11 import javax.swing.jlabel; 12 import javax.swing.jscrollpane; 13 14 /** 15 * Displays a times table using a {@link JTextArea JTextArea}. 16 * @author <a href="mailto:seanb@cs.man.ac.uk">sean Bechhofer</a> 17 */ 18 public class TimesTableScroll extends JFrame implements ActionListener { 19 20 private JTextField multiplierjtextfield = new JTextField(5); 21 private JTextField tablesizejtextfield = new JTextField(5); 22 private JTextArea displayjtextarea = new JTextArea(15, 20); 23 24 public TimesTableScroll() { 25 settitle("times Table"); 26 Container contents = getcontentpane(); 27 contents.setlayout(new BorderLayout()); 28 JPanel numberspanel = new JPanel( new GridLayout( 2, 2 ) ); 29 contents.add( numberspanel, BorderLayout.NORTH ); 30 numberspanel.add( new JLabel( "Multiplier:" ) ); 31 numberspanel.add( multiplierjtextfield ); 32 33 numberspanel.add( new JLabel( "Table size:" ) ); 34 numberspanel.add( tablesizejtextfield ); 35 contents.add( new JScrollPane ( displayjtextarea), 36 BorderLayout.CENTER ); 37 JButton displayjbutton = new JButton("Display"); 38 contents.add(displayjbutton, BorderLayout.SOUTH); 39 displayjbutton.addactionlistener(this); 40 41 setdefaultcloseoperation(exit_on_close); 42 pack(); 43 } // TimesTableScroll 44 45 public void actionperformed(actionevent e) { 46 displayjtextarea.settext(""); 47 int multiplier = Integer.parseInt(multiplierJTextField.getText()); 48 int tablesize = Integer.parseInt(tableSizeJTextField.getText()); 49 displayjtextarea.append("--------------------------------\n"); 11

50 displayjtextarea.append(" Times table for " + multiplier + "\n"); 51 displayjtextarea.append("--------------------------------\n"); 52 for (int thisnumber = 1; 53 thisnumber <= tablesize; 54 thisnumber = thisnumber + 1) 55 displayjtextarea.append(" " + thisnumber + " x " + 56 multiplier + " = " 57 + thisnumber * multiplier + "\n"); 58 displayjtextarea.append("--------------------------------\n"); 59 } // actionperformed 60 61 public static void main(string [] args) { 62 TimesTableScroll thetimestable = new TimesTableScroll(); 63 thetimestable.setvisible( true ); 64 } // main 65 66 } // class TimesTableScroll 67 68 12

COMP16121 Sample Code Lecture 4 Sean Bechhofer, University of Manchester, Manchester, UK sean.bechhofer@manchester.ac.uk 1 GCDWithNew 1 import java.awt.container; 2 import java.awt.flowlayout; 3 import java.awt.gridlayout; 4 import java.awt.event.actionevent; 5 import java.awt.event.actionlistener; 6 7 import javax.swing.jbutton; 8 import javax.swing.jframe; 9 import javax.swing.jlabel; 10 import javax.swing.jpanel; 11 import javax.swing.jtextfield; 12 13 /** 14 * Calculates GCD of two integers. GUI allows spawning of a new window. 15 * @author <a href="mailto:seanb@cs.man.ac.uk">sean Bechhofer</a> 16 */ 17 public class GCDWithNew extends JFrame implements ActionListener { 18 19 private JTextField number1jtextfield = new JTextField(20); 20 private JTextField number2jtextfield = new JTextField(20); 21 private JTextField resultjtextfield = new JTextField(20); 22 23 private JButton computejbutton = new JButton("Compute"); 24 private JButton newjbutton = new JButton("New"); 25 26 public GCDWithNew() { 27 settitle("gcd"); 28 Container contents = getcontentpane(); 29 contents.setlayout(new GridLayout(0, 1)); 30 31 JPanel numberfieldspanel = new JPanel(); 32 numberfieldspanel.setlayout( new GridLayout( 0, 2 ) ); 33 contents.add( numberfieldspanel ); 34 35 numberfieldspanel.add(new JLabel("Number 1")); 36 numberfieldspanel.add(new JLabel("Number 2")); 37 numberfieldspanel.add(number1jtextfield); 38 numberfieldspanel.add(number2jtextfield); 39 1

40 JPanel buttonandresultpanel = new JPanel(); 41 buttonandresultpanel.setlayout( new GridLayout( 0, 2 ) ); 42 contents.add( buttonandresultpanel ); 43 44 JPanel buttonpanel = new JPanel(); 45 buttonpanel.setlayout( new FlowLayout() ); 46 buttonandresultpanel.add( buttonpanel ); 47 48 buttonpanel.add(computejbutton); 49 computejbutton.addactionlistener(this); 50 51 buttonpanel.add(newjbutton); 52 newjbutton.addactionlistener(this); 53 54 JPanel resultpanel = new JPanel(); 55 resultpanel.setlayout( new GridLayout( 0, 1 ) ); 56 buttonandresultpanel.add( resultpanel ); 57 58 resultpanel.add(new JLabel("GCD of Number 1 and Number 2")); 59 resultpanel.add(resultjtextfield); 60 /* Specify appropriate behaviour when the window is closed */ 61 setdefaultcloseoperation(exit_on_close); 62 63 pack(); 64 } // GCDWithNew 65 66 public void actionperformed(actionevent e) { 67 if ( e.getsource() == computejbutton ) { 68 int number1 = Integer.parseInt(number1JTextField.getText()); 69 int number2 = Integer.parseInt(number2JTextField.getText()); 70 int thegcd = greatestcommondivisor(number1, number2); 71 resultjtextfield.settext("" + thegcd); 72 } else if ( e.getsource() == newjbutton ) { 73 GCDWithNew newone = new GCDWithNew(); 74 newone.setvisible( true ); 75 } 76 } // actionperformed 77 78 private static int greatestcommondivisor(int multiple1ofgcd, 79 int multiple2ofgcd) { 80 // Both multiple1ofgcd and multiple2ofgcd must be non-negative 81 while (multiple1ofgcd!= multiple2ofgcd) 82 if (multiple1ofgcd > multiple2ofgcd) 83 multiple1ofgcd = multiple1ofgcd - multiple2ofgcd; 84 else 85 multiple2ofgcd = multiple2ofgcd - multiple1ofgcd; 86 return multiple1ofgcd; 87 } // greatestcommondivisor 88 89 public static void main(string [] args) { 2

90 GCDWithNew thegcd = new GCDWithNew(); 91 thegcd.setvisible( true ); 92 } // main 93 } // class GCDWithNew 3

2 GCDWithNew2 1 import javax.swing.jtextfield; 2 import javax.swing.jframe; 3 import java.awt.event.actionlistener; 4 import javax.swing.jbutton; 5 import java.awt.event.actionevent; 6 import java.awt.gridlayout; 7 import javax.swing.jlabel; 8 import java.awt.container; 9 import javax.swing.jpanel; 10 import java.awt.flowlayout; 11 import java.awt.point; 12 13 /** 14 * Calculates GCD of two integers. GUI allows spawning of a new window. 15 * @author <a href="mailto:seanb@cs.man.ac.uk">sean Bechhofer</a> 16 */ 17 public class GCDWithNew2 extends JFrame implements ActionListener { 18 19 private JTextField number1jtextfield = new JTextField(20); 20 private JTextField number2jtextfield = new JTextField(20); 21 private JTextField resultjtextfield = new JTextField(20); 22 23 private JButton computejbutton = new JButton("Compute"); 24 private JButton newjbutton = new JButton("New"); 25 26 public GCDWithNew2() { 27 settitle("gcd"); 28 Container contents = getcontentpane(); 29 contents.setlayout(new GridLayout(0, 1)); 30 31 JPanel numberfieldspanel = new JPanel(); 32 numberfieldspanel.setlayout( new GridLayout( 0, 2 ) ); 33 contents.add( numberfieldspanel ); 34 35 numberfieldspanel.add(new JLabel("Number 1")); 36 numberfieldspanel.add(new JLabel("Number 2")); 37 numberfieldspanel.add(number1jtextfield); 38 numberfieldspanel.add(number2jtextfield); 39 40 JPanel buttonandresultpanel = new JPanel(); 41 buttonandresultpanel.setlayout( new GridLayout( 0, 2 ) ); 42 contents.add( buttonandresultpanel ); 43 44 JPanel buttonpanel = new JPanel(); 45 buttonpanel.setlayout( new FlowLayout() ); 46 buttonandresultpanel.add( buttonpanel ); 47 48 buttonpanel.add(computejbutton); 49 computejbutton.addactionlistener(this); 4

50 51 buttonpanel.add(newjbutton); 52 newjbutton.addactionlistener(this); 53 54 JPanel resultpanel = new JPanel(); 55 resultpanel.setlayout( new GridLayout( 0, 1 ) ); 56 buttonandresultpanel.add( resultpanel ); 57 58 resultpanel.add(new JLabel("GCD of Number 1 and Number 2")); 59 resultpanel.add(resultjtextfield); 60 /* Specify appropriate behaviour when the window is closed */ 61 setdefaultcloseoperation(exit_on_close); 62 63 pack(); 64 } // GCDWithNew2 65 66 public void actionperformed(actionevent e) { 67 if ( e.getsource() == computejbutton ) { 68 int number1 = Integer.parseInt(number1JTextField.getText()); 69 int number2 = Integer.parseInt(number2JTextField.getText()); 70 int thegcd = greatestcommondivisor(number1, number2); 71 resultjtextfield.settext("" + thegcd); 72 } else if ( e.getsource() == newjbutton ) { 73 GCDWithNew2 newone = new GCDWithNew2(); 74 newone.setlocation( new Point( this.getlocation().x + 10, 75 this.getlocation().y + 10 ) ); 76 newone.setvisible( true ); 77 } 78 } // actionperformed 79 80 private static int greatestcommondivisor(int multiple1ofgcd, 81 int multiple2ofgcd) { 82 // Both multiple1ofgcd and multiple2ofgcd must be non-negative 83 while (multiple1ofgcd!= multiple2ofgcd) 84 if (multiple1ofgcd > multiple2ofgcd) 85 multiple1ofgcd = multiple1ofgcd - multiple2ofgcd; 86 else 87 multiple2ofgcd = multiple2ofgcd - multiple1ofgcd; 88 return multiple1ofgcd; 89 } // greatestcommondivisor 90 91 public static void main(string [] args) { 92 GCDWithNew2 thegcd = new GCDWithNew2(); 93 thegcd.setvisible( true ); 94 } // main 95 } // class GCDWithNew2 5

3 GCDWithNew3 1 import javax.swing.jtextfield; 2 import javax.swing.jframe; 3 import java.awt.event.actionlistener; 4 import javax.swing.jbutton; 5 import java.awt.event.actionevent; 6 import java.awt.gridlayout; 7 import javax.swing.jlabel; 8 import java.awt.container; 9 import javax.swing.jpanel; 10 import java.awt.flowlayout; 11 import java.awt.point; 12 13 /** 14 * Calculates GCD of two integers. GUI allows spawning of a new window. 15 * @author <a href="mailto:seanb@cs.man.ac.uk">sean Bechhofer</a> 16 */ 17 public class GCDWithNew3 extends JFrame implements ActionListener { 18 19 private JTextField number1jtextfield = new JTextField(20); 20 private JTextField number2jtextfield = new JTextField(20); 21 private JTextField resultjtextfield = new JTextField(20); 22 23 private JButton computejbutton = new JButton("Compute"); 24 private JButton newjbutton = new JButton("New"); 25 26 public GCDWithNew3() { 27 settitle("gcd"); 28 Container contents = getcontentpane(); 29 contents.setlayout(new GridLayout(0, 1)); 30 31 JPanel numberfieldspanel = new JPanel(); 32 numberfieldspanel.setlayout( new GridLayout( 0, 2 ) ); 33 contents.add( numberfieldspanel ); 34 35 numberfieldspanel.add(new JLabel("Number 1")); 36 numberfieldspanel.add(new JLabel("Number 2")); 37 numberfieldspanel.add(number1jtextfield); 38 numberfieldspanel.add(number2jtextfield); 39 40 JPanel buttonandresultpanel = new JPanel(); 41 buttonandresultpanel.setlayout( new GridLayout( 0, 2 ) ); 42 contents.add( buttonandresultpanel ); 43 44 JPanel buttonpanel = new JPanel(); 45 buttonpanel.setlayout( new FlowLayout() ); 46 buttonandresultpanel.add( buttonpanel ); 47 48 buttonpanel.add(computejbutton); 49 computejbutton.addactionlistener(this); 6

50 51 buttonpanel.add(newjbutton); 52 newjbutton.addactionlistener(this); 53 54 JPanel resultpanel = new JPanel(); 55 resultpanel.setlayout( new GridLayout( 0, 1 ) ); 56 buttonandresultpanel.add( resultpanel ); 57 58 resultpanel.add(new JLabel("GCD of Number 1 and Number 2")); 59 resultpanel.add(resultjtextfield); 60 /* Specify appropriate behaviour when the window is closed */ 61 setdefaultcloseoperation(dispose_on_close); 62 63 pack(); 64 } // GCDWithNew3 65 66 public void actionperformed(actionevent e) { 67 if ( e.getsource() == computejbutton ) { 68 int number1 = Integer.parseInt(number1JTextField.getText()); 69 int number2 = Integer.parseInt(number2JTextField.getText()); 70 int thegcd = greatestcommondivisor(number1, number2); 71 resultjtextfield.settext("" + thegcd); 72 } else if ( e.getsource() == newjbutton ) { 73 GCDWithNew3 newone = new GCDWithNew3(); 74 newone.setlocation( new Point( this.getlocation().x + 10, 75 this.getlocation().y + 10 ) ); 76 newone.setvisible( true ); 77 } 78 } // actionperformed 79 80 private static int greatestcommondivisor(int multiple1ofgcd, 81 int multiple2ofgcd) { 82 // Both multiple1ofgcd and multiple2ofgcd must be non-negative 83 while (multiple1ofgcd!= multiple2ofgcd) 84 if (multiple1ofgcd > multiple2ofgcd) 85 multiple1ofgcd = multiple1ofgcd - multiple2ofgcd; 86 else 87 multiple2ofgcd = multiple2ofgcd - multiple1ofgcd; 88 return multiple1ofgcd; 89 } // greatestcommondivisor 90 91 public static void main(string [] args) { 92 GCDWithNew3 thegcd = new GCDWithNew3(); 93 thegcd.setvisible( true ); 94 } // main 95 } // class GCDWithNew3 7

4 LogGUI 1 import javax.swing.jframe; 2 import java.awt.event.actionlistener; 3 import javax.swing.jtextfield; 4 import javax.swing.jtextarea; 5 import java.awt.container; 6 import javax.swing.jbutton; 7 import java.awt.borderlayout; 8 import java.awt.event.actionevent; 9 import java.awt.gridlayout; 10 import javax.swing.jpanel; 11 import javax.swing.jlabel; 12 13 /** 14 * Simple GUI that allows users to log messages. 15 * @author <a href="mailto:seanb@cs.man.ac.uk">sean Bechhofer</a> 16 */ 17 public class LogGUI extends JFrame implements ActionListener { 18 19 /* GUI Components */ 20 private JTextField labelfield = new JTextField("label"); 21 private JTextArea messagetextarea = new JTextArea(15, 20); 22 private JButton logbutton = new JButton("Log"); 23 private JButton newbutton = new JButton("New"); 24 25 /* The LogBook that we will log messages to */ 26 private LogBook logbook; 27 28 /** 29 * Creates a new <code>loggui</code> instance. 30 * 31 * @param alogbook The <code>logbook</code> that the GUI is 32 * connected to. 33 */ 34 public LogGUI( LogBook alogbook ) { 35 logbook = alogbook; 36 settitle("log"); 37 Container contents = getcontentpane(); 38 contents.setlayout(new BorderLayout()); 39 40 JPanel labelpanel = new JPanel( new GridLayout( 0, 2 ) ); 41 labelpanel.add( new JLabel( "Label:" ) ); 42 labelpanel.add( labelfield ); 43 44 45 contents.add( labelpanel, BorderLayout.NORTH ); 46 contents.add( messagetextarea, BorderLayout.CENTER ); 47 48 JPanel buttonpanel = new JPanel( new GridLayout( 0, 2 ) ); 49 buttonpanel.add( logbutton ); 8

50 logbutton.addactionlistener( this ); 51 52 buttonpanel.add( newbutton ); 53 newbutton.addactionlistener( this ); 54 55 contents.add( buttonpanel, BorderLayout.SOUTH ); 56 57 setdefaultcloseoperation( DISPOSE_ON_CLOSE ); 58 pack(); 59 } // LogGUI 60 61 public void actionperformed( ActionEvent e ) { 62 if ( e.getsource() == logbutton ) { 63 /* Log the message */ 64 logbook.logmessage( labelfield.gettext(), 65 messagetextarea.gettext() ); 66 /* Now clear the text */ 67 messagetextarea.settext(""); 68 } else if ( e.getsource() == newbutton ) { 69 /* Create a new GUI with the same logbook */ 70 LogGUI newloggui = new LogGUI( logbook ); 71 newloggui.setvisible( true ); 72 } 73 } // actionperformed 74 75 public static void main(string [] args) { 76 LogBook logbook = new LogBook( "My LogBook" ); 77 LogGUI theloggui = new LogGUI( logbook ); 78 theloggui.setvisible( true ); 79 } // main 80 81 } // class LogGUI 82 83 9

5 LogBook 1 import java.text.simpledateformat; 2 import java.util.date; 3 4 /** 5 * A class that records information. 6 * @author <a href="mailto:seanb@cs.man.ac.uk">sean Bechhofer</a> 7 */ 8 public class LogBook { 9 10 /* private String logname; */ 11 private String log; 12 private SimpleDateFormat dateformat; 13 14 /** 15 * Create a new <code>logbook</code> instance. 16 * 17 * @param alogname name of the room. 18 */ 19 public LogBook( String alogname ) { 20 /* logname = alogname; */ 21 dateformat = new SimpleDateFormat( "hh:mm:ss " ); 22 log = ""; 23 } 24 25 /** 26 * Log a message. 27 * 28 * @param user a <code>string</code> value 29 * @param message a <code>string</code> value 30 */ 31 public void logmessage( String label, 32 String message ) { 33 Date now = new Date(); 34 35 String logmessage = "Time: " + dateformat.format( now ) + "\n" + 36 "Label: " + label + "\n"+ 37 "Note: " + message; 38 39 /* Add the message to the log */ 40 log = log + logmessage + "\n"; 41 42 /* Display the message */ 43 System.out.println( "-------------------------------" ); 44 System.out.println( logmessage ); 45 System.out.println( "-------------------------------" ); 46 } 47 } 10

6 LogGUI2 1 import javax.swing.jframe; 2 import java.awt.event.actionlistener; 3 import javax.swing.jtextfield; 4 import javax.swing.jtextarea; 5 import java.awt.container; 6 import javax.swing.jbutton; 7 import java.awt.borderlayout; 8 import java.awt.event.actionevent; 9 import java.awt.gridlayout; 10 import javax.swing.jpanel; 11 import javax.swing.jlabel; 12 13 /** 14 * Simple GUI2 that allows labels to log messages. 15 * @author <a href="mailto:seanb@cs.man.ac.uk">sean Bechhofer</a> 16 */ 17 public class LogGUI2 extends JFrame implements ActionListener { 18 19 /* GUI Components */ 20 private JTextField labelfield = new JTextField("label"); 21 private JTextArea messagetextarea = new JTextArea(15, 20); 22 private JButton logbutton = new JButton("Log"); 23 private JButton newbutton = new JButton("New"); 24 private JButton transcriptbutton = new JButton("Transcript"); 25 26 /* The LogBook that we will log messages to */ 27 private LogBook2 logbook; 28 29 /** 30 * Creates a new <code>loggui2</code> instance. 31 * 32 * @param alogbook The <code>logbook</code> that the GUI is 33 * connected to. 34 */ 35 public LogGUI2( LogBook2 alogbook ) { 36 logbook = alogbook; 37 settitle("log"); 38 Container contents = getcontentpane(); 39 contents.setlayout(new BorderLayout()); 40 41 JPanel labelpanel = new JPanel( new GridLayout( 0, 2 ) ); 42 labelpanel.add( new JLabel( "Label:" ) ); 43 labelpanel.add( labelfield ); 44 45 46 contents.add( labelpanel, BorderLayout.NORTH ); 47 contents.add( messagetextarea, BorderLayout.CENTER ); 48 49 JPanel buttonpanel = new JPanel( new GridLayout( 0, 3 ) ); 11

50 buttonpanel.add( logbutton ); 51 logbutton.addactionlistener( this ); 52 53 buttonpanel.add( newbutton ); 54 newbutton.addactionlistener( this ); 55 56 buttonpanel.add( transcriptbutton ); 57 transcriptbutton.addactionlistener( alogbook ); 58 59 contents.add( buttonpanel, BorderLayout.SOUTH ); 60 61 setdefaultcloseoperation( DISPOSE_ON_CLOSE ); 62 pack(); 63 } // LogGUI2 64 65 public void actionperformed( ActionEvent e ) { 66 if ( e.getsource() == logbutton ) { 67 /* Log the message */ 68 logbook.logmessage( labelfield.gettext(), 69 messagetextarea.gettext() ); 70 /* Now clear the text */ 71 messagetextarea.settext(""); 72 } else if ( e.getsource() == newbutton ) { 73 /* Create a new GUI with the same logbook */ 74 LogGUI2 newloggui = new LogGUI2( logbook ); 75 newloggui.setvisible( true ); 76 } 77 } // actionperformed 78 79 public static void main(string [] args) { 80 LogBook2 logbook = new LogBook2( "LogBook" ); 81 LogGUI2 theloggui = new LogGUI2( logbook ); 82 theloggui.setvisible( true ); 83 } // main 84 85 } // class LogGUI2 86 87 12

7 LogBook2 1 import java.text.simpledateformat; 2 import java.util.date; 3 import java.awt.event.actionlistener; 4 import java.awt.event.actionevent; 5 6 /** 7 * A class that records information. 8 * @author <a href="mailto:seanb@cs.man.ac.uk">sean Bechhofer</a> 9 */ 10 public class LogBook2 implements ActionListener { 11 12 /* private String logname; */ 13 private String log; 14 private SimpleDateFormat dateformat; 15 16 /** 17 * Create a new <code>logbook</code> instance. 18 * 19 * @param alogname name of the room. 20 */ 21 public LogBook2( String alogname ) { 22 /* logname = alogname; */ 23 dateformat = new SimpleDateFormat( "hh:mm:ss " ); 24 log = ""; 25 } 26 27 /** 28 * Log a message. 29 * 30 * @param user a <code>string</code> value 31 * @param message a <code>string</code> value 32 */ 33 public void logmessage( String label, 34 String message ) { 35 Date now = new Date(); 36 37 String logmessage = "Time: " + dateformat.format( now ) + "\n" + 38 "Label: " + label + "\n"+ 39 "Note: " + message; 40 41 /* Add the message to the log */ 42 log = log + logmessage + "\n"; 43 } 44 45 public void actionperformed( ActionEvent e ) { 46 System.out.println("========================"); 47 System.out.println( log ); 48 System.out.println("========================"); 49 } 13

50 } 14