Improving Java User Interface Basics

Improving Java User Interface Basics
paly

This chapter covers how to construct menu-driven and query-driven terminal interfaces, as well as graphical user interfaces. It also includes formatting text, handling number formats, and using the application controller pattern.

  • Uploaded on | 0 Views
  • andres andres

About Improving Java User Interface Basics

PowerPoint presentation about 'Improving Java User Interface Basics'. This presentation describes the topic on This chapter covers how to construct menu-driven and query-driven terminal interfaces, as well as graphical user interfaces. It also includes formatting text, handling number formats, and using the application controller pattern.. The key topics included in this slideshow are Java, user interface, terminal interfaces, graphical user interfaces, formatting text, application controller pattern, menu-driven program, query-driven input,. Download this presentation absolutely free.

Presentation Transcript


1. 1 Chapter 8 Improving the User Interface Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne

2. Chapter 8 Lambert / Osborne Fundamentals of Java 4E 2 2 2 Objectives Construct a query-driven terminal interface. Construct a menu-driven terminal interface. Construct a graphical user interface. Format text, including numbers, for output. Handle number format exceptions during input.

3. Chapter 8 Lambert / Osborne Fundamentals of Java 4E 3 3 3 Vocabulary application controller pattern data model event-driven format flag format specifier menu-driven program model view query-controlled input

4. Chapter 8 Lambert / Osborne Fundamentals of Java 4E 4 4 A Thermometer Class Used to convert temperatures between Fahrenheit and Celsius. Class stores temperature internally in Celsius, but it can be set and retrieved in either Fahrenheit or Celsius. 4

5. Chapter 8 Lambert / Osborne Fundamentals of Java 4E 5 5 Repeating Sets of Inputs Techniques for handling repeating set of inputs: Count-controlled and sentinel-controlled already learned. Query-controlled input: Before each set of inputs after the first, the program asks the user if there are more inputs. 5

6. Chapter 8 Lambert / Osborne Fundamentals of Java 4E 6 6 Repeating Sets of Inputs (continued) Interface for a query-controlled temperature conversion program 6

7. Chapter 8 Lambert / Osborne Fundamentals of Java 4E 7 7 Repeating Sets of Inputs (continued) 7 The program is implemented by means of two classes: One to handle user interface. The Thermometer class. The code for the interface class uses String variable doItAgain . Controls how many times the loop repeats.

8. Chapter 8 Lambert / Osborne Fundamentals of Java 4E 8 8 A Menu-Driven Conversion Program Menu-driven programs begin by displaying a list of options. The user selects an option. Then the program prompts for additional inputs related to the option, and performs the needed computations. The menu displays again. Code uses if-else statements to evaluate next step after each input. 8

9. Chapter 8 Lambert / Osborne Fundamentals of Java 4E 9 9 A Menu-Driven Conversion Program (continued) 9 Interface for a menu-driven version of the temperature conversion program

10. Chapter 8 Lambert / Osborne Fundamentals of Java 4E 10 10 Formatted Output with printf and format Using printf to Format Numbers: The precision of floating-point numbers refers to the number of digits to the right of the decimal program supported by the programming language. The print and println methods display only the necessary digits for the number. The printf method is used to format output. 10

11. Chapter 8 Lambert / Osborne Fundamentals of Java 4E Formatted Output with printf and format (continued) Using printf to Format Numbers (cont)? The parameters of the method printf consist of a format string and one or more data values. The format string is a combination of literal string information and formatting information. The formatting information consists of one or more format specifiers: Begin with a % character, and end with a letter that indicates the format type. 11

12. Chapter 8 Lambert / Osborne Fundamentals of Java 4E Formatted Output with printf and format (continued) Using printf to Format Numbers (cont): Commonly used format types 12

13. Chapter 8 Lambert / Osborne Fundamentals of Java 4E 13 13 Formatted Output with printf and format (continued) Using printf to Format Numbers (cont): The symbol %n can be used to embed an end- of-line character in a format string. The symbol %% produces the % character. Otherwise, when the compiler encounters a format specifier in a format string, it attempts to match it to an expression following the string. The two must match in type and position. 13

14. Chapter 8 Lambert / Osborne Fundamentals of Java 4E 14 14 Formatted Output with printf and format (continued) Text Justification and Multiple Columns: Data-processing applications frequently display tables with columns of words and numbers. Unless carefully formatted, these tables are unreadable. Each column has a designated width, and the values are justified in the same manner (left, right or center). 14

