130 likes | 265 Views
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. Option pane – simple message box
E N D
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
Option pane – simple message box • Includes a message or request for input • JOptionPane
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
Integer, Float, Double • Classes • Convert string to a number • Wrapper classes • parseInt is a method of class Integer • parseFloat…
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.*;
Gui1 import javax.swing.*; // for GUI components public class Gui1 { public static void main(String [] args) { JOptionPane.showMessageDialog(null, "Hello world!"); } }
JOptionPane • Display a message (showMessageDialog) • To present a list of choices to the user (showInputDialog) • To ask the user to type input (showConfirmDialog)
GUI2 import javax.swing.*; // for GUI components public 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?"); } }
showMessageDialog • JOptionPane.showMessageDialog(parentComponent, messageStrExp, boxTitleStr, messageType); parentComponent – null causes dialog box to appear in middle of string messageStrExp – appears in dialog box boxTitleString – title of dialog box messageType – ERROR_MESSAGE – error icon INFORMATION_MESSAGE – information icon PLAIN_MESSAGE – no icon QUESTION_MESSAGE – question icon WARNING_MESSAGE – warning icon
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); } }
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);
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); } }
str = JOptionPane.showInputDialog(stringExpr); radiusStr = JOptionPane.showInputDialog(“Enter the radius:”)