Presentation is loading. Please wait.

Presentation is loading. Please wait.

Swing Hierarchy Swing classes derived from JComponent will be lightweight, written entirely in Java. The top-level Swing windows are heavyweight. They.

Similar presentations


Presentation on theme: "Swing Hierarchy Swing classes derived from JComponent will be lightweight, written entirely in Java. The top-level Swing windows are heavyweight. They."— Presentation transcript:

1 Swing Hierarchy Swing classes derived from JComponent will be lightweight, written entirely in Java. The top-level Swing windows are heavyweight. They depend on the native system. javax.swing Component Container Panel Window Frame Dialog Object java.lang JApplet java.awt java.applet Applet JDialog JFrame JWindow JComponent Class extends Key Abstract Class implements Interface Package

2 Swing Programs Four basic types of Top Level Window – JFrame, a top level window decorated like a native window – JWindow, an undecorated stand-alone window (splash- screen) – JApplet, an embeddable applet – JDialog, a popup dialog window Each program type is implemented as a framework class

3 Swing Hierarchy (Part II) Swing components names start with ‘J’. JButton JMenuItem Class extends Key Abstract Class implements Interface Package Object java.lang JComponent java.awt Component Container avax.swing AbstractButton JToggleButton JMenu JLabel JTextComponent JTextField JTextArea JPasswordField JCheckbox JRadioButton JPopupMenu JPanel JMenuBar JScrollPane JList JOptionPane

4 Event Classes ComponentsEventsDescription Button, JButtonActionEventUser clicked button CheckBox, JCheckBoxItemEventUser toggled a checkbox CheckboxMenuItem, JCheckboxMenuItemItemEventUser toggled a checkbox Choice, JPopupMenuItemEventUser selected a choice Component, JComponentComponentEventComponent was moved or resized FocusEventComponent acquired or lost focus KeyEventUser typed a key MouseEventUser manipulated the mouse Container, JContainerContainerEventComponent added/removed from container List, JListActionEventUser double-clicked a list item ItemEventUser clicked a list item Menu, JMenuActionEventUser selected menu item Scrollbar, JScrollbarAdjustmentEventUser moved scrollbar TextComponent, JTextComponentTextEventUser edited text TextField, JTextFieldActionEventUser typed Enter key Window, JWindowWindowEventUser manipulated window AWT events for each type of component. AWT events for each type of component.

5 New Swing Event Classes ComponentEvents Description JPopupMenuPopupMenuEvent User selected a choice JComponentAncestorEvent An event occurred in an ancestor JListListSelectionEvent User double-clicked a list item ListDataEvent List's contents were changed JMenuMenuEvent User selected menu item JTextComponentCaretEvent Mouse clicked in text UndoableEditEvent An undoable edit has occurred JTableTableModelEvent Items added/removed from table TableColumnModelEvent A table column was moved JTreeTreeModelEvent Items added/removed from tree TreeSelectionEvent User selected a tree node TreeExpansionEvent User changed tree node JWindowWindowEvent User manipulated window Newly defined Swing events. Newly defined Swing events.

6 GUI Design: Choosing Basic Components Swing objects for input, output, control, guidance: Swing objects for input, output, control, guidance: Guidance: A JLabel displays a short string of text or an image. It can serve as a prompt. Guidance: A JLabel displays a short string of text or an image. It can serve as a prompt. Input: A JTextField allows editing of a single line of text. It can get the user’s input. Input: A JTextField allows editing of a single line of text. It can get the user’s input. Output: A JTextArea allows editing of multiple lines of text. We’ll use it to display results. Output: A JTextArea allows editing of multiple lines of text. We’ll use it to display results. Control: A JButton is an action control. By implementing the ActionListener interface we will handle the user's action events. Control: A JButton is an action control. By implementing the ActionListener interface we will handle the user's action events.

7 Types of Layout Managers ManagerDescription java.awt.BorderLayoutArranges elements along the north, south, east, west, and in the center of the container. java.swing.BoxLayoutArranges elements in a single row or single column. java.awt.CardLayoutArranges elements like a stack of cards, with one visible at a time. java.awt.FlowLayoutArranges elements left to right across the container. java.awt.GridBagLayoutArranges elements in a grid of variable sized cells (complicated). java.awt.GridLayoutArranges elements into a two-dimensional grid of equally sized cells. java.swing.OverlayLayoutArranges elements on top of each other.

8 Default Layout Managers ContainerLayout Manager JAppletBorderLayout (on its content pane) JBoxBoxLayout JDialogBorderLayout (on its content pane) JFrameBorderLayout (on its content pane) JPanelFlowLayout JWindowBorderLayout (on its content pane) In AWT, the default layout for applets was FlowLayout. Top-level windows use BorderLayout

