Listing 5.1 FlowLayout (TFlowLayout.java)
/*
*
*/
import javax.swing.*;
import java.awt.*;
public class TFlowLayout extends JApplet {
Container container = null;
public void init() {
// 1. Get the handle on applet's content pane.
container = this.getContentPane();
// 2. Create the flow layout object and set it for the applet.
FlowLayout fl = new FlowLayout(FlowLayout.LEFT, 5, 10);
// horizontal gap = 5 pixels, vertical gap = 10 pixels
container.setLayout(fl);
// 3. Create and add some buttons to the applet.
for (int i=0; i<4; i++) {
JButton button = new JButton("Button"+(i+1));
button.setPreferredSize(new Dimension(100,25)); // width=100, height=25
container.add(button);
}
}
}
Listing 5.2 GridLayout (TGridLayout.java)
/*
*
*/
import javax.swing.*;
import java.awt.*;
public class TGridLayout extends JApplet {
Container container = null;
public void init() {
// 1. Get a handle on the applet's content pane.
container = this.getContentPane();
// 2. Assign the grid layout to the content pane.
GridLayout grid = new GridLayout(3,2, 5,5);
// rows = 3, cols = 2, horizontal gap =5, vertical gap = 5
container.setLayout(grid);
// 3. Create and add components to the content pane.
for (int i=0; i<6; i++)
container.add(new JButton("Button"+(i+1)));
}
}
Listing 5.3 Border Layout Manager (TBorderLayout.java)
/*
*/
import java.awt.*;
import javax.swing.*;
public class TBorderLayout extends JApplet {
Container container = null;
public void init() {
//1. Get a handle on the applet's content pane.
container = this.getContentPane();
// container.setLayout(new BorderLayout());
/* Note: Don't have to explicitly set border layout,
because the content pane uses border layout
by default */
// 2. Give some horizontal and vertical gaps between buttons.
((BorderLayout) container.getLayout()).setHgap(2);
((BorderLayout) container.getLayout()).setVgap(2);
// 3. Array that contains border layout constants.
String[] borderConsts = { BorderLayout.NORTH,
BorderLayout.SOUTH,
BorderLayout.EAST,
BorderLayout.WEST,
BorderLayout.CENTER };
// 4. Array that contains button names.
String[] buttonNames = { "North Button", "South Button",
"East Button", "West Button",
"Center Button" };
// 5. Create and add buttons to the container.
for (int i=0; i
*
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
public class TCardLayout extends JApplet {
// 1. Declare some panel and button references.
JPanel controlPanel;
JPanel cardsPanel, swCompPanel, aeroCompPanel;
JButton nextButton, previousButton;
JButton javasoftButton, sunButton,
ibmButton, netscapeButton;
JButton nasaButton, boeingButton,
lockheedButton, northropButton;
URL currentSite = null;
// 2. Define the string arrays for the sites and site urls.
String[] swCompSites = {"JavaSoft", "Sun",
"IBM", "NetScape"};
String[] aeroCompSites = {"NASA", "Boeing",
"Lockheed", "Northrop"};
String[] siteURLs = { "http://www.javasoft.com/",
"http://www.sun.com/",
"http://www.ibm.com/",
"http://www.netscape.com/" };
Container container = null;
public void init() {
// 3. Get a handle on the applet's content pane.
container = this.getContentPane();
// 4. Create a control panel object.
controlPanel = new JPanel();
controlPanel.setLayout(new GridLayout(1,2,10,10));
// 5. Create and add the next and previous control buttons.
ButtonListener listener = new ButtonListener();
nextButton = new JButton("Next Card");
nextButton.setActionCommand("Next Button");
nextButton.addActionListener(listener);
controlPanel.add(nextButton);
previousButton = new JButton("Previous Card");
previousButton.setEnabled(false);
previousButton.setActionCommand("Previous Button");
previousButton.addActionListener(listener);
controlPanel.add(previousButton);
// 6. Create a panel to contain the cards.
/* Each card will display a set of buttons to
visit a specific Web site. */
cardsPanel = new JPanel();
cardsPanel.setLayout(new CardLayout());
swCompPanel = new JPanel();
aeroCompPanel = new JPanel();
swCompPanel.setLayout(new GridLayout(2,2,10,10));
aeroCompPanel.setLayout(new GridLayout(2,2,10,10));
// 7. Add buttons to the these cards.
for (int i=0; i
*
*/
import javax.swing.*;
import java.awt.*;
public class TGridBagLayout extends JApplet {
Container container = null;
public void init() {
// 1. Get a handle on the applet's content pane.
container = this.getContentPane();
// 2. Set the container to the grid bag layout and
// define a constraint object.
GridBagLayout gridbag = new GridBagLayout();
container.setLayout(gridbag);
GridBagConstraints c = new GridBagConstraints();
// 3. Common settings for constraint object instant variables.
c.fill = GridBagConstraints.BOTH;
// 4. Settings for button B1.
c.insets = new Insets(5,5,5,5);
c.gridx = 0; c.gridy = 0;
c.gridwidth = 2; c.gridheight = 2;
c.weightx = 1.0; c.weighty = 1.0;
makeButton("B1", gridbag, c);
// 5. Settings for button B2.
c.insets = new Insets(0,0,0,0);
c.gridx = 2; c.gridy = 0;
c.gridwidth = 1; c.gridheight = 3;
makeButton("B2", gridbag, c);
// 6. Settings for button B3.
c.gridx = 0; c.gridy = 2;
c.gridwidth = 1; c.gridheight = 1;
c.weightx = 1.0; c.weighty=0.5;
makeButton("B3", gridbag, c);
// 7. Settings for button B4.
c.gridx = 1; c.gridy = 2;
makeButton("B4", gridbag, c);
}
// 8. Define the function to create and add a button
// according to the constraints.
public void makeButton(String name,
GridBagLayout gridbag,
GridBagConstraints c) {
JButton button = new JButton(name);
gridbag.setConstraints(button, c);
container.add(button);
}
}
Listing 5.6 BoxLayout Example Using Invisible Components (TBox1.java)
// Demonstrates the positioning of components in a box using
// invisible components
/*
*
*/
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
public class TBox1 extends JApplet {
Container container = null;
public void init() {
// 1. Get the handle on the applet's content pane.
container = getContentPane();
// 2. Create a vertical box and add it to the applet.
Box baseBox = Box.createVerticalBox();
container.add(baseBox);
// 3. Create the demo panel1 with box layout to display
// buttons B1 and B2.
// a) Create the panel and set the box layout.
JPanel glueDemoPanel1 = new JPanel();
glueDemoPanel1.setLayout(new BoxLayout(glueDemoPanel1,
BoxLayout.X_AXIS));
// b) Set the title border around the panel.
TitledBorder border1 = new TitledBorder(
new LineBorder(Color.black),
"Glue Component: Demo-1");
border1.setTitleColor(Color.black);
glueDemoPanel1.setBorder(border1);
// c) Add the buttons B1 and B2 with a glue component
// between them.
glueDemoPanel1.add(new JButton("B1"));
glueDemoPanel1.add(Box.createHorizontalGlue());
glueDemoPanel1.add(new JButton("B2"));
// d) Add the panel to the base box.
baseBox.add(glueDemoPanel1);
// 4. Create the demo panel-2, assign box layout and add
// buttons B3 and B4.
// a) Create the glue panel 2 and assign the box layout.
JPanel glueDemoPanel2 = new JPanel();
glueDemoPanel2.setLayout(new BoxLayout(glueDemoPanel2,
BoxLayout.X_AXIS));
// b) Add a titled border to the panel.
TitledBorder border2 = new TitledBorder(
new LineBorder(Color.black),
"Glue Component: Demo-2");
border2.setTitleColor(Color.black);
glueDemoPanel2.setBorder(border2);
// c) Add the buttons B3 and B4 to the panel; also add
// the glue components between the buttons, and the
// buttons and panel.
glueDemoPanel2.add(Box.createHorizontalGlue());
glueDemoPanel2.add(new JButton("B3"));
glueDemoPanel2.add(Box.createHorizontalGlue());
glueDemoPanel2.add(new JButton("B4"));
glueDemoPanel2.add(Box.createHorizontalGlue());
// d) Add the panel to the base box.
baseBox.add(glueDemoPanel2);
// 5. Create a filler panel and add buttons B5 and B6.
// a) Create the panel object and assign box layout.
JPanel fillerDemoPanel = new JPanel();
fillerDemoPanel.setLayout(new BoxLayout(fillerDemoPanel,
BoxLayout.X_AXIS));
// b) Set the titled border to the above panel.
TitledBorder border3 = new TitledBorder(
new LineBorder(Color.black),
"Filler Component Demo");
border3.setTitleColor(Color.black);
fillerDemoPanel.setBorder(border3);
// c) Add buttons B5 and B6 to the panel; also add the
// filler component between the buttons.
fillerDemoPanel.add(new JButton("B5"));
fillerDemoPanel.add(new Box.Filler(
new Dimension(50,75), // Minimum Size
new Dimension(50,75), // Preferred Size
new Dimension(Short.MAX_VALUE,75)));
// Maximum Value
fillerDemoPanel.add(new JButton("B6"));
// Attach the panel to the base box.
baseBox.add(fillerDemoPanel);
// 6. Create rigid area panel and add buttons B7 and B8.
// a) Create a panel and assign the box layout.
JPanel rigidADemoPanel = new JPanel();
rigidADemoPanel.setLayout(new BoxLayout(rigidADemoPanel,
BoxLayout.X_AXIS));
// b) Assign the title border around the panel.
TitledBorder border4 = new TitledBorder(
new LineBorder(Color.black),
"Rigid Area Component Demo");
border4.setTitleColor(Color.black);
rigidADemoPanel.setBorder(border4);
// c) Add buttons B7 and B8 to the rigid area panel.
// Also add a rigid area in the middle of the buttons.
rigidADemoPanel.add(new JButton("B7"));
rigidADemoPanel.add(Box.createRigidArea(new
Dimension(150,0)));
rigidADemoPanel.add(new JButton("B8"));
// d) Add the panel to the base box.
baseBox.add(rigidADemoPanel);
// 7. Create the strut panel, assign the box layout, and
// add the buttons B9 and B10.
// a) Create the panel and assign the box layout
JPanel strutDemoPanel = new JPanel();
strutDemoPanel.setLayout(new BoxLayout(strutDemoPanel,
BoxLayout.X_AXIS));
// b) Set the titled border around the panel.
TitledBorder border5 = new TitledBorder(
new LineBorder(Color.black),
"Strut Component Demo");
border5.setTitleColor(Color.black);
// c) Add the buttons B9 and B10 to the panel. Also assign
// the horizontal strut in the middle of the buttons.
strutDemoPanel.setBorder(border5);
strutDemoPanel.add(new JButton("B9"));
strutDemoPanel.add(Box.createHorizontalStrut(150));
strutDemoPanel.add(new JButton("B10"));
// d) Add the panel to the base box.
baseBox.add(strutDemoPanel);
}
}
Listing 5.7 BoxLayout Example Using Multiple Nesting (TBox2.java)
// Multiple-nesting of boxes can produce the effect of
// grid bag layout
/*
*
*/
import javax.swing.*;
import java.awt.*;
public class TBox2 extends JApplet {
Container container = null;
public void init() {
// 1. Get the handle on the applets' content pane
// and set the background color to white.
container = getContentPane();
container.setBackground(Color.white);
// 2. Create a horizontal box and add it to the applet.
Box baseBox = Box.createHorizontalBox();
container.add(baseBox);
// 3. Create a vertical box and add it to the base box.
Box vBox = Box.createVerticalBox();
baseBox.add(vBox);
// 4. Create button B1 and add it to vBox.
JButton b1 = new JButton("B1");
b1.setAlignmentX(Component.CENTER_ALIGNMENT);
b1.setMaximumSize(new Dimension(150,150));
vBox.add(b1);
// 5. Create a horizontal box and add it to vBox.
Box hBox = Box.createHorizontalBox();
vBox.add(hBox);
// 6. Create button B2 and add it to hBox.
JButton b2 = new JButton("B2");
b2.setAlignmentY(Component.CENTER_ALIGNMENT);
b2.setMaximumSize(new Dimension(75,100));
hBox.add(b2);
// 7. Create another button B3 and add it to hBox.
JButton b3 = new JButton("B3");
b3.setAlignmentY(Component.CENTER_ALIGNMENT);
b3.setMaximumSize(new Dimension(75,100));
hBox.add(b3);
// 8. Create the vertical box (vBox1) and
// add it to the base box.
Box vBox1 = Box.createVerticalBox();
baseBox.add(vBox1);
// 9. Create the button B4 and add it to vBox1.
JButton b4 = new JButton("B4");
b4.setAlignmentX(Component.CENTER_ALIGNMENT);
b4.setMaximumSize(new Dimension(100,250));
vBox1.add(b4);
}
}