joptionpane class

Upload: mrchung

Post on 02-Jun-2018

222 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/10/2019 JOptionPane Class

    1/19

    JOptionPaneclass

  • 8/10/2019 JOptionPane Class

    2/19

    DialogBoxes

    Adialog

    box

    isasmallgraphicalwindowthatdisplaysamessagetotheuserorrequestsinput.

    AvarietyofdialogboxescanbedisplayedusingtheJOptionPaneclass.

    Twoofthedialogboxesare:

    MessageDialog/ adialogboxthatdisplaysamessage.

    InputDialog/ adialogboxthatpromptstheuserforinput.

  • 8/10/2019 JOptionPane Class

    3/19

    UsingtheimportStatement

    TheJOptionPaneclassisnotautomaticallyavailabletoyourJavaprograms.

    Thefollowingstatementmustbebeforetheprogramsclassheader:

    import javax.swing.JOptionPane;

    Thisstatementtellsthecompiler

    wheretofindtheJOptionPaneclass.

  • 8/10/2019 JOptionPane Class

    4/19

    DialogBoxes

    TheJOptionPaneclassprovidesstaticmethodstodisplayeachtypeofdialogbox.

  • 8/10/2019 JOptionPane Class

    5/19

    MessageDialogs

    JOptionPane.showMessageDialogmethodisusedtodisplayamessagedialog.

    JOptionPane.showMessageDialog(null,"Hello World");

    Thesecondargumentisthemessagethatistobe displayed

  • 8/10/2019 JOptionPane Class

    6/19

    InputDialogs

    Aninputdialogisaquickandsimplewaytoasktheusertoenterdata.

    Thedialogdisplaysatextfield, an

    OkbuttonandaCancelbutton. IfOkispressed, thedialogreturns

    theusersinput.

    IfCancelispressed, thedialogreturnsnull.

  • 8/10/2019 JOptionPane Class

    7/19

    InputDialogs

    String name;

    name = JOptionPane.showInputDialog(

    "Enter your name.");

    Theargumentpassedtothemethodisthemessage

    to

    display

    . IftheuserclicksontheOKbutton, namereferencesthestringenteredbytheuser.

    IftheuserclicksontheCancelbutton, namereferencesnull.

  • 8/10/2019 JOptionPane Class

    8/19

    NamesDialog.java

    importjavax.swing.JOptionPane;publicclassNamesDialog

    { publicstaticvoidmain(String[]args) { StringfirstName; // Theuser'sfirstname StringmiddleName; // Theuser'smiddlename StringlastName; // Theuser'slastname // Gettheuser'sfirstname firstName=

    JOptionPane.showInputDialog("Whatis" + "yourfirstname? ");

  • 8/10/2019 JOptionPane Class

    9/19

    NamesDialog.java

    // Gettheuser'smiddlename.

    middleName=

    JOptionPane.showInputDialog(

    "Whatis" + "yourmiddlename?

    ");

    // Gettheuser'slastname.

    lastName=

    JOptionPane.showInputDialog("Whatis"+ "yourlastname? ");

  • 8/10/2019 JOptionPane Class

    10/19

    Example

    // Displayagreeting

    JOptionPane.showMessageDialog(n

    ull, "Hello" + firstName+ " "+middleName+ " " + lastName);

    System.exit(0);

    }

    }

  • 8/10/2019 JOptionPane Class

    11/19

    TheSystem.exit() Method

    AprogramthatusesJOptionPanedoesnotautomaticallystopexecutingwhentheendofthemain

    methodisreached. Javageneratesathread, whichisa

    processrunninginthecomputer,whenaJOptionPaneiscreated.

    IftheSystem.exitmethodisnotcalled, thisthreadcontinuestoexecute.

  • 8/10/2019 JOptionPane Class

    12/19

    TheSystem.exit() Method

    TheSystem.exitmethodrequiresanintegerargument.System.exit(0);

    Thisargumentisanexit

    code

    thatis

    passedbacktotheoperatingsystem. Thiscodeisusuallyignored, however, it

    canbeusedoutsidetheprogram: toindicatewhethertheprogramended

    successfullyorastheresultofafailure.

    Thevalue0 traditionallyindicatesthattheprogramendedsuccessfully.

  • 8/10/2019 JOptionPane Class

    13/19

    ConvertingaStringtoa

    Number

    TheJOptionPanesshowInputDialogmethodalwaysreturnstheuser'sinputasaString

    Stringcontaininganumber, suchas127.89, canbeconvertedtoanumericdatatype.

  • 8/10/2019 JOptionPane Class

    14/19

    TheParseMethods

    Parsemethodsconvertstringstonumericdatatypes

    Theyare: Byte.parseByte Integer.parseInt Short.parseShort Long.parseLong Float

    .parseFloat

    Double.parseDouble

  • 8/10/2019 JOptionPane Class

    15/19

    TheParseMethods/

    Examples bytebVar= Byte.parseByte("1"); intiVar= Integer.parseInt("2599"); shortsVar= Short.parseShort("10"); longlVar= Long.parseLong("15908");

    floatfVar= Float.parseFloat("12.3"); doubledVar=

    Double.parseDouble("7945.6");

  • 8/10/2019 JOptionPane Class

    16/19

    PayrollDialog.java

    importjavax.swing.JOptionPane;

    publicclassPayrollDialog

    {

    publicstaticvoidmain(String[]args) {

    StringinputString; // Forreadinginput

    Stringname; // Theuser'sname

    inthours; // Thenumberofhoursworked

    doublepayRate; // Theuser'shourlypayrate

    doublegrossPay; // Theuser'sgrosspay

  • 8/10/2019 JOptionPane Class

    17/19

    PayrollDialog.java

    // Gettheuser'sname. name=

    JOptionPane.showInputDialog("Whatis"+ "yourname?");

    // Getthehoursworked. inputString= JOptionPane.showInputDialog(

    "Howmanyhours +

    didyouworkthisweek? "); // Converttheinputtoanint. hours= Integer.parseInt(inputString);

  • 8/10/2019 JOptionPane Class

    18/19

    PayrollDialog.java

    // Getthehourlypayrate.

    inputString=

    JOptionPane.showInputDialog("What

    is+ " yourhourlypayrate? "); // Converttheinputtoadouble.

    payRate=Double.parseDouble(inputString);

    // Calculatethegrosspay. grossPay= hours* payRate;

  • 8/10/2019 JOptionPane Class

    19/19

    PayrollDialog.java

    // Displaytheresults.

    JOptionPane.showMessageDialog(null,"Hello" + name+ ". Yourgrosspayis% "+ grossPay);

    // Endtheprogram.

    System.exit(0);

    }}