gui graphical user interface each onscreen component and window is an object object interaction...
TRANSCRIPT
![Page 1: GUI Graphical User Interface Each onscreen component and window is an object Object interaction makes communication and scoping challenging Event-driven](https://reader036.vdocuments.net/reader036/viewer/2022082411/5697bf8a1a28abf838c8a863/html5/thumbnails/1.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022082411/5697bf8a1a28abf838c8a863/html5/thumbnails/2.jpg)
• 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](https://reader036.vdocuments.net/reader036/viewer/2022082411/5697bf8a1a28abf838c8a863/html5/thumbnails/3.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022082411/5697bf8a1a28abf838c8a863/html5/thumbnails/4.jpg)
• 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](https://reader036.vdocuments.net/reader036/viewer/2022082411/5697bf8a1a28abf838c8a863/html5/thumbnails/5.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022082411/5697bf8a1a28abf838c8a863/html5/thumbnails/6.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022082411/5697bf8a1a28abf838c8a863/html5/thumbnails/7.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022082411/5697bf8a1a28abf838c8a863/html5/thumbnails/8.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022082411/5697bf8a1a28abf838c8a863/html5/thumbnails/9.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022082411/5697bf8a1a28abf838c8a863/html5/thumbnails/10.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022082411/5697bf8a1a28abf838c8a863/html5/thumbnails/11.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022082411/5697bf8a1a28abf838c8a863/html5/thumbnails/12.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022082411/5697bf8a1a28abf838c8a863/html5/thumbnails/13.jpg)
str = JOptionPane.showInputDialog(stringExpr);
radiusStr = JOptionPane.showInputDialog(“Enter the radius:”)