JOptionPane class.

Slides:



Advertisements
Similar presentations
Chapter 2: Java Fundamentals cont’d
Advertisements

Chapter 2 - Introduction to Java Applications
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.
Using JOptionPanes for graphical communication with our programs. Pages Horstmann 139.
Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in.
Introduction to Computers and Programming Lecture 3: Variables and Input Professor: Evan Korth New York University.
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 3: 1 CMT1000: Introduction to Programming Ed Currie Lecture 3: Program structure.
School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 4: 1 CMT1000: Introduction to Programming Ed Currie Lecture 5a: Input and.
Chapter 2 - Introduction to Java Applications
Programming with Java standard classes. Java API Application Programming Interface Provides hundreds of standard classes that can be incorporated into.
Relational Operators Control structures Decisions using “if” statements  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
JOptionPane class. Dialog Boxes A dialog box is a small graphical window that displays a message to the user or requests input. A variety of dialog boxes.
Warm-Up: Monday, March 3 List all devices you can think of that can be used to input information into the computer.
CS-0401 INTERMEDIATE PROGRAMMING USING JAVA Prof. Dr. Paulo Brasko Ferreira Fall 2014.
Intro to GUIs (Graphical User Interfaces) Section 2.5Intro. to GUIs: a GUI Greeter Section 3.7Graphical/Internet Java: Einstein's Equation.
OOP (Java): Simple/ OOP (Java) Objectives – –give some simple examples of Java applications and one applet 2. Simple Java Programs Semester.
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
9/6: Variable Types, Arithmetic Operators, Comparison Operators Addition.java in depth Variable types & data types Input from user: how to get it Arithmetic.
习 题 4.23 编写一个 applet ,读取一个矩形的边长,然后 用在 paint 方法中使用 drawString 方法画出用星组成 的空心矩形。程序应能画出边长从 1 到 20 的任何矩 形。
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A Simple Program: Printing a.
CS 106 Introduction to Computer Science I 01 / 31 / 2007 Instructor: Michael Eckmann.
Programming Fundamentals 2: Simple/ F II Objectives – –give some simple examples of Java applications and one applet 2. Simple Java.
1 COMP 241: Object-Oriented Programming with Java Fall 2004 Lecture 1 September 27, 2004 Serdar Taşıran.
9/20: The while Repetition Structure last time’s program repetition structures: what they are the while repetition structure homework due on Thursday program.
1/28: Inputs, Variable Types, etc. Addition.java in depth Variable types & data types Input from user: how to get it Arithmetic operators.
Dialog Boxes.
Data Types – Reference Types Objective To understand what reference types are The need to study reference types To understand Java standard packages To.
GUI Graphical User Interface Each onscreen component and window is an object Object interaction makes communication and scoping challenging Event-driven.
Java Fundamentals Part Integer Division Division can be tricky. In a Java program, what is the value of 1/2? You might think the answer is.
Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.
Ihsan Ayyub Qazi Introduction to Computer Programming Recitation 3 Ihsan Ayyub Qazi.
Chapter 2: Java Fundamentals Starting Out with Java: From Control Structures through Objects Fifth Edition by Tony Gaddis.
Creating and Using Dialogs ● A dialog is a box that pops up and prompts the user for a value or informs them of something ● One way: directly create objects.
InterestRate Create an InterestRate class and InterestRateViewer client class to do the following: A person is purchasing an item with their credit card.
Casting, Wrapper Classes, Static Methods, JOptionPane Class.
JOptionPane. Import javax.swing.JOptionPane Use showInputDialog() for input. Only string values can be input. To convert an input value from a string.
AP Java 10/1/2015. public class Rolling { public static void main( String [] args) public static void main( String [] args) { int roll; int roll; for.
Methods. Methods are groups of statements placed together under a single name. All Java applications have a class which includes a main method class MyClass.
INTERMEDIATE PROGRAMMING USING JAVA
Java Programming Lecture 2
Data Types – Reference Types
OBJECT ORIENTED PROGRAMMING I LECTURE 7 GEORGE KOUTSOGIANNAKIS
Chapter 2: Java Fundamentals by Tony Gaddis and Godfrey Muganda
AP Java 10/4/2016.
2.5 Another Java Application: Adding Integers
Section 64 – Manipulating Data Using Methods – Java Swing
Dialogues and Wrapper Classes
Chapter 2 - Introduction to Java Applications
Message, Input, Confirm, and Specialized Dialogs
JOptionPane Dialogs javax.swing.JOptionPane is a class for creating dialog boxes. Has both static methods and instance methods for dialogs. Easy to create.
CS-0401 INTERMEDIATE PROGRAMMING USING JAVA
OBJECT ORIENTED PROGRAMMING I LECTURE 7 GEORGE KOUTSOGIANNAKIS
Chapter 2: Java Fundamentals
Chapter 2 - Introduction to Java Applications
Chapter 2: Java Fundamentals
Chapter 3: Introduction to Objects and Input/Output
Chapter 2 - Introduction to Java Applications
AP Java 10/4/2016.
Message, Input, and Confirm Dialogs
Introduction to Java Programming
The System.exit() Method
Object Oriented Programming
CS431 ws99 Half Text Half Graphics
Chapter 2: Java Fundamentals
Dr. Sampath Jayarathna Cal Poly Pomona
Chapter 2: Java Fundamentals
F II 2. Simple Java Programs Objectives
Chapter 2: Java Fundamentals cont’d
Presentation transcript:

JOptionPane class

Dialog Boxes A dialog box is a small graphical window that displays a message to the user or requests input. A variety of dialog boxes can be displayed using the JOptionPane class. Two of the dialog boxes are: Message Dialog - a dialog box that displays a message. Input Dialog - a dialog box that prompts the user for input.

Using the import Statement The JOptionPane class is not automatically available to your Java programs. The following statement must be before the program’s class header: import javax.swing.JOptionPane; This statement tells the compiler where to find the JOptionPane class. 

Dialog Boxes The JOptionPane class provides static methods to display each type of dialog box.

Message Dialogs JOptionPane.showMessageDialog method is used to display a message dialog. JOptionPane.showMessageDialog(null, "Hello World"); The second argument is the message that is to be displayed

Input Dialogs An input dialog is a quick and simple way to ask the user to enter data. The dialog displays a text field, an Ok button and a Cancel button. If Ok is pressed, the dialog returns the user’s input. If Cancel is pressed, the dialog returns null.

Input Dialogs String name; name = JOptionPane.showInputDialog( "Enter your name."); The argument passed to the method is the message to display. If the user clicks on the OK button, name references the string entered by the user. If the user clicks on the Cancel button, name references null.

NamesDialog.java import javax.swing.JOptionPane; public class NamesDialog { public static void main(String[] args) String firstName; // The user's first name String middleName; // The user's middle name String lastName; // The user's last name // Get the user's first name firstName = JOptionPane.showInputDialog("What is " + "your first name? ");

NamesDialog.java // Get the user's middle name. middleName = JOptionPane.showInputDialog( "What is " + "your middle name? "); // Get the user's last name. lastName = JOptionPane.showInputDialog("What is " + "your last name? ");

Example // Display a greeting JOptionPane.showMessageDialog(null, "Hello " + firstName + " " +middleName + " " + lastName); System.exit(0); }

The System.exit() Method A program that uses JOptionPane does not automatically stop executing when the end of the main method is reached. Java generates a thread, which is a process running in the computer, when a JOptionPane is created. If the System.exit method is not called, this thread continues to execute.

The System.exit() Method The System.exit method requires an integer argument. System.exit(0); This argument is an exit code that is passed back to the operating system. This code is usually ignored, however, it can be used outside the program: to indicate whether the program ended successfully or as the result of a failure. The value 0 traditionally indicates that the program ended successfully.

Converting a String to a Number The JOptionPane’s showInputDialog method always returns the user's input as a String String containing a number, such as “127.89, can be converted to a numeric data type.

The Parse Methods Parse methods convert strings to numeric data types They are: Byte.parseByte Integer.parseInt Short.parseShort Long.parseLong Float.parseFloat Double.parseDouble

The Parse Methods- Examples byte bVar = Byte.parseByte("1"); int iVar = Integer.parseInt("2599"); short sVar = Short.parseShort("10"); long lVar = Long.parseLong("15908"); float fVar = Float.parseFloat("12.3"); double dVar = Double.parseDouble("7945.6");

PayrollDialog.java import javax.swing.JOptionPane; public class PayrollDialog { public static void main(String[] args) String inputString; // For reading input String name; // The user's name int hours; // The number of hours worked double payRate; // The user's hourly pay rate double grossPay; // The user's gross pay

PayrollDialog.java // Get the user's name. name = JOptionPane.showInputDialog("What is " + "your name? "); // Get the hours worked. inputString = JOptionPane.showInputDialog( "How many hours” + “ did you work this week? "); // Convert the input to an int. hours = Integer.parseInt(inputString);

PayrollDialog.java // Get the hourly pay rate. inputString = JOptionPane.showInputDialog("What is” + " your hourly pay rate? "); // Convert the input to a double. payRate = Double.parseDouble(inputString); // Calculate the gross pay. grossPay = hours * payRate;

PayrollDialog.java // Display the results. JOptionPane.showMessageDialog(null, "Hello " + name + ". Your gross pay is $" + grossPay); // End the program. System.exit(0); }