IIT CS 201 - Chapter 12 Graphical User Interfaces

Unformatted text preview:

1Chapter 12Graphical User InterfacesTopics• GUI Applications with JFrame• GUI Components• JLabel• Event Handling• Layout Managers• GUI Components– Text Fields– Command Buttons, Radio Buttons, and Checkboxes– Lists and Combo Boxes• Adapter Classes• Mouse MovementsIntroduction• GUIs enable the user to– select the next function to be performed – enter data– set program preferences, such as colors or fonts. • GUIs also make the program easier to use – a GUI is a familiar interface to users. – Users can learn quickly to operate your program, in many cases, without consulting documentation or requiring extensive training. GUI• Presents a pictorial interface to a program• It gives a program a distinct look and feel• GUIs are built from a widgets – Window gadgets which are window components – Is an object with which the user interacts via the mouse, keyboard, voice recognition, etc• Components Examples:– JLabel– JTextField– JButton– JCheckList– JComboBox,– JList2The JFrame Class• The JFrame class, in thejavax.swing package, allows youto display a window.• JFrame is a– Component, a graphical objectthat can be displayed– Container, a component that holds other objects– Window, a basic window– Frame, a framed windowJFrame• Is a window with a title bar and a border• By default the window is hidden• We specify action when the window is closedsetDefaultCloseOperation– DISPOSE_ON_CLOSE– DO_NOTHING_ON_CLOSE– HIDE_ON_CLOSE– EXIT_ON_CLOSEJFrame• Window that knows how to resize itself• Moves around on the screen when dragged• Can minimize, maximizeContent PaneJFrame ConstructorsJFrame( String titleBarText ) constructs a JFrame object, initially invisible, with titleBarText displayed in the window's title bar JFrame( )constructs a JFrame object, initially invisible, with no text in the title barConstructor3Useful Methods of the JFrame Class• Applications extending JFrame inherit these methods. setVisible( boolean mode )displays this window if mode is true; hides the window if mode is falsevoidsetSize( int width, int height )sizes the window to the specified width and height in pixelsvoidsetDefaultCloseOperation( int operation )sets the default operation when the user closes this window, that is, when the user clicks on the X icon in the top-right corner of the windowvoid getContentPane( )returns the content pane object for this windowContainerMethod name and argument listReturn valueA Shell GUI Application • See Example 12.1 ShellGUIApplication.java• Our application inherits from JFramepublic class ShellGUIApplication extends JFrame• The main method – instantiates an instance of our application:ShellGUIApplication basicGui = new ShellGUIApplication( );– Specifies that the application should terminate when the user closes the window:basicGui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );A Shell GUI Application (con't)• The GUI application's constructor's job is to:– call the constructor of the JFrame superclass– get an object reference to the content pane container. We will add our GUI components to the content pane.– set the layout manager. Layout managers arrange the GUI components in the window.– instantiate each component– add each component to the content pane– set the size of the window– display the window.Common ErrorTrapBe sure to call the setSize method to set the initial dimensions of the window and call the setVisiblemethod to display the window and its contents. Omitting the call to the setSize method will create a default JFrame consisting of a title bar only. If you omit the call to the setVisible method, the window will not open when the application begins.4GUI Components• A component performs at least one of these functions:– displays information – collects data from the user– allows the user to initiate program functions• The Java class library provides a number of component classes in the javax.swing packageAWT Versus Swing• Java supports two implementations of GUI components: AWT (Abstract Window Toolkit) and swing. • AWT components:– the original implementation of Java components– AWT hands off some of the display and behavior of the component to the native windowing system– called heavyweight components• Disadvantage: – because of the inconsistencies in the look-and-feel of the various windowing systems, an application may behave slightly differently on one platform than on another.AWT Versus Swing• Swing Components– second generation of GUI components– developed entirely in Java to provide a consistent look and feel from platform to platform – referred to as lightweight components• Benefits:– Applications run consistently across platforms, which makes maintenance easier.– An application can take on the look-and-feel of the platform on which it is running, if desired.Java Swing Components, part 1Single-line text field for accepting passwords without displaying the characters typed.Password field /JPasswordFieldMultiple-line text field for data entry or displayText area /JTextAreaA single-line text box for displaying information and accepting user inputText field / JTextFieldDisplays an image or read-only text. Labels are often paired with text fields to identify the contents of the text field.Label /JLabelPurposeComponent/Java Class5Java Swing Components, part 2Drop-down list of items that the user clicks to select one itemDrop-down List /JComboBoxList of items that the user clicks to select one or more items List /JListToggle button that the user clicks to select 0, 1, or more options in a group.Checkbox /JCheckBoxToggle button that the user clicks to select one option in the group. Radio Button / JRadioButtonCommand button that the user clicks to signal that an operation should be performed Button /JButtonPurposeComponent/Java ClassUseful JComponent MethodssetBackground( Color backColor )sets the background color of the component to backColor.voidsetForeground( Color foreColor )sets the foreground color of the component to foreColor.voidsetToolTipText( String toolTip )sets the tool tip text to toolTip. When the mouse lingers over the component, the tool tip text will be displayed.void setVisible( boolean mode )makes the component visible if mode is true; hides the component if mode is false. The default is visible.voidMethod name and argument listReturn valueMore Useful JComponent MethodssetEnabled( boolean mode


View Full Document

IIT CS 201 - Chapter 12 Graphical User Interfaces

Download Chapter 12 Graphical User Interfaces
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Chapter 12 Graphical User Interfaces and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Chapter 12 Graphical User Interfaces 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?