Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "GUI Graphical User Interface Each onscreen component and window is an object Object interaction makes communication and scoping challenging Event-driven."— Presentation transcript:

1 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

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

3 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

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

5 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.*;

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

7 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)

8 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?"); }

9 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

10 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); }

11 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);

12 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); }

13 str = JOptionPane.showInputDialog(stringExpr); radiusStr = JOptionPane.showInputDialog(“Enter the radius:”)


Download ppt "GUI Graphical User Interface Each onscreen component and window is an object Object interaction makes communication and scoping challenging Event-driven."

Similar presentations


Ads by Google