9 JTextField method setFont (inherited by JTextField indirectly from class Component ) sets the font of the JTextField to a new Font (package java.awt ). String passed to the JCheckBox constructor is the checkbox label that appears to the right of the JCheckBox by default. When the user clicks a JCheckBox, an ItemEvent occurs. – Handled by an ItemListener object, which must implement method itemStateChanged. An ItemListener is registered with method addItemListener. JCheckBox method isSelected returns true if a JCheckBox is selected. (C) 2010 Pearson Education, Inc. All rights reserved.

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25 Painting When a GUI needs to change its visual appearance it performs a paint operation Swing components generally repaint themselves as needed Painting code executes on the event- dispatching thread – If painting takes a long time, no events will be handled during that time

26 Example import javax.swing.*; import java.awt.*; public class Painting extends JPanel { public Painting() {} public void paintComponent(Graphics g) { super.paintComponent(g); g.setColor( Color.yellow ); g.fillOval( 10,10,50,50 ); g.setColor( Color.black ); g.drawOval( 10,10,50,50 ); } public static void main( String args[] ) { JFrame win = new JFrame( "Painting" ); win.setSize(100, 100); win.getContentPane().add( new Painting() ); win.setvisible(true); }}

27 The Graphics Object The Graphics object both a context for painting and methods for performing the painting. The graphics context consists of state such as the current painting color, the current font, and the current painting area – The color and font are initialized to the foreground color and font of the component just before the invocation of paintComponent

28 The Coordinate System Each component has its own integer coordinate system – Ranging from (0, 0) to (width - 1, height - 1) – Each unit represents the size of one pixel

29 Color Combinations of Red, Green, Blue Each [0, 255] Java: new Color(255, 150, 0) Hokie Orange

30 Font Color and font: g2.setColor( new Color(r,g,b) ); g2.setFont( new Font(…) ); new font(“Serif”, Font.BOLD, Font.Italic, 18);

31 Re-Paint Screen is like a painter’s canvas All windows paint on the same surface! Windows don’t “remember” whats under them Need to re-paint when portions are newly exposed Receive Repaint events Open, resize, bring to front When other windows in front move, resize, close

32 MVC Paradigm The MVC paradigm breaks applications or interfaces into three parts: the model, the view, and the controller. Users interact with a controller (e.g., buttons), and make changes to the model (e.g., data), which is then reflected in the view (e.g., graph). A --> 25 % B --> 60 % C --> 15 % Model View(s) Draw Graph Control

33 MVC Relationships ModelView Controller MVC Relationships The Controller has references to both the view and the model; the controller is the base of the triad. The Model might have a reference to the view, to allow for notification of changes. The model does not know who controls it, however. The View has a reference to the model, for gathering data. View also has reference to the Controller, to notify of successful updates. (References are minimal, perhaps to a base class, to allow swapping of controls.)

34 (C) 2010 Pearson Education, Inc. All rights reserved. Many event-listener interfaces contain multiple methods. An adapter class implements an interface and provides a default implementation (with an empty method body) of each method in the interface. You extend an adapter class to inherit the default implementation of every method and override only the method(s) you need for event handling.

35 (C) 2010 Pearson Education, Inc. All rights reserved.

36 Interface MouseWheelListener enables applications to respond to the rotation of a mouse wheel. Method mouseWheelMoved receives a MouseWheelEvent as its argument. Class MouseWheelEvent (a subclass of Mouse-Event ) contains methods that enable the event handler to obtain information about the amount of wheel rotation.

37 (C) 2010 Pearson Education, Inc. All rights reserved.

38

39

40

41

42

43

44

45

46

47

48

49 Class Point (package java.awt ) represents an x-y coordinate. – We use objects of this class to store the coordinates of each mouse drag event. Class Graphics is used to draw. MouseEvent method getPoint obtains the Point where the event occurred. Method repaint (inherited from Component ) indicates that a Component should be refreshed on the screen as soon as possible.

50 (C) 2010 Pearson Education, Inc. All rights reserved.

51 Graphics method fillOval draws a solid oval. – Four parameters represent a rectangular area (called the bounding box) in which the oval is displayed. – The first two are the upper-left x-coordinate and the upper-left y- coordinate of the rectangular area. – The last two represent the rectangular area’s width and height. Method fillOval draws the oval so it touches the middle of each side of the rectangular area.

52 (C) 2010 Pearson Education, Inc. All rights reserved.

53

54


Download ppt "Swing Hierarchy Swing classes derived from JComponent will be lightweight, written entirely in Java. The top-level Swing windows are heavyweight. They."

Similar presentations


Ads by Google