Box1.java Source code

Java tutorial

Introduction

Here is the source code for Box1.java

Source

// : c14:Box1.java
// Vertical and horizontal BoxLayouts.
// <applet code=Box1 width=450 height=200></applet>
// From 'Thinking in Java, 3rd ed.' (c) Bruce Eckel 2002
// www.BruceEckel.com. See copyright notice in CopyRight.txt.

import java.awt.BorderLayout;
import java.awt.Container;

import javax.swing.Box;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JFrame;

public class Box1 extends JApplet {
    public void init() {
        Box bv = Box.createVerticalBox();
        for (int i = 0; i < 5; i++)
            bv.add(new JButton("bv " + i));
        Box bh = Box.createHorizontalBox();
        for (int i = 0; i < 5; i++)
            bh.add(new JButton("bh " + i));
        Container cp = getContentPane();
        cp.add(BorderLayout.EAST, bv);
        cp.add(BorderLayout.SOUTH, bh);
    }

    public static void main(String[] args) {
        run(new Box1(), 450, 200);
    }

    public static void run(JApplet applet, int width, int height) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(applet);
        frame.setSize(width, height);
        applet.init();
        applet.start();
        frame.setVisible(true);
    }
} ///:~