CIS 35A: Introduction to Java Programming

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

Swing

Swing
Layout managers
Border

Two frames with panels that use the Border layout manager

Common constructor and method of the BorderLayout class

Constructor Description
BorderLayout() Creates a Border layout manager.
Method Description
add(Component, regionField) Adds the component to the specified panel region.

Region fields of the BorderLayout class

NORTH WEST CENTER EAST SOUTH

Code that creates the Border Layout frame

class BorderLayoutPanel extends JPanel
{
    public BorderLayoutPanel()
    {
        JButton button1 = new JButton("Button 1 (NORTH)");
        JButton button2 = new JButton("Button 2 (WEST)");
        JButton button3 = new JButton("Button 3 (CENTER)");
        JButton button4 = new JButton("Button 4 (EAST)");
        JButton button5 = new JButton("Button 5 (SOUTH)");

        this.setLayout(new BorderLayout());
        this.add(button1, BorderLayout.NORTH);
        this.add(button2, BorderLayout.WEST);
        this.add(button3, BorderLayout.CENTER);
        this.add(button4, BorderLayout.EAST);
        this.add(button5, BorderLayout.SOUTH);
    }
}

Code that creates the Future Value Calculator frame

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

Previous | Flow | Border | Next