Download Dialogs

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
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.
– 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.
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
Import );class JOptionPane
JOptionPane.showMessageDialog( null, "Welcome\nto\nJava"
}
public static void main( String args[] )
{
new Dialog1();
} // end main
} // end class Dialog1
Show a message dialog with text
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 class method 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.
Entering Text in a Dialog Box
• Input dialog
– Allows user to input information
– Created using method
showInputDialog from class
JOptionPane
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
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
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.
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
Import );class JOptionPane
JOptionPane.showMessageDialog( null, "Welcome\nto\nJava"
}
public static void main( String args[] )
{
new Dialog1();
} // end main
} // end class Dialog1
Show a message dialog with text
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 class method 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.
Entering Text in a Dialog Box
• Input dialog
– Allows user to input information
– Created using method
showInputDialog from class
JOptionPane
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
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
• Exercises
• Adapt our Hello World and Leap Year
programs to include Swing Input and
Output
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…
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
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
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
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.
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 )
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 ();
}
}
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
Math class methods.
Method
Description
Example
abs( x )
absolute value of x
ceil( x )
rounds x to the smallest integer not
less than x
abs( 0.0 ) is 0.0
abs( -23.7 ) is 23.7
ceil( 9.2 ) is 10.0
ceil( -9.8 ) is -9.0
cos( x )
trigonometric cosine of x (x in radians)
cos( 0.0 ) is 1.0
exp( x )
exponential method ex
floor( x )
rounds x to the largest integer not greater Floor( 9.2 ) is 9.0
than x
floor( -9.8 ) is -10.0
log( x )
natural logarithm of x (base e)
abs( 23.7 ) is 23.7
max( x, y ) larger value of x and y
min( x, y ) smaller value of x and y
exp( 1.0 ) is 2.71828
exp( 2.0 ) is 7.38906
log( Math.E ) is 1.0
log( Math.E * Math.E ) is 2.0
max( 2.3, 12.7 ) is 12.7
max( -2.3, -12.7 ) is -2.3
min( 2.3, 12.7 ) is 2.3
min( -2.3, -12.7 ) is -12.7
pow( x, y ) x raised to the power y (i.e., xy)
pow( 2.0, 7.0 ) is 128.0
pow( 9.0, 0.5 ) is 3.0
sin( x )
trigonometric sine of x (x in radians)
sin( 0.0 ) is 0.0
sqrt( x )
square root of x
sqrt( 900.0 ) is 30.0
tan( x )
trigonometric tangent of x (x in radians)
tan( 0.0 ) is 0.0
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
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){
interestrate = rate;}
// class method
– To change the interest rate , we need to create a class
method to update it.
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.
Examples of Vectors
• import java.io.*;
• import java.util.*;
• class VectorApp{ public static void
main(String[] args){
• Vector v = new Vector(20);
• Integer j = null;
• int i;
•
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("");
•
}
•
}