Swings PPT

download Swings PPT

of 21

Transcript of Swings PPT

  • 8/2/2019 Swings PPT

    1/21

  • 8/2/2019 Swings PPT

    2/21

    Swing components are built aspart of JFC(JAVA FOUNDATIONCLASSES).

    Swing components are lightweight components as they are

    written in java. Swing components are built on

    top of AWT.

    Swing components are consistent

    across all platforms they look andwork alike in all platforms.

    Swing components are madeavailable in javax.swing package.

  • 8/2/2019 Swings PPT

    3/21

    swing components

    ButtonsCommonbuttons

    Radio

    buttonsCheckbuttons

  • 8/2/2019 Swings PPT

    4/21

    Swing components

    Comboboxes

    Lists

    Menus

  • 8/2/2019 Swings PPT

    5/21

    Swing components

    Spinners

    Sliders

    Textfields

  • 8/2/2019 Swings PPT

    6/21

    Console vs. GUI applications

    What is the differencebetween a GUI and aconsole app?

    From the programmersperspective?

    A console app enables interactionthrough a specified flow of I/Os. A GUI

    app makes it much more flexible. Theuser is allowed to perform combinationsof actions. The programmer must havetaken all possible behaviors in mind.

  • 8/2/2019 Swings PPT

    7/21

    Swing classes

    Components you use to builda GUI app, are instances ofclasses contained in thejavax.swing package:

    JButton

    JTextField

    JRadioButton

    JCheckBox

    JComboBox

    JLabeletc

  • 8/2/2019 Swings PPT

    8/21

    Building GUIs essentials

    A GUI consists of a number ofcomponents contained in somepane.

    To appear onscreen, every GUIcomponent must be part of acontainment hierarchy. Acontainment hierarchy is a tree ofcomponents that has a top-levelcontainer as its root.

    For Java apps this top-levelcontainer will typically be a

    JFrame. Components will then be pinned

    on the top-level containerscontent pane.

  • 8/2/2019 Swings PPT

    9/21

    Building GUIs essentials

    JFrame

    object Containerobject

    JButton object

    JLabel object

  • 8/2/2019 Swings PPT

    10/21

    Building GUIs essentials

    Putting this in code:

    import javax.swing.*;

    public class MyApp extends JFrame{

    private JButton b1;

    private JLabel l1;

    Public MyApp(){

    super(SwingApplication");

    Container myCont = getContentPane();

    b1=new JButton(Im a Swing button!);l1= new JLabel(Number of button clicks: + num);

    myCont.add(b1);

    myCont.add(l1);

    }

  • 8/2/2019 Swings PPT

    11/21

  • 8/2/2019 Swings PPT

    12/21

    and again

    JList: JList()

    JList(Vector v)

    Example:

    String[] data = {"one", "two",

    "three", "four"};

    JList dataList = new JList(data);

    dataList.add(five);

  • 8/2/2019 Swings PPT

    13/21

    and again

    JRadioButton: JRadioButton(String s, Icon i,

    boolean state)

    Example:

    JRadioButton rb1= newJRadioButton(one);

    JRadioButton rb2= new

    JRadioButton(two);

    ButtonGroup bg= new ButtonGroup();

    bg.add(rb1);

    bg.add(rb2);

  • 8/2/2019 Swings PPT

    14/21

    Layout Managers

    You use layout managersto design your GUIs.

    There are severalmanagers like: FlowLayout

    BorderLayout

    GridLayout

    CardLayout GridBagLayout

  • 8/2/2019 Swings PPT

    15/21

    FlowLayout

    Default layout Components laid out

    from the top-left corner,

    from left to right and topto bottom like a text.

  • 8/2/2019 Swings PPT

    16/21

    BorderLayout

    Places components in up to five areas:top, bottom, left, right, and center. Allextra space is placed in the center area

  • 8/2/2019 Swings PPT

    17/21

    GridLayout

    Simply makes a bunch of componentsequal in size and displays them in the

    requested number of rows and columns

  • 8/2/2019 Swings PPT

    18/21

    CardLayout

    lets you implementan area that containsdifferent components

    at different times. ACardLayout is oftencontrolled by acombo box, with the

    state of the combobox determiningwhich panel (group ofcomponents) theCardLayout displays

  • 8/2/2019 Swings PPT

    19/21

    GridBagLayout

    is a sophisticated, flexible layout manager.It aligns components by placing themwithin a grid of cells, allowing some

    components to span more than one cell.

  • 8/2/2019 Swings PPT

    20/21

    Layout Managers

    Putting it into code: Setting the manager:Container myCont = getContentPane();

    myCont.setLayout(new FlowLayout());

    Adding Components:myCont.add(aComponent, BorderLayout.WEST);

  • 8/2/2019 Swings PPT

    21/21

    Refer text for moreinformation