Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Swing Components Chapter 14.  Part of the Java Foundation Classes (JFC)  Provides a rich set of GUI components  Used to create a Java.

Similar presentations


Presentation on theme: "Introduction to Swing Components Chapter 14.  Part of the Java Foundation Classes (JFC)  Provides a rich set of GUI components  Used to create a Java."— Presentation transcript:

1 Introduction to Swing Components Chapter 14

2  Part of the Java Foundation Classes (JFC)  Provides a rich set of GUI components  Used to create a Java program with a graphical user interface (GUI)  table controls, list controls, tree controls, buttons, and labels, and so on…

3  In April 1997, JavaSoft announced the Java Foundation Classes (JFC).  a major part of the JFC is a new set of user interface components called Swing. AWTSwingAccessibility Java 2D Drag And Drop

4  AWT  java.awt  java.awt.color  java.awt.datatransfer  java.awt.event  java.awt.font  java.awt.geom  java.awt.image  Swing  javax.accessibility  javax.swing  javax.swing.colorchoos er  javax.swing.event  javax.swing.filechooser  javax.swing.plaf  javax.swing.table  javax.swing.text.html  javax.swing.tree

5  A GUI consists of different graphic Component objects which are combined into a hierarchy using Container objects.  Component class  An abstract class for GUI components such as menus, buttons, labels, lists, etc.  Container  An abstract class that extends Component. Containers can hold multiple components.

6  Container  Type of component that holds other components  Can treat group as single entity  Defined in Container class  Often takes form of window  Drag  Resize  Minimize  Restore  Close

7  Swing provides:  A wide variety of components (tables, trees, sliders, progress bars, internal frame, …)  Swing components can have tooltips placed over them  Arbitrary keyboard events can be bound to components  Additional debugging support.  Support for parsing and displaying HTML based information

8 JFrame window = new JFrame(" title " );  A JFrame is a Window with all of the adornments added  JFrame inherits from Frame, Window, Container, Component, and Object  A JFrame provides the basic building block for screen-oriented applications

9 import javax.swing.*; public class SwingFrame { public static void main( String args[] ) { JFrame window = new JFrame ( "My First GUI Program" ) ; window.setVisible(true); }

10  Set size and title window.setSize(200, 100); window.setTitle("My frame");  Close JFrame  Click Close button  JFrame becomes hidden and application keeps running  Default behavior  To change this behavior  Use setDefaultCloseOperation() method window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

11 import javax.swing.*; import java.awt.*; public class SwingFrame { public static void main( String args[] ) { JFrame window = new JFrame( "My First GUI Program" ); window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); window.setSize( 250, 150 ); window.setVisible(true); }

12  JFrame s have several panes:  Components are placed in the Content Pane Glass pane Layered pane Menu bar Content pane

13  JComponent  JComboBox, JLabel, JList, JMenuBar, JPanel, JPopupMenu, JScrollBar, JScrollPane, JTable, JTree, JInternalFrame, JOptionPane, JProgressBar, JRootPane, JSeparator, JSlider, JSplitPane, JTabbedPane, JToolBar, JToolTip, Jviewport, JColorChooser, JTextComponent, …

14 label = new JLabel("text", JLabel.RIGHT ); Font typeSet = new Font("Arial", Font.BOLD, 36 );  JLabels are components that you can fill with text.  When creating a label you can specify the initial value and the alignment you wish to use within the label.  You can use getText() and setText() to get and change the value of the label.  You can use setFont() to change the font, style and size of your text

15 import javax.swing.*; public class SwingFrame { public static void main( String args[] ) { JFrame window = new JFrame ( "My First GUI Program" ) ; JLabel label = new JLabel( "Hello World" ); Font typeSet = new Font( "Century", Font.BOLD, 28 ); label.setFont(typeSet); window.add( label ); window.setSize(250, 100); window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); window.setVisible(true); }

16  JButton extends Component, displays a string, and delivers an ActionEvent for each mouse click.  Normally buttons are displayed with a border  In addition to text, JButtons can also display icons button = new JButton( ”text“ );

17 import javax.swing.*; public class SwingFrame { public static void main( String args[] ) { JFrame window = new JFrame( "My First GUI Program" ); JButton button = new JButton( "Click Me!!" ); window.add( button ); window.setSize(250, 100); window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); win.setVisible(true); }

18  Layout Manager  An interface that defines methods for positioning and sizing objects within a container. Java defines several default implementations of LayoutManager.  Geometrical placement in a Container is controlled by a LayoutManager object

19 19  Containers may contain components (which means containers can contain containers!!).  All containers come equipped with a layout manager which positions and shapes (lays out) the container's components.  Much of the action in Swing occurs between components, containers, and their layout managers.

20 20  Layouts allow you to format components on the screen in a platform-independent way  The standard JDK provides many classes that implement the LayoutManager interface, including:  FlowLayout  GridLayout  BorderLayout  BoxLayout  CardLayout  OverlayLayout  GridBagLayout

21 21  Invoke the setLayout() method on the container to use the new layout. setLayout( new FlowLayout() );  The layout manager should be established before any components are added to the container  JFrame will otherwise stack all components on top of each other

22 22  When you add components to the screen, they flow left to right (centered) based on the order added and the width of the screen.  Very similar to word wrap and full justification on a word processor.  If the screen is resized, the components' flow will change based on the new width and height  FlowLayout is the default layout for the JPanel class.

23 import javax.swing.*; import java.awt.*; public class LayOut { public static void main( String args[] ) { JFrame window = new JFrame( "Layout Program" ); JLabel title = new JLabel( "Layout Example" ); JLabel name = new JLabel( "Enter your name" ); JTextField textInput = new JTextField( 12 ); JButton button = new JButton( "Enter your name" ); window.setSize(200, 150); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); window.setVisible(true); window.setLayout( new FlowLayout() ); window.add( title ); window.add( name ); window.add( textInput ); window.add( button ); } }

24 24 import javax.swing.*; import java.awt.*; public class ShowFlowLayout { public static void main( String args[] ) { JFrame win = new JFrame( "My First GUI Program" ); win.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); win.getContentPane().setLayout( new FlowLayout() ); for ( int i = 0; i < 10; i++ ) { win.getContentPane().add( new JButton( String.valueOf( i ) ) ); } win.setVisible(true); }

25 25 You can resize with the mouse and the buttons will move to fill in the space

26  Programs respond to events that are generated outside the control of the program  User types a key  The left mouse button is pressed  A CD is removed from the CD drive  When an event occurs, it is handled by an event handler  Event driven programming involves 1. writing the handlers, and 2. arranging for the handler to be notified when certain events occur

27  An event is represented by an object that gives information about the event and identifies the event source  Event sources are typically components, but other kinds of objects can also be event sources  A listener is an object that wants to be notified when a particular event occurs  An event source can have multiple listeners registered on it  A single listener can register with multiple event sources

28  In order for an object to be notified when a particular event occurs, the object 1. must implement the appropriate Listener interface 2. must be registered as an event listener on the appropriate event source

29 ActionListener Type User clicks a button, presses return while typing in a text field, or chooses a menu item ActionListener Users closes a frame (main window) WindowListener User presses a mouse button while the cursor is over a component MouseListener User moves the mouse over a component MouseMotionListener A component becomes visible ComponentListener A component gets the keyboard focus FocusListener A table of list selection changes ListSelectionListener


Download ppt "Introduction to Swing Components Chapter 14.  Part of the Java Foundation Classes (JFC)  Provides a rich set of GUI components  Used to create a Java."

Similar presentations


Ads by Google