Download presentation
Presentation is loading. Please wait.
Published byJonah Alexander Modified over 8 years ago
1
Component-Based Software Engineering Java with added Swing Paul J Krause
2
// file: Welcome.java public class Welcome { public static void main(String[ ] args) { public static void main(String[ ] args) { System.out.println(“Welcome to JavaWorld!”); System.out.println(“Welcome to JavaWorld!”); }}
3
javac Welcome.java java Welcome Welcome to JavaWorld!
4
// file: Welcome.java // Always introduce the class public class Welcome { // Class name must match file name for public classes public static void main(String[ ] args) { public static void main(String[ ] args) { // So, “args” is an array of Strings. // “void” means no value is returned System.out.println(“Welcome to JavaWorld!”); System.out.println(“Welcome to JavaWorld!”); // System is a class that interfaces to system facilities // System is a class that interfaces to system facilities // “out” is the standard output stream of System
5
String Handling is Simple System.out.println(“Welcome to JavaWorld\n” + “\tObject-Oriented Programming\n” + “\tPLUS\n” + “\tConnectivity!”); Java Welcome Welcome to JavaWorld Object-Oriented Programming PLUSConnectivity!
6
A Java Class Rectangle length height area circumference // Rectangle.java // A straightforward implementation of the Java Rectangle class public class Rectangle { public double length; public double height; public Rectangle(double length, double height) { this.length = length; this.height = height; } public double area( ) { return length*height; } public double circumference( ) { return 2*(length+height); } }
7
public class MakeRectangle { public static void main(String[ ] args) { double length = Double.parseDouble(args[0]); // parse first argument to double double height = Double.parseDouble(args[1]); // parse second argument to double Rectangle rectangle = new Rectangle(length, height); double area = rectangle.area(); // calculate area System.out.println(area); // display it to the user }}
8
javac Rectangle.java javac MakeRectangle.java java MakeRectangle 3.0 2.0 6.0
9
Java Foundation Classes Provides Graphics and GUI capability for Java applications See: Java Foundation Classes In a Nutshell, by David Flanagan Provides a reference, but its not good as an introduction Provides a reference, but its not good as an introduction
10
Data Entry with Swing import javax.swing.*; public class MakeRectangle { public static void main(String[] args) { String input = JOptionPane.showInputDialog("Enter length"); JOptionPane.showInputDialog("Enter length"); A Class in Swing that can be used to display simple dialog boxes to the user A method in JOptionPane that displays a simple data entry dialog
11
Modified MakeRectangle import javax.swing.*; public class MakeRectangle { public static void main(String[ ] args) { String input = JOptionPane.showInputDialog("Enter length"); double length = Double.parseDouble(input); input = JOptionPane.showInputDialog("Enter height"); double height = Double.parseDouble(input); Rectangle rectangle = new Rectangle(length, height); double area = rectangle.area(); System.out.println(area); }// This will turn out to be an unsatisfactory termination }
13
GUI application termination import javax.swing.*; public class MakeRectangle { public static void main(String[ ] args) { String input = JOptionPane.showInputDialog("Enter length"); double length = Double.parseDouble(input);... Rectangle rectangle = new Rectangle(length, height); double area = rectangle.area(); System.out.println(area); System.exit(0);// terminates the program - this is required // for any program using a GUI }}
14
Prettier Output Swing to the rescue again! Need to display a different kind of dialog: present a message present a message no field for the user to edit no field for the user to edit Use JOptionPane again: JOptionPane.showMessageDialog(null, "The area is " + area); JOptionPane.showMessageDialog(null, "The area is " + area); Parent component if relevant (“null” here) The message to be displayed Note use of “+” to concatenate strings
15
// MakeRectangle.java // Application with “complete” GUI import javax.swing.*; public class MakeRectangle { public static void main(String[ ] args) { public static void main(String[ ] args) { String input = JOptionPane.showInputDialog("Enter length"); String input = JOptionPane.showInputDialog("Enter length"); double length = Double.parseDouble(input); double length = Double.parseDouble(input); input = JOptionPane.showInputDialog("Enter height"); input = JOptionPane.showInputDialog("Enter height"); double height = Double.parseDouble(input); double height = Double.parseDouble(input); Rectangle rectangle = new Rectangle(length, height); Rectangle rectangle = new Rectangle(length, height); double area = rectangle.area(); double area = rectangle.area(); JOptionPane.showMessageDialog(null, "The area is " + area); JOptionPane.showMessageDialog(null, "The area is " + area); System.exit(0); System.exit(0);}}
17
Improving the dialog This was the simplest method for creating a message dialog But we can: alter the icon alter the icon specify the title bar string specify the title bar stringJOptionPane.showMessageDialog( null, "The area is " + area, null, "The area is " + area, "Result ", JOptionPane.PLAIN_MESSAGE); "Result ", JOptionPane.PLAIN_MESSAGE); The message Title bar string Icon type
19
Message dialog types JOptionPane.ERROR_MESSAGE Indicates an error to the application user Indicates an error to the application user JOptionPane.INFORMATION_MESSAGE Displays an informational message - the user simply dismisses the dialog when ready Displays an informational message - the user simply dismisses the dialog when ready JOptionPane.WARNING_MESSAGE Warns the user of a potential problem Warns the user of a potential problem
20
Message dialog types JOptionPane.QUESTION_MESSAGE Poses a question - normally requires a Yes/No response from the user Poses a question - normally requires a Yes/No response from the user JOptionPane.PLAIN_MESSAGE Dialog that simply contains a message with no icon Dialog that simply contains a message with no icon
22
public class Circle { protected double radius; protected double radius; protected void checkRadius(double radius) { protected void checkRadius(double radius) { if (radius < 0.0) { if (radius < 0.0) { JOptionPane.showMessageDialog(null, JOptionPane.showMessageDialog(null, "Radius must not be negative", "Radius must not be negative", "Illegal argument", JOptionPane.ERROR_MESSAGE); System.exit(-1); System.exit(-1); } } public Circle(double radius) { public Circle(double radius) { checkRadius(radius); checkRadius(radius); this.radius = radius; this.radius = radius; } public double area() {return PI*radius*radius; } public double area() {return PI*radius*radius; } public double circumference() {return 2*PI*radius; } public double circumference() {return 2*PI*radius; }}
24
More robust Classes Declare fields as Private or Protected Protected fields can be accessed by subclasses or members of the same package Protected fields can be accessed by subclasses or members of the same package Declare public “get” and “set” methods with appropriate checks on the “set” methods with appropriate checks on the “set” methods E.g. public void setRadius(double radius) { checkRadius(radius); this.radius = radius; } this.radius = radius; }
25
public class Circle { public static final double PI = 3.14159; // a constant public static final double PI = 3.14159; // a constant protected double radius; protected double radius; protected void checkRadius(double radius) { protected void checkRadius(double radius) { if (radius < 0.0) { if (radius < 0.0) { throw new IllegalArgumentException("radius must not be negative"); } throw new IllegalArgumentException("radius must not be negative"); } } public Circle(double radius) { public Circle(double radius) { checkRadius(radius); checkRadius(radius); this.radius = radius; this.radius = radius; } public double getRadius() {return radius;} public double getRadius() {return radius;} public void setRadius(double radius) { public void setRadius(double radius) { checkRadius(radius); checkRadius(radius); this.radius = radius; this.radius = radius; } public double area() {return PI*radius*radius; } public double area() {return PI*radius*radius; } public double circumference() {return 2*PI*radius; } public double circumference() {return 2*PI*radius; }}
26
Catching Exceptions Use the following construction: try { // code that may throw exceptions } catch (AnException e1) { // Code to handle this kind of exception } finally { // Code that will always execute }
27
Trying to succeed try { // Normally this block of code will be // executed successfully. // However, if an exception is thrown // execution will stop and the interpreter // will attempt to find an appropriate // “catch” statement. }
28
Catching Exceptions There may be several “catch” blocks for different kinds of exceptions catch (AnException e1) { // This block will be invoked if an // AnException kind of exception is // thrown, or a sub-type of AnException. // The block can refer to the exception // object by its name “e1” }
29
And Finally finally { // This block contains code that will // always be run // - even if control leaves the “try” block // - even if control leaves the “try” block // because of a, or // because of a return, continue or // statement. // break statement. // It will, however, be skipped if there is a // It will, however, be skipped if there is a // System.exit( ) clause in the “try” block
30
public class MakeCircle { public static void main(String[ ] args) { public static void main(String[ ] args) { // details of input omitted // details of input omitted try { try { Circle circle = new Circle(radius); Circle circle = new Circle(radius); double area = circle.area( ); double area = circle.area( ); JOptionPane.showMessageDialog( JOptionPane.showMessageDialog( null, "The area equals " + area, null, "The area equals " + area, "Not a lot of people know that", JOptionPane.PLAIN_MESSAGE) ; } catch(IllegalArgumentException e1) { catch(IllegalArgumentException e1) { JOptionPane.showMessageDialog(null, "Radius must not be negative", JOptionPane.showMessageDialog(null, "Radius must not be negative", "Illegal argument", JOptionPane.ERROR_MESSAGE) ; } finally { finally { System.exit(0); System.exit(0); } }}
33
Some kinds-of Exception ExceptionClassNotFoundExceptionIllegalAccessExceptionRuntimeExceptionArithmeticExceptionArrayStoreException Illegal ArgumentException …
34
A little bit of formatting With double precision numbers you may find you get outputs with an unrealistic degree of precision E.g. 7 figures after the decimal place E.g. 7 figures after the decimal place This looks messy and the precision is normally meaningless
37
The java.text Package As an example, we will use the DecimalFormat class from this package Create an instance of DecimalFormat with the required format pattern Invoke the “format” method of this instance to create a formatted string representation
38
import javax.swing.*; import java.text.DecimalFormat; public class MakeCircle { public static void main(String[ ] args) { public static void main(String[ ] args) { String input = JOptionPane.showInputDialog("Enter radius"); String input = JOptionPane.showInputDialog("Enter radius"); double radius = Double.parseDouble(input); double radius = Double.parseDouble(input); DecimalFormat twoDigits = new DecimalFormat( "0.00"); DecimalFormat twoDigits = new DecimalFormat( "0.00"); try { try { Circle circle = new Circle(radius); Circle circle = new Circle(radius); double area = circle.area(); double area = circle.area(); JOptionPane.showMessageDialog( JOptionPane.showMessageDialog( null, "The area equals " + twoDigits.format( area ), null, "The area equals " + twoDigits.format( area ), "Not a lot of people know that", JOptionPane.PLAIN_MESSAGE); "Not a lot of people know that", JOptionPane.PLAIN_MESSAGE); } // Rest of code omitted } One or more digits in front of the decimal point Only two digits behind the decimal point
40
Informing Exceptions Remember that in the “main” method of MakeCircle, we explicitly included the error message: catch(IllegalArgumentException e1) { JOptionPane.showMessageDialog(null, "Radius must not be negative", JOptionPane.showMessageDialog(null, "Radius must not be negative", "Illegal argument", JOptionPane.ERROR_MESSAGE) ; } Should not do this - there may be several different arguments that could throw the same exception type
41
Inside Circle.java protected void checkRadius(double radius) { protected void checkRadius(double radius) { if (radius < 0.0) { if (radius < 0.0) { throw new IllegalArgumentException("radius must not be negative"); throw new IllegalArgumentException("radius must not be negative"); } } Inside MakeCircle.java catch(IllegalArgumentException e1) { JOptionPane.showMessageDialog(null, e1.toString(), JOptionPane.showMessageDialog(null, e1.toString(), "Illegal argument", JOptionPane.ERROR_MESSAGE); "Illegal argument", JOptionPane.ERROR_MESSAGE); }
43
toString methods Useful to define these for key classes Provides an easy way of summarising the state of an instance of a class Can by very useful for debugging
44
public class Point { // The notes show this class with more extensive commenting // The notes show this class with more extensive commenting protected double x, y;// Coordinates of the point accessible to subclasses protected double x, y;// Coordinates of the point accessible to subclasses public Point(double x, double y) { setPoint( x, y ); } public Point(double x, double y) { setPoint( x, y ); } // method sets the location of a point // method sets the location of a point public void setPoint(double xCoordinate, double yCoordinate) { public void setPoint(double xCoordinate, double yCoordinate) { x = xCoordinate; x = xCoordinate; y = yCoordinate; y = yCoordinate; } // conversion of a Point object to a string representation public String toString( ) { public String toString( ) { return("[" + x + ", " + y + "]"); }}
45
import javax.swing.*; public class MakePoint { public static void main(String[ ] args) { public static void main(String[ ] args) { String input; String input; input = JOptionPane.showInputDialog("Enter x Coordinate"); input = JOptionPane.showInputDialog("Enter x Coordinate"); double x = Double.parseDouble(input); double x = Double.parseDouble(input); input = JOptionPane.showInputDialog("Enter y Coordinate"); input = JOptionPane.showInputDialog("Enter y Coordinate"); double y = Double.parseDouble(input); double y = Double.parseDouble(input); try { try { Point point = new Point( x, y); Point point = new Point( x, y); JOptionPane.showMessageDialog( JOptionPane.showMessageDialog( null, "New point created: " + point.toString( ), null, "New point created: " + point.toString( ), "Pointed Message", JOptionPane.PLAIN_MESSAGE); "Pointed Message", JOptionPane.PLAIN_MESSAGE); } // rest of class definition omitted } // rest of class definition omitted
47
Adding properties to Points Point xyxy setLocation toString Location Circle radius area circumference plus Extension
48
public class Circle extends Point { public static final double PI = 3.14159; // a constant public static final double PI = 3.14159; // a constant protected double radius; protected double radius; protected void checkRadius(double radius) { // details omitted } protected void checkRadius(double radius) { // details omitted } public Circle(double x, double y, double radius) { public Circle(double x, double y, double radius) { super(x, y); super(x, y); checkRadius(radius); checkRadius(radius); this.radius = radius; this.radius = radius; } public String toString( ) {// Override Point.toString to add info. public String toString( ) {// Override Point.toString to add info. return "This is a Circle object with the following properties\n" + return "This is a Circle object with the following properties\n" + "Location: " + super.toString() + "\n" + "Location: " + super.toString() + "\n" + "Radius = " + radius + "\n"; "Radius = " + radius + "\n"; }}
49
main method of MakeCircle public void main(String[ ], args) { // … details omitted Circle circle = new Circle(x, y, radius); Circle circle = new Circle(x, y, radius); JOptionPane.showMessageDialog( JOptionPane.showMessageDialog( null, circle.toString( ), null, circle.toString( ), "Something New", JOptionPane.PLAIN_MESSAGE); "Something New", JOptionPane.PLAIN_MESSAGE); // and the rest... // and the rest...}
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.