gui graphical user interface each onscreen component and window is an object object interaction...

13
GUI • Graphical User Interface • Each onscreen component and window is an object • Object interaction makes communication and scoping challenging • Event-driven - program’s execution is driven by the series of events that occur

Upload: marshall-burns

Post on 17-Jan-2016

221 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: GUI Graphical User Interface Each onscreen component and window is an object Object interaction makes communication and scoping challenging Event-driven

GUI

• Graphical User Interface• Each onscreen component and window is an

object• Object interaction makes communication and

scoping challenging• Event-driven - program’s execution is driven

by the series of events that occur

Page 2: GUI Graphical User Interface Each onscreen component and window is an object Object interaction makes communication and scoping challenging Event-driven

• Option pane – simple message box• Includes a message or request for input• JOptionPane

Page 3: GUI Graphical User Interface Each onscreen component and window is an object Object interaction makes communication and scoping challenging Event-driven

Parsing Number Strings

• Integer.parseInt(strExpression)• Float.parseFloat(strExpression)• Double.parseDouble(strExpression)• Examples:• Integer.parseInt(“-542”) = -542• Float.parseFloat(“34.63”) = 34.63• Double.parseDouble(“-758.873”) = -758.873

Page 4: GUI Graphical User Interface Each onscreen component and window is an object Object interaction makes communication and scoping challenging Event-driven

• Integer, Float, Double• Classes• Convert string to a number• Wrapper classes• parseInt is a method of class Integer• parseFloat…

Page 5: GUI Graphical User Interface Each onscreen component and window is an object Object interaction makes communication and scoping challenging Event-driven

Using Dialog boxes

• GUI• Class JOptionPane• Contained in package javax.swing• In earlier versions of java, Swing was an extension to Java’s

feature set• Two methods:

– To present a list of choices to the user • showInputDialog

– Display a message • showMessageDialog (parent window, display string)

• import javax.swing.*;

Page 6: GUI Graphical User Interface Each onscreen component and window is an object Object interaction makes communication and scoping challenging Event-driven

Gui1

import javax.swing.*; // for GUI componentspublic class Gui1{ public static void main(String [] args) { JOptionPane.showMessageDialog(null, "Hello world!"); }}

Page 7: GUI Graphical User Interface Each onscreen component and window is an object Object interaction makes communication and scoping challenging Event-driven

JOptionPane

1. Display a message (showMessageDialog)2. To present a list of choices to the user

(showInputDialog)3. To ask the user to type input

(showConfirmDialog)

Page 8: GUI Graphical User Interface Each onscreen component and window is an object Object interaction makes communication and scoping challenging Event-driven

GUI2import javax.swing.*; // for GUI componentspublic class Gui2{ public static void main(String [] args) { String name = JOptionPane.showInputDialog(null, "What is your name"); int choice = JOptionPane.showConfirmDialog(null, "Do you like chocolate, " + name + "?"); if (choice == JOptionPane.YES_OPTION) { JOptionPane.showMessageDialog(null, "Of course! Who doesn't?"); } else JOptionPane.showMessageDialog(null, "Seriously?"); }}

Page 9: GUI Graphical User Interface Each onscreen component and window is an object Object interaction makes communication and scoping challenging Event-driven

showMessageDialog• JOptionPane.showMessageDialog(parentComponent, messageStrExp,

boxTitleStr, messageType); parentComponent – null causes dialog box to appear in middle of stringmessageStrExp – appears in dialog boxboxTitleString – title of dialog boxmessageType –

ERROR_MESSAGE – error iconINFORMATION_MESSAGE – information icon PLAIN_MESSAGE – no iconQUESTION_MESSAGE – question iconWARNING_MESSAGE – warning icon

Page 10: GUI Graphical User Interface Each onscreen component and window is an object Object interaction makes communication and scoping challenging Event-driven

import javax.swing.*;

public class AreaAndCircumferenceProgram { public static final double PI = 3.14; public static void main(String[] args) { double radius, area, circumference; String radiusString,outputStr;

radiusString = JOptionPane.showInputDialog(“Enter radius:"); radius = Double.parseDouble(radiusString); area = PI * radius * radius; circumference = 2 * PI * radius; outputStr = "Radius: " + radius + "\nArea: " + area + " square units\n" + "Circumference: " + circumference + " units"; JOptionPane.showMessageDialog(null, outputStr, "Circle", JOptionPane.INFORMATION_MESSAGE); System.exit(0); }}

Page 11: GUI Graphical User Interface Each onscreen component and window is an object Object interaction makes communication and scoping challenging Event-driven

String method format

• String.format(formatString,argumentList)• Works like printf• Can be used as argument to println• Double x = 15.674;• Double y = 235.73;• Double z = 9525.9864;• System.out.println(String.format(“%.2f”,x);• System.out.println(String.format(“%.3f”,y);• System.out.println(String.format(%.2f”,z);

Page 12: GUI Graphical User Interface Each onscreen component and window is an object Object interaction makes communication and scoping challenging Event-driven

import javax.swing.*;

public class AreaAndCircumferenceProgram { public static final double PI = 3.14; public static void main(String[] args) { double radius, area, circumference; String radiusString,outputStr;

radiusString = JOptionPane.showInputDialog(“Enter radius"); radius = Double.parseDouble(radiusString); area = PI * radius * radius; circumference = 2 * PI * radius; outputStr = String.format("Radius: %.2f %n Area: %.2f square units %nCircumference: %.2f units", radius, area, circumference); JOptionPane.showMessageDialog(null, outputStr, "Circle", JOptionPane.INFORMATION_MESSAGE); }}

Page 13: GUI Graphical User Interface Each onscreen component and window is an object Object interaction makes communication and scoping challenging Event-driven

str = JOptionPane.showInputDialog(stringExpr);

radiusStr = JOptionPane.showInputDialog(“Enter the radius:”)