15. Chapter 8 Lambert / Osborne Fundamentals of Java 4E 15 15 Formatted Output with printf and format (continued) Text Justification and Multiple Columns (cont): A table of sales figures shown with and without formatting 15

16. Chapter 8 Lambert / Osborne Fundamentals of Java 4E 16 16 Formatted Output with printf and format (continued) Text Justification and Multiple Columns (cont): The columns in Version 2 are produced by displaying pieces of text that are justified within fields. Field : a fixed number of columns within which the characters of a data value can be placed. A data value is left-justified when its display begins in the leftmost column of its field. Trailing or leading spaces are used to occupy columns that are not filled by the value. 16

17. Chapter 8 Lambert / Osborne Fundamentals of Java 4E 17 17 Formatted Output with printf and format (continued) Text Justification and Multiple Columns (cont): Format flags support the justification of text as well as other format styles. 17 Some commonly used format flags

18. Chapter 8 Lambert / Osborne Fundamentals of Java 4E 18 18 Formatted Output with printf and format (continued) 18 Text Justification and Multiple Columns (cont): To output data in formatted columns, establish the width of each field and then choose the appropriate format flags and specifiers to use with printf . Some example format strings and their outputs

19. Chapter 8 Lambert / Osborne Fundamentals of Java 4E 19 19 Formatted Output with printf and format (continued) 19 Formatting with String.format : The String method can be used to build a formatted string. The method expects the same parameters as printf and returns a formatted string.

20. Chapter 8 Lambert / Osborne Fundamentals of Java 4E 20 20 Handling Number Format Exceptions During Input If input data are invalid, the program can display an error message and prompt for the data again. Typical errors are input numbers that are without a certain range. Input methods must be able to detect if data is entered in an invalid format. The Scanner and nextDouble methods do. 20

21. Chapter 8 Lambert / Osborne Fundamentals of Java 4E 21 21 Handling Number Format Exceptions During Input (continued) 21 When format errors are detected, these methods throw an exception that halts the program. The bad format is detected before the client code can react to the error. Acceptable during testing and debugging, but the final product needs to respond to formatting errors without halting the program.

22. Chapter 8 Lambert / Osborne Fundamentals of Java 4E 22 22 Handling Number Format Exceptions During Input (continued) 22 The programmer embeds the call to an input method in a try-catch statement: The statements within the try clause are executed until one throws an exception. Then, the catch clause is executed. If no exception, the catch clause is skipped.

23. Chapter 8 Lambert / Osborne Fundamentals of Java 4E 23 23 Graphics and GUIs 23 A GUI can present the user with entry fields for many data values simultaneously: Command buttons, drop-down menus The Model/View/Controller Pattern: Data model : class type whose responsibilities include initializing and managing the data. View : class type such as windows, buttons, data files, and labels that display controls for user interaction.

24. Chapter 8 Lambert / Osborne Fundamentals of Java 4E 24 24 Graphics and GUIs (continued) The Model/View/Controller Pattern (cont): Controller : class type that are listeners. Responsible for handling user interaction. Application : class type that sets up the other elements in a main method to provide an entry point for running a Java program. 24 Interface for the GUI-based temperature conversion program

25. Chapter 8 Lambert / Osborne Fundamentals of Java 4E 25 25 Graphics and GUIs (continued) 25 Use grids, panels, and padding when designing the layout of labels and data fields. Real GUI programs are event-driven. When the program opens, it waits for events such as mouse click or typing characters in a field. The JVM runs in a loop behind the scenes. To make the program robust, allow the JVM to track the main window of the dialog box.

26. Chapter 8 Lambert / Osborne Fundamentals of Java 4E 26 26 Summary In this chapter, you learned: A terminal input/output (I/O) interface can be extended to handle repeated sets of inputs, by using either a query-based pattern or a menu- driven pattern. A graphical user interface (GUI) allows the user to interact with a program by displaying window objects and handling mouse events. 26

27. Chapter 8 Lambert / Osborne Fundamentals of Java 4E 27 27 Summary (continued) 27 In a terminal-based program, the program controls most of the interaction with the user, whereas GUI-based programs are driven by user events. The two primary tasks of a GUI-based program are to arrange the window objects in a window and handle interactions with the user.

Related