1 / 25

Internet Software Development

Internet Software Development. Java with added Swing Lecture 3. // file: Welcome.java public class Welcome { public static void main(String[ ] args) { System.out.println(“Welcome to JavaWorld!”); } }. javac Welcome.java java Welcome Welcome to JavaWorld!.

mandel
Download Presentation

Internet Software Development

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Internet Software Development Java with added Swing Lecture 3

  2. // file: Welcome.java public class Welcome { public static void main(String[ ] args) { 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) { // So, “args” is an array of Strings. // “void” means no value is returned System.out.println(“Welcome to JavaWorld!”); // 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 PLUS Connectivity!

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

  10. 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 Data Entry with Swing import javax.swing.*; public class MakeRectangle { public static void main(String[] args) { String input = JOptionPane.showInputDialog("Enter length");

  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 }

  12. 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 } }

  13. The message to be displayed Note use of “+” to concatenate strings Parent component if relevant (“null” here) Prettier Output • Swing to the rescue again! • Need to display a different kind of dialog: • present a message • no field for the user to edit • Use JOptionPane again: JOptionPane.showMessageDialog(null, "The area is " + area);

  14. // MakeRectangle.java // Application with “complete” GUI 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(); JOptionPane.showMessageDialog(null, "The area is " + area); System.exit(0); } }

  15. The message Title bar string Icon type Improving the dialog • This was the simplest method for creating a message dialog • But we can: • alter the icon • specify the title bar string JOptionPane.showMessageDialog( null, "The area is " + area, "Result", JOptionPane.PLAIN_MESSAGE);

  16. Message dialog types • JOptionPane.ERROR_MESSAGE • Indicates an error to the application user • JOptionPane.INFORMATION_MESSAGE • Displays an informational message - the user simply dismisses the dialog when ready • JOptionPane.WARNING_MESSAGE • Warns the user of a potential problem

  17. Message dialog types • JOptionPane.QUESTION_MESSAGE • Poses a question - normally requires a Yes/No response from the user • JOptionPane.PLAIN_MESSAGE • Dialog that simply contains a message with no icon

  18. public class Circle { protected double radius; protected void checkRadius(double radius) { if (radius < 0.0) { JOptionPane.showMessageDialog(null, "Radius must not be negative", "Illegal argument", JOptionPane.ERROR_MESSAGE); System.exit(-1); } } public Circle(double radius) { checkRadius(radius); this.radius = radius; } public double area() {return PI*radius*radius; } public double circumference() {return 2*PI*radius; } }

  19. More robust Classes • Declare fields as Private or Protected • 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 • E.g. public void setRadius(double radius) { checkRadius(radius); this.radius = radius; }

  20. public class Circle { public static final double PI = 3.14159; // a constant protected double radius; protected void checkRadius(double radius) { if (radius < 0.0) { throw new IllegalArgumentException("radius must not be negative"); } } public Circle(double radius) { checkRadius(radius); this.radius = radius; } public double getRadius() {return radius;} public void setRadius(double radius) { checkRadius(radius); this.radius = radius; } public double area() {return PI*radius*radius; } public double circumference() {return 2*PI*radius; } }

More Related