CIS 35A: Introduction to Java Programming

Home | Green Sheet | Lectures | Assignments | FAQ | Grades

Swing

Swing
Layout managers
Flow

Two panels that use the Flow layout manager

The setLayout method of the Container class

Method Description
setLayout(LayoutManager) Sets the layout manager for this container.

Two constructors of the FlowLayout class

Constructor Description
FlowLayout() Creates a Flow layout with centered alignment.
FlowLayout(alignmentField) Creates a Flow layout with the specified alignment.

Alignment fields of the FlowLayout class

CENTER LEFT RIGHT

Code that creates the centered button panel

class ButtonPanel extends JPanel
{
    public ButtonPanel()
    {
        this.setLayout(
            new FlowLayout(FlowLayout.CENTER));
        this.add(new JButton("Button One"));
        this.add(new JButton("Button Two"));
        this.add(new JButton("Button Three"));
        this.add(new JButton("Button Four"));
        this.add(new JButton("Button Five"));
    }
}

Code that creates the right-aligned button panel

class FutureValuePanel extends JPanel
{
    private JButton calculateButton, exitButton;
    public FutureValuePanel()
    {
        this.setLayout(new FlowLayout(FlowLayout.RIGHT));
        calculateButton = new JButton("Calculate");
        this.add(calculateButton);
        exitButton = new JButton("Exit");
        this.add(exitButton);
    }
}

Previous | Flow | Border | Next