import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.*; /** * A GUI application that plays with BoxLayout and * a subpanel using NO layout manager * * @author Sharon Tuttle * @version 2015-10-12 */ public class BoxPlusPlay { /** * creates a BoxPlusPlayFrame * * @param args not used here */ public static void main(String args[]) { EventQueue.invokeLater( new Runnable() { public void run() { BoxPlusPlayFrame mainFrame = new BoxPlusPlayFrame(); mainFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); mainFrame.setVisible(true); } }); } } /** * A frame with subcontainers using BoxLayout and * a panel using NO layout manager */ class BoxPlusPlayFrame extends JFrame { // data fields private final static int DEFAULT_WIDTH = 700; private final static int DEFAULT_HEIGHT = 650; /** * constructs a BoxPlusPlayFrame instance */ public BoxPlusPlayFrame() { this.setTitle("BoxPlusPlay"); this.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); // add BoxPlusPlay panel to frame BoxPlusPlayPanel panel = new BoxPlusPlayPanel(); this.add(panel); } } /** * A panel containing multiple subcontainers, some * using BoxLayout and one using no layout manager * */ class BoxPlusPlayPanel extends JPanel { // data fields for BoxPlusPlayPanel private final static Font DISPLAY_FONT = new Font("Dialog", Font.PLAIN, 20); /** * constructs a BoxPlusPlay panel instance */ public BoxPlusPlayPanel() { this.setLayout(new BorderLayout()); //******** // let's play with a horizontal box of buttons //******** Box topRowBox = Box.createHorizontalBox(); topRowBox.setBorder(new TitledBorder(new EtchedBorder(), "topRowBox")); // add 5 insensitive buttons to the box for (int i=0; i<5; i++) { JButton btn = new JButton("B" + i); if (i==2) { // add a bit of glue topRowBox.add(Box.createHorizontalGlue()); } // let's add a rigid area as well if (i==4) { topRowBox.add(Box.createRigidArea( new Dimension(50, 100))); } topRowBox.add(btn); } this.add(topRowBox, BorderLayout.PAGE_START); //******** // now let's add in a vertical box //******** Box lineStartBox = Box.createVerticalBox(); lineStartBox.setBorder(new EtchedBorder()); // silly: what happens if you add horizontal // glue to a vertical box? --- can't see any effect; lineStartBox.add(Box.createHorizontalGlue()); JButton b1 = new JButton("1"); lineStartBox.add(b1); // add a wide-ish rigid area next lineStartBox.add(Box.createRigidArea(new Dimension(100, 50))); JButton b2 = new JButton("2"); lineStartBox.add(b2); // and some more-proper vertical glue lineStartBox.add(Box.createVerticalGlue()); JButton b3 = new JButton("3"); lineStartBox.add(b3); this.add(lineStartBox, BorderLayout.LINE_START); //******** // let's add a center panel using NO layout manager //******** JPanel centerPanel = new JPanel(); centerPanel.setLayout(null); JButton mooy = new JButton("moo"); centerPanel.add(mooy); mooy.setBounds(125, 175, 500, 100); JButton oinky = new JButton("oink"); centerPanel.add(oinky); oinky.setBounds(150, 250, 50, 125); this.add(centerPanel, BorderLayout.CENTER); } }