1 / 29

Dialogs

Dialogs. Displaying Text in a Dialog Box. Windows and dialog boxes Up to this our output has been to the screen Many Java applications use these to display output Package is javax.swing – contains classes for creation of GUIs.

xandy
Download Presentation

Dialogs

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

  2. Displaying Text in a Dialog Box Windows and dialog boxes Up to this our output has been to the screen Many Java applications use these to display output Package is javax.swing – contains classes for creation of GUIs. JOptionPane class provides prepackaged dialog boxes called message dialogs showMessageDialog() – displays a dialog box displaying a message. Two parameters , position of the dialog box and the message.

  3. Displaying Text in a Dialog Box // Printing multiple lines in dialog box. import javax.swing.JOptionPane; // import class JOptionPane public class Dialog1 { public Dialog1() { // display a dialog with the message JOptionPane.showMessageDialog( null, "Welcome\nto\nJava" ); } public static void main( String args[] ) { new Dialog1(); } // end main } // end class Dialog1 Import class JOptionPane Show a message dialog with text

  4. Displaying Text in a Dialog Box showMessageDialog() – displays a dialog box displaying a message. Two parameters , position of the dialog box and the message. showMessageDialog() is a classmethod of JOptionPane class , it is a static method. Such methods often define frequently used tasks that do not explicitly require the creation of an object. A static method typically is called by using its class name dot operator and the method name. JOptionPane.showMessageDialog( null, "Welcome\nto\nJava" ); Note we did not have to create an instance of the class JoptionPane to use the showMessageDialog() method.

  5. Entering Text in a Dialog Box Input dialog Allows user to input information Created using method showInputDialog from class JOptionPane

  6. Entering Text in a Dialog Box // Basic input with a dialog box. import javax.swing.JOptionPane; public class NameDialog { public NameDialog() { // prompt user to enter name String name =JOptionPane.showInputDialog( "What is your name?" ); // create the message String message = String.format( "Welcome, %s, to Java Programming!", name ); // display the message to welcome the user by name JOptionPane.showMessageDialog( null, message ); } public static void main( String args[] ) { new NameDialog(); } } // end class NameDialog

  7. Entering Text in a Dialog Box showInputDialog() – parameter is the prompt The user types characters in the text field, User enters OK - showInputDialog() returns a String. User enters Cancel - showInputDialog() returns null static String method format() –returns a formatted String similar to printf

  8. Displaying Text in a Dialog Box Windows and dialog boxes Up to this our output has been to the screen Many Java applications use these to display output Package is javax.swing – contains classes for creation of GUIs. JOptionPane class provides prepackaged dialog boxes called message dialogs showMessageDialog() – displays a dialog box displaying a message. Two parameters , position of the dialog box and the message.

  9. Displaying Text in a Dialog Box // Printing multiple lines in dialog box. import javax.swing.JOptionPane; // import class JOptionPane public class Dialog1 { public Dialog1() { // display a dialog with the message JOptionPane.showMessageDialog( null, "Welcome\nto\nJava" ); } public static void main( String args[] ) { new Dialog1(); } // end main } // end class Dialog1 Import class JOptionPane Show a message dialog with text

  10. Displaying Text in a Dialog Box showMessageDialog() – displays a dialog box displaying a message. Two parameters , position of the dialog box and the message. showMessageDialog() is a classmethod of JOptionPane class , it is a static method. Such methods often define frequently used tasks that do not explicitly require the creation of an object. A static method typically is called by using its class name dot operator and the method name. JOptionPane.showMessageDialog( null, "Welcome\nto\nJava" ); Note we did not have to create an instance of the class JoptionPane to use the showMessageDialog() method.

  11. Entering Text in a Dialog Box Input dialog Allows user to input information Created using method showInputDialog from class JOptionPane

  12. Entering Text in a Dialog Box // Basic input with a dialog box. import javax.swing.JOptionPane; public class NameDialog { public NameDialog() { // prompt user to enter name String name =JOptionPane.showInputDialog( "What is your name?" ); // create the message String message = String.format( "Welcome, %s, to Java Programming!", name ); // display the message to welcome the user by name JOptionPane.showMessageDialog( null, message ); } public static void main( String args[] ) { new NameDialog(); } } // end class NameDialog

  13. Entering Text in a Dialog Box showInputDialog() – parameter is the prompt The user types characters in the text field, User enters OK - showInputDialog() returns a String. User enters Cancel - showInputDialog() returns null static String method format() –returns a formatted String similar to printf

  14. Exercises • Adapt our Hello World and Leap Year programs to include Swing Input and Output

  15. Java packages Java Packages available with the JDK java.io – for system input and output java.math – for integer and decimal arithmetic java.text - for handling text, dates, numbers … java.util – for collections, date & time facilities also new class Scanner for Data input java.net – for implementing networking apps java.sql – for accessing data in a data source and many more…

  16. Class fields and class methods A class field is associated with the class in which it is defined rather than with each instance of the class. public static final double PI = 3.14159 The static modifier says that the field is a class field. The final modifier indicates that the value in the field does not change ie a constant

  17. Using Math Class All methods of the Math class are class methods Must use the class name to invoke the methods Math.round(x), Math.pow(x,y), Math.sqrt(y) … A utility type class A class field is associated with the class in which it is defined rather than with each instance of the class

  18. Class fields and class methods A class field is associated with the class in which it is defined rather than with each instance of the class. public static final double PI = 3.14159 The static modifier says that the field is a class field. The final modifier indicates that the value in the field does not change ie a constant

  19. Class fields and class methods A class methods are declared with the static modifier, they are associated with the class, rather than the instance. Public static double radinsToDegrees(double rads) { return rads *180/PI;} System.out.println(c+"\t"+ Math.round(c*9.0/5 + 32)); A class method can use any class fields and other class methods of its own class.

  20. static Methods in Class Math static method (or class method) Applies to the class as a whole instead of a specific object of the class Call a static method by using the method call:ClassName.methodName( arguments ) All methods of the Math class are static example: Math.sqrt( 900.0 )

  21. TemperatureTable class TemperatureTable { TemperatureTable() {System.out.println("Temperature Conversion Table");System.out.println("============================");System.out.println();System.out.println("C\tF"); for (int c = 5; c <= 20; c++) { System.out.println(c+"\t"+ Math.round(c*9.0/5 + 32)); } } public static void main ( String[] args) { new TemperatureTable (); } }

  22. static Fields and Class Math Constants Keyword final Cannot be changed after initialization static fields (or class variables) Are fields where one copy of the variable is shared among all objects of the class Math.PI and Math.E are final static fields of the Math class

  23. Math class methods.

  24. static Method main Method main main is declared static so it can be invoked without creating an object of the class containing main Any class can contain a main method The JVM invokes the main method belonging to the class specified by the first command-line argument to the java command

  25. Account example Account class Interest rate – have the same interest rate for all accounts Declare it as a class field / attribute public class Account { private int accountno; private String name; private String address; private double balance; static double interestrate = 3.5; //class field static void changeRate(double rate){ // class method interestrate = rate;} To change the interest rate , we need to create a class method to update it.

  26. Account example AccountTest class To change the interest rate , we use the class method . Use the Class name . Class method name Account.changeRate(4.5); //class method How would you update the account class to automatically allocate a new account number for each account created.

  27. Examples of Vectors

  28. import java.io.*; • import java.util.*; • class VectorApp{ public static void main(String[] args){ • Vector v = new Vector(20); • Integer j = null; • int i;

  29. System.out.println("Generating random integers between 1 and 100, wrapping it as integer objects and adding to a vector object"); • for(i=0;i<10;i++){ • j = new Integer((int)(Math.random() * 100)); v.addElement(j); • System.out.println("Added Integer object elements " + j); • } • System.out.println("size of the vector "+v.size()); System.out.println("capacity of the vector "+v.capacity()); Enumeration enum = v.elements(); • while(enum.hasMoreElements()) • System.out.println("Enumeration of the elements in the vector container "+(Integer)enum.nextElement()); • System.out.println(""); • } • }

More Related