670 likes | 691 Vues
Swing Set. By Theparit Peerasathien. Features of the Java Foundation Classes. New GUI called swing set Graphic API called 2D graphics Accessibility API Drag and Drop API. Heavyweights & Lightweights component. Heavyweight components (peer)
E N D
Swing Set By Theparit Peerasathien
Features of the Java Foundation Classes • New GUI called swing set • Graphic API called 2D graphics • Accessibility API • Drag and Drop API
Heavyweights & Lightweights component • Heavyweight components (peer) • Associated with its own native screen resource • Connect with JVM & API • Slow • Example • Top-level swing: JFrame, JDialog, JApplet
Heavyweights & Lightweights component • Lightweight components • borrows" the screen resource of an ancestor • no native resource of its own • Example • All Swing set except Top-level swing
How to use Components • Top-level component is a root of every containment hierarchy • All Swing programs have at least one Top-level component • Add Lightweight component to content Panes
Swing Set • Top-Level Containrs • General-Purpose Containers • Special-Purpose Containers • Basic Controls • Uneditable Information Displays • Interactive Displays of Highly Formatted Information
Top-level Components • Types of top-level containers • JFrames • JDialogs • JApplets • JWindow
Top-Level Component • Add() components to Content pane • setLayout() content pane • Calling by • public Container getContentPane(); • Feature • support for adding a menu bar • need to use a content pane .
JFrame • Window with border, title and buttons • Making frames • JFrame frame = new JFrame(); Or a extend JFrame class (often better code this way). • Style defined with UIManager.setLookAndFeel(looknfeel); SwingUtilities.updateComponentTreeUI(frame); frame.pack();
Responding to Window-Closing Events • By default, when the user closes a frame onscreen, the frame is only hidden. • register a window listener that handles window-closing events • Use setDefaultCloseOperation method • DO_NOTHING_ON_CLOSE • HIDE_ON_CLOSE • DISPOSE_ON_CLOSE • EXIT_ON_CLOSE
JApplet • a subclass of java.applet.Applet • Features • top-level Swing container • Swing applet has a root pane- support for adding a menu bar • has a single content pane.
JApplet • add components to a Swing applet's content pane, not directly to the applet • set the layout manager on a Swing applet's content pane, not directly on the applet • default layout manager for a Swing applet's content pane is BorderLayout • should not put painting code directly in a JApplet object
JApplet • import javax.swing.JApplet;import java.awt.*; • public class JAppletTest extends JApplet { • public void int() { • Container cp = getCntentPane(); • cp.setLayout (new FlowLayput); • cp.add(new JLabel(“Hello”); • }}
windows that are more limited than frames To create simple, standard dialogs, you use the JOptionPane class. The ProgressMonitor class can put up a dialog that shows the progress of an operation JColorChooser and JFileChooser supply standard dialogs for Color chooser and File Chooser JDialog
can create and customize several different kinds of dialogs provides support for laying out standard dialogs, providing icons, specifying the dialog's title and text, and customizing the button text JOptionPane
Sample JDialog • //default title and iconJOptionPane.showMessageDialog(frame, "Eggs aren't supposed to be green."); • //custom title, warning iconJOptionPane.showMessageDialog(frame, "Eggs aren't supposed to be green.", "Inane warning", JOptionPane.WARNING_MESSAGE); • //custom title, custom iconJOptionPane.showMessageDialog(frame, "Eggs aren't supposed to be green.", "Inane custom dialog", JOptionPane.INFORMATION_MESSAGE, icon);
Swing Set • Top-Level Containrs • General-Purpose Containers • Special-Purpose Containers • Basic Controls • Uneditable Information Displays • Interactive Displays of Highly Formatted Information
JComponents • Except top-level container, All Swing components names begin with “J” descend from the JComponent class • Example JPanel, JButton, JTable • Except JFrame JDialog (Top-level container)
JComponent Features • Tool tips - When the cursor pauses over the component, the specified string is displayed in a small window • Painting and Border • allows you to specify the border that a component displays around its edges • Look and feel • has a corresponding ComponentUI • Custom properties - associate one or more properties (name/object pairs) with any JComponent
JComponent Features • Support for Layout • setter methods — setPreferredSize, setMinimumSize, setMaximumSize, setAlignmentX, and setAlignmentY • Support drag and Drop • provides API to set a component's transfer handler, which is the basis for Swing's drag and drop support • Double buffering • Double buffering smooths on-screen painting • Key Bindings • components react when the user presses a key on the keyboard Example when a button has the focus, typing the Space key is equivalent to a mouse click on the the button
General-Purpose Containers • Example • Panel • Scroll pane • Split pane • Tabbed pane • Tool bar
Panel • JPanel class provides general-purpose containers for lightweight components • By default, panels don't paint anything except for their background • customize their painting • In many look and feels (but not GTK+), panels are opaque by default • You can change a panel's transparency by invoking setOpaque
Setting the Layout Manager • By default, a panel's layout manager is an instance of FlowLayout • Example • JPanel p = new JPanel(new BorderLayout()); • JPanel p = new JPanel(); p.setLayout(new BoxLayout(p, BoxLayout.PAGE_AXIS));
Adding Components • FlowLayout • aFlowPanel.add(aComponent); aFlowPanel.add(anotherComponent); • BorderLayout • aBorderPanel.add(aComponent, BorderLayout.CENTER); aBorderPanel.add(anotherComponent, BorderLayout.PAGE_END);
Swing Set • Top-Level Containrs • General-Purpose Containers • Special-Purpose Containers • Basic Controls • Uneditable Information Displays • Interactive Displays of Highly Formatted Information
Special-Purpose Containers • Intermediate containers that play specific roles in the UI. • Example • Internal Frame • Layered Pane • Root Pane
Internal Frames • JInternalFrame class you can display a JFrame-like window within another window • add internal frames to a desktop pane • The desktop pane is an instance of JDesktopPane
Example • ...//In the constructor of InternalFrameDemo, a JFrame subclass: • desktop = new JDesktopPane(); createFrame(); • //Create first window setContentPane(desktop); • //Make dragging a little faster but perhaps uglier. desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE); • protected void createFrame() { MyInternalFrame frame = new MyInternalFrame(); frame.setVisible(true); desktop.add(frame); try { frame.setSelected(true); } catch (java.beans.PropertyVetoException e) {}
Internal Frames vs. Regular Frames • internal frames is similar in many ways to the code for using regular Swing frames • internal frames have root panes • also provides other API, such as pack • Internal frames aren't windows or top-level containers • You can programatically iconify or maximize an internal frame. You can also specify what icon goes in the internal frame's title bar. You can even specify whether the internal frame has the window decorations to support resizing, iconifying, closing, and maximizing.
Layered Panes • Swing container that provides a third dimension for positioning components • specify its depth as an integer. The higher the number, the higher the depth. • If components overlap, components at a higher depth are drawn on top of components at a lower depth
Position Depth • Positions are specified with an int between -1 and (n - 1), where n is number of component • the smaller the position number, the higher the component within its depth exception (-1) is deepest too
Root Panes • don't directly create a JRootPane object • instantiate JInternalFrame or one of the top-level Swing containers • Use Top-Level Container • getting the content pane • setting its layout manager • adding Swing components
four parts of root pane • The glass pane • completely transparent unless you implement the glass pane's paintComponent method so that it does something, and it intercepts input events for the root pane. • The layered pane • Serves to position its contents, which consist of the content pane and the optional menu bar. Can also hold other components in a specified Z order.. • The content pane • The container of the root pane's visible components, excluding the menu bar.. • The optional menu bar • The home for the root pane's container's menus. If the container has a menu bar, you generally use the container's setJMenuBar method to put the menu bar in the appropriate place.
The Glass Pane • is useful when you want to be able to catch events or paint over an area that already contains one or more components • It contains a check box that lets you set whether the glass pane is "visible" — whether it can get events and paint itself onscreen. • When the glass pane is visible, it blocks all input events from reaching the components
Swing Set • Top-Level Containrs • General-Purpose Containers • Special-Purpose Containers • Basic Controls • Uneditable Information Displays • Interactive Displays of Highly Formatted Information
Basic Control Component • Atomic components that exist primarily to get input from the user • show simple state