210 likes | 240 Views
Explore text areas, sliders, menus, frames, pop-up menus, look-and-feel options in UI design. Learn to customize JPanel, JSliders, JPopupMenu, and use Pluggable Look-and-Feel for Java Swing GUI. Understand Layout Managers, including BoxLayout and GridBagLayout, for precise component arrangement.
E N D
Graphical User Interface Components The Sequal Chapter 14
What You Will Learn • Text Areas • Sliders • Menus • With frames • Pop up menus • Look and feel options
JTextArea • Area for manipulating multiple lines of text • Extends JTextComponent • Inherits from that class • View Figure 14.1 • Note • Creation of box • Creation of two JTextArea objects • Copy of marked text to other text area
Subclass of JPanel • A JPanel can • Aggregate a set of GUI components • Create dedicated drawing areas • Create areas that receive mouse events • Subclass of a JPanel should • Override method paintComponent • Call the superclass version as first statement • Note the customized JPanel subclass, Figure 14.2
Demonstration of Customized JPanel • Now view Figure 14.3 • Note • Declaration, instantiation of the CustomPanel • Method draw used in actionPerformed methods
Another Customized JPanel Class • Possible for the (sub)class to handle its own events • Note SelfContainedPanel class, Figure 14.4 • This class has • Mouse activity handlers • Preferred dimension specified • Call to paintComponent method • Figure 14.5 uses this customized subclass
tick mark JSliders • Enable users to select from range of integer values • Features • Tick marks (major and minor) • Snap-to ticks • Orientation (horizontal and vertical) thumb
JSliders • JPanel subclass for drawing circles, Figure 14.7 • JSlider used to control size of a circle graphic, Figure 14.8 • Note • Slider declaration, implementation • Registration of listener for the slider
Using Menus with Frames • Allows for performing actions with cluttering GUI • Contained by menu bar – JMenuBar • Comprised of menu items – MenuItem • Note Figure 14.9 • Declares menu items • Populates the menus • Specifies actions for menu choices
JPopupMenu • Menus that are context-sensitive • Right click and popup menu appears • In Swing, use JPopupMenu • Provide options specific to component • Popup trigger event occurs when object right mouse clicked • Demonstration of JPopupMenu shown in Figure 14.10
Pluggable Look-and-Feel • Recall that Java is designed to run on variety of platforms • GUI components on a Mac, look different from those on a PC or on a UNIX platform • Look-and-Feel capability • Java program on a particular platform looks like other programs on the same platform • Also possible to specify the look-and-feel of a Swing GUI
Pluggable Look-and-Feel • Figure 14.11 demonstrates how to change the appearance • Note use of UIManager methods
JDesktopPane and JInternalFrame • Multiple document interface • Main (parent) window • Child windows • View Figure 14.12 which demonstrates this • Note • Declaration, instantiation of JDesktopPane object • Choosing and painting of the icons
JTabbedPane • Arranges GUI components into layers • One layer visible at a time • Access each layer via a tab • View tab demo program, Figure 14.13 • Note • declaration of tabbed panes
Layout Managers • BoxLayout arranges GUI components • Horizontally along x-axis • Vertically along y-axis
BoxLayout • Demonstrated in Figure 14.15 • Also uses the tabs • Offers several different options for laying out three JButtons • Note • Creation of boxes • Adding of buttons
GridBagLayout Layout Manager • Flexible GridBagLayout • Components can vary in size • Components can occupy multiple rows and columns • Components can be added in any order • Uses GridBagConstraints • Specifies how component is placed in GridBagLayout
GridBagLayout Layout Manager • GridBagConstraints fields
GridBagLayout Layout Manager • Designing a GUI to use the GridBagLayout • Program for this layout demonstrated in Figure 14.19 Column 0 1 2 0 1 Row 2 3
GridBagLayout Layout Manager • Variation of GridBagLayoutdoes not use gridx and gridy • Use constants RELATIVE and REMAINDER • RELATIVE • Specifies next-to-last component placement in row or column • Component placed next to one previously added • REMAINDER • Specifies component as last component in row or column
GridBagLayout Layout Manager • Use of constraints demonstrated inFigure 14.20 • Note • Setting constraint weights • Setting grid width • Adding buttons • The addComponent method