Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object-Oriented Programming with Java Java with added Swing Lecture 3.

Similar presentations


Presentation on theme: "Object-Oriented Programming with Java Java with added Swing Lecture 3."— Presentation transcript:

1 Object-Oriented Programming with Java Java with added Swing Lecture 3

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.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 Rectangle length height area circumference

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

9  javac Rectangle.java  javac MakeRectangle.java  java MakeRectangle 3.0 2.0 6.0 

10 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

11 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

12 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

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

15 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

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

18 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

20 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

21 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

23 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

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

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


Download ppt "Object-Oriented Programming with Java Java with added Swing Lecture 3."

Similar presentations


Ads by Google