Box2.java Source code

Java tutorial

Introduction

Here is the source code for Box2.java

Source

// : c14:Box2.java
// Adding struts.
// <applet code=Box2 width=450 height=300></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 Box2 extends JApplet {
    public void init() {
        Box bv = Box.createVerticalBox();
        for (int i = 0; i < 5; i++) {
            bv.add(new JButton("bv " + i));
            bv.add(Box.createVerticalStrut(i * 10));
        }
        Box bh = Box.createHorizontalBox();
        for (int i = 0; i < 5; i++) {
            bh.add(new JButton("bh " + i));
            bh.add(Box.createHorizontalStrut(i * 10));
        }
        Container cp = getContentPane();
        cp.add(BorderLayout.EAST, bv);
        cp.add(BorderLayout.SOUTH, bh);
    }

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

    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);
    }
} ///:~