JPanel

download JPanel

of 6

Transcript of JPanel

  • 8/3/2019 JPanel

    1/6

    Creating a JPanel

    Constructor Purpose

    JPanel()

    JPanel(LayoutManager)

    Creates a panel. The

    LayoutManager parameter

    provides a layout manager for the

    new panel. By default, a panel uses a

    FlowLayout to lay out its

    components.

    Managing a Container's Components

    Method Purpose

    void add(Component)

    void add(Component, int)

    void add(Component,

    Object)

    Adds the specified component

    to the panel.

    When present, the int

    parameter is the index of the

    component within the

    container. By default, the first

    component added is at index 0,

    the second is at index 1, and so

    on.

    The Object parameter is

    layout manager dependent and

    typically provides information to

  • 8/3/2019 JPanel

    2/6

    void add(Component,

    Object, int)

    void add(String,

    Component)

    the layout manager regarding

    positioning and other layout

    constraints for the added

    component.

    The String parameter is

    similar to the Object

    parameter.

    int getComponentCount()

    Gets the number of components

    in this panel.

    Component

    getComponent(int)

    Component

    getComponentAt(int, int)

    Component

    getComponentAt(Point)

    Component[]

    getComponents()

    Gets the specified component or

    components. You can get a

    component based on its index or

    x, yposition.

    void remove(Component)

    void remove(int)

    void removeAll()

    Removes the specified

    component(s).

    Setting or Getting the LayoutManager

    Method Purpose

  • 8/3/2019 JPanel

    3/6

    void

    setLayout(LayoutManager)

    LayoutManager

    getLayout()

    Sets or gets the layout manager

    for this panel. The layout

    manager is responsible for

    positioning the panel'scomponents within the panel's

    bounds according to some

    philosophy.

    import java.awt.BorderLayout;

    import java.awt.Color;

    import java.awt.Font;import java.awt.event.WindowAdapter;

    import java.awt.event.WindowEvent;

    import javax.swing.JButton;

    import javax.swing.JFrame;

    import javax.swing.JPanel;

    /**

    * A component subclass that demonstrates

    nested containers and components. It

    * creates the hierarchy shown below, and

  • 8/3/2019 JPanel

    4/6

    uses different colors to distinguish

    * the different nesting levels of the co

    ntainers

    *

    * Containers---panel1----button1 | |---

    panel2----button2 | |

    * |----panel3----button3 | |------

    panel4----button4 | |----button5 |---

    button6

    */

    public c

    lassConta

    i

    nersex

    tends

    JPanel {public Containers() {

    this.setBackground(Color.white); // T

    his component is white

    this.setFont(newFont("Dialog", Font.

    BOLD, 24));

    JPanel p1 =new

    JPanel();p1.setBackground(newColor(200, 200,

    200)); // Panel1 is darker

    this.add(p1); // p1 is contained by t

    his component

    p1.add(newJButton("#1")); // Button

    1 is contained in p1

    JPanel p2 = newJPanel();

    p2.setBackground(newColor(150, 150,

    150)); // p2 is darker than p2

    p1.add(p2); // p2 is contained in p1

  • 8/3/2019 JPanel

    5/6

    p2.add(newJButton("#2")); // Button

    2 is contained in p2

    JPanel p3 = newJPanel();

    p3.setBackground(newColor(100, 100,

    100)); // p3 is darker than p2

    p2.add(p3); // p3 is contained in p2

    p3.add(newJButton("#3")); // Button

    3 is contained in p3

    JPanel p4 =new

    JPanel();p4.setBackground(newColor(150, 150,

    150)); // p4 is darker than p1

    p1.add(p4); // p4 is contained in p1

    p4.add(newJButton("#4")); // Button4

    is contained in p4

    p4.add(newJButton("#5")); // Button5

    is also conta

    ined

    in p4

    this.add(newJButton("#6")); // Butto

    n6 is contained in this component

    }

    public static voidmain(String[] args)

    {JFrame frame = newJFrame();

    frame.addWindowListener(newWindowAda

    pter() {

    public voidwindowClosing(WindowEve

  • 8/3/2019 JPanel

    6/6

    nt e) {

    System.exit(0);

    }

    });

    frame.getContentPane().add(newContai

    ners(), BorderLayout.CENTER);

    // Finally, set the size of the main

    window, and pop it up.

    frame.setSize(600, 400);

    f

    rame.setVi

    si

    ble(true

    );}

    }