Commit 0271a291 authored by Keiko Katsuragawa's avatar Keiko Katsuragawa

add 2-6-Layout

parent 1f3188e5
import java.awt.*;
import javax.swing.*;
import java.awt.LayoutManager;
import java.awt.Insets;
import java.util.ArrayList;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
// give positions and sizes for all widgets
public class DemoAlignLayout extends JFrame {
public static void main(String[] args) {
DemoAlignLayout demo = new DemoAlignLayout();
}
DemoAlignLayout() {
this.setSize(800, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// layout
this.setLayout(new AlignLayout(40, 40));
// add components
JComponent c;
c = new JLabel("Number");
// try uncommenting this
// c.setPreferredSize(new Dimension(200, 50));
debugBorder(c);
this.add(c);
c = new JSlider(0, 100, 50);
debugBorder(c);
this.add(c);
c = new JLabel("Choice");
debugBorder(c);
this.add(c);
// create a radio button group
ButtonGroup radiobuttons = new ButtonGroup();
JPanel radioPanel = new JPanel(new GridLayout(1, 0));
for (String s: new String[] {"A", "B", "C"})
{
JRadioButton rb = new JRadioButton(s);
radiobuttons.add(rb);
radioPanel.add(rb);
}
debugBorder(radioPanel);
this.add(radioPanel);
System.out.println("Preferred size: " + this.getPreferredSize());
System.out.println("Minimum size: " + this.getMinimumSize());
this.setVisible(true);
}
// I made this to make it easy to add some debug information
void debugBorder(JComponent c) {
// try uncommenting this
c.setBorder(BorderFactory.createLineBorder(Color.black));
}
}
/**
* A layout manager that arranges components in a horizontal line equally spaced
* and centred vertically in the parent container.
*/
class AlignLayout implements LayoutManager {
int minimumSpacing;
int preferredSpacing;
/** Construct a new AlignLayout object with spacing between components. */
public AlignLayout(int minSpacing, int preferredSpacing) {
super();
this.minimumSpacing = minimumSpacing;
this.preferredSpacing = preferredSpacing;
}
// for specialized layouts, not used here
public void addLayoutComponent(String name, Component comp) { }
public void removeLayoutComponent(Component comp) { }
/**
* Calculates the preferred size dimensions for the specified panel given
* the components in the specified parent container.
*/
public Dimension preferredLayoutSize(Container parent) {
synchronized (parent.getTreeLock()) {
// get space needed for all children
Dimension space = calculateSpace(parent, true);
// this container's padding
Insets insets = parent.getInsets();
Dimension d = new Dimension(insets.left + space.width +insets.right,
insets.top + space.height + insets.bottom);
return d;
}
}
/**
* Calculates the minimum size dimensions for the specified panel given the
* components in the specified parent container.
*/
public Dimension minimumLayoutSize(Container parent) {
synchronized (parent.getTreeLock()) {
// get space needed for all children
Dimension space = calculateSpace(parent, false);
// this container's padding
Insets insets = parent.getInsets();
Dimension d = new Dimension(insets.left + space.width +insets.right,
insets.top + space.height + insets.bottom);
return d;
}
}
/**
* Lays out the container in the specified panel.
*/
public void layoutContainer(Container parent) {
synchronized (parent.getTreeLock()) {
// get space needed for all children (preferred)
Dimension space = calculateSpace(parent, true);
// this container's padding
Insets insets = parent.getInsets();
// get actual space available in parent
int w = parent.getWidth() - insets.left - insets.right;
int h = parent.getHeight() - insets.top - insets.bottom;
System.out.println("layoutContainer (parent size " + w + "," + h + ")");
// vertical centre line to layout component
int y = h / 2;
// starting x is left side of all components to lay out
int x = (w - space.width) / 2;
int nComponents = parent.getComponentCount();
for (int i = 0; i < nComponents; i++) {
Component c = parent.getComponent(i);
Dimension d = c.getPreferredSize();
c.setBounds(x, y - d.height / 2, d.width, d.height);
x += d.width + preferredSpacing;
}
}
}
/*
* Precondition: the caller has gotten the treelock.
*/
private Dimension calculateSpace(Container parent, boolean isPreferred) {
// find total width for all components and
// height of tallest component
Dimension result = new Dimension(0,0);
int nComponents = parent.getComponentCount();
for (int i = 0; i < nComponents; i++) {
Dimension d;
if (isPreferred) {
d = parent.getComponent(i).getPreferredSize();
} else {
d = parent.getComponent(i).getMinimumSize();
}
// update the total width and height required
result.width += d.width;
result.height = Math.max(result.height, d.height);
}
// add spacing in between components
if (isPreferred) {
result.width += (nComponents - 1) * preferredSpacing;
} else {
result.width += (nComponents - 1) * minimumSpacing;
}
return result;
}
}
import java.awt.*;
import javax.swing.*;
import java.awt.LayoutManager;
import java.awt.Insets;
import java.util.ArrayList;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
// give positions and sizes for all widgets
public class DemoFormLayout extends JFrame {
public static void main(String[] args) {
DemoFormLayout demo = new DemoFormLayout();
}
DemoFormLayout() {
this.setSize(400, 150);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// layout
this.setLayout(new FormLayout(10, 10));
// add first pair
JComponent c;
c = new JLabel("Number");
// try uncommenting this
// c.setPreferredSize(new Dimension(200, 50));
debugBorder(c);
this.add(c);
c = new JSlider(0, 100, 50);
debugBorder(c);
this.add(c);
// add second pair
c = new JLabel("Choice");
debugBorder(c);
this.add(c);
// create a radio button group
ButtonGroup radiobuttons = new ButtonGroup();
JPanel radioPanel = new JPanel(new GridLayout(1, 0));
for (String s: new String[] {"A", "B", "C"})
{
JRadioButton rb = new JRadioButton(s);
radiobuttons.add(rb);
radioPanel.add(rb);
}
debugBorder(radioPanel);
this.add(radioPanel);
this.setVisible(true);
}
void debugBorder(JComponent c) {
// try uncommenting this
// c.setBorder(BorderFactory.createLineBorder(Color.black));
}
}
/**
* A layout manager that arranges components in a double column. In most cases
* the left column will hold a label and the right column will hold a component
* the user can manipulate. Preferred component sizes are respected as much as
* possible. Components in the left column are right justified; components in
* the right column are left justified.
*
* @author Byron Weber Becker
*/
class FormLayout implements LayoutManager {
private int hGap = 8;
private int vGap = 8;
/** Construct a new FormLayout object. */
public FormLayout() {
super();
}
/**
* Construct a new FormLayout object.
*
* @param hGap
* the number of hortizontal pixels between components
* @param vGap
* the number of vertical pixels between components
*/
public FormLayout(int hGap, int vGap) {
super();
this.hGap = hGap;
this.vGap = vGap;
}
/**
* Adds the specified component with the specified name to the layout.
*
* @param name
* the component name
* @param comp
* the component to be added
*/
public void addLayoutComponent(String name, Component comp) {
}
/**
* Removes the specified component from the layout.
*
* @param comp
* the component to be removed
*/
public void removeLayoutComponent(Component comp) {
}
/**
* Calculates the preferred size dimensions for the specified panel given
* the components in the specified parent container.
*
* @param parent
* the component to be laid out
*
* @see #minimumLayoutSize
*/
public Dimension preferredLayoutSize(Container parent) {
synchronized (parent.getTreeLock()) {
ColDims cd = colDims(parent, true);
Insets insets = parent.getInsets();
Dimension d = new Dimension(cd.left + cd.right + insets.left
+ insets.right, cd.height + insets.top + insets.bottom);
return d;
}
}
/*
* Precondition: the caller has gotten the treelock. preferred = true for
* preferred sizes; false for minimum sizes
*/
private ColDims colDims(Container parent, boolean preferred) {
// ColDims: left col width, right col width, height of all rows
ColDims cd = new ColDims();
int nComponents = parent.getComponentCount();
// step through rows (every other component)
for (int i = 1; i < nComponents; i += 2) {
Component left = parent.getComponent(i - 1);
Component right = parent.getComponent(i);
Dimension dLeft;
Dimension dRight;
if (preferred) {
dLeft = left.getPreferredSize();
dRight = right.getPreferredSize();
} else {
dLeft = left.getMinimumSize();
dRight = right.getMinimumSize();
}
cd.left = (int) Math.max(cd.left, dLeft.width);
cd.right = (int) Math.max(cd.right, dRight.width);
cd.height += (int) Math.max(dLeft.height, dRight.height);
cd.height += this.vGap;
}
// edge case (odd number of components...)
if (nComponents % 2 == 1) { // get the last one on the left
Component left = parent.getComponent(nComponents - 1);
Dimension dLeft;
if (preferred) {
dLeft = left.getPreferredSize();
} else {
dLeft = left.getMinimumSize();
}
cd.left = (int) Math.max(cd.left, dLeft.width);
cd.height += dLeft.height + this.vGap;
}
cd.left += this.hGap / 2;
cd.right += this.hGap / 2;
return cd;
}
/**
* Calculates the minimum size dimensions for the specified panel given the
* components in the specified parent container.
*
* @param parent
* the component to be laid out
* @see #preferredLayoutSize
*/
public Dimension minimumLayoutSize(Container parent) {
synchronized (parent.getTreeLock()) {
ColDims cd = colDims(parent, false);
Insets insets = parent.getInsets();
Dimension d = new Dimension(cd.left + cd.right + insets.left
+ insets.right, cd.height + insets.top + insets.bottom);
return d;
}
}
/**
* Lays out the container in the specified panel.
*
* @param parent
* the component which needs to be laid out
*/
public void layoutContainer(Container parent) {
Insets insets = parent.getInsets();
synchronized (parent.getTreeLock()) {
ColDims cd = this.colDims(parent, true);
int desiredWidth = cd.left + cd.right + insets.left + insets.right;
double widthScale = Math.min(1.0, parent.getWidth()
/ (double) desiredWidth);
double heightScale = Math.min(1.0, parent.getHeight()
/ (double) cd.height);
double scale = Math.min(widthScale, heightScale);
// System.out.println("FormLayout.layoutContainer: widthScale = " +
// widthScale + "; heightScale = " + heightScale);
int midPt = (int) (((cd.left + insets.left + this.hGap) / (double) (cd.left
+ cd.right + insets.left + insets.right + hGap)) * parent
.getWidth());
int top = insets.top + this.vGap;
int nComponents = parent.getComponentCount();
for (int i = 1; i < nComponents; i += 2) {
Component left = parent.getComponent(i - 1);
Component right = parent.getComponent(i);
Dimension lDim = left.getPreferredSize();
Dimension rDim = right.getPreferredSize();
int rowHeight = (int) (Math.max(lDim.height, rDim.height) * heightScale);
// scale left side, if necessary; then position
if (lDim.width > desiredWidth || lDim.height > rowHeight) {
lDim.width = (int) (lDim.width * scale);
lDim.height = (int) (lDim.height * scale);
}
left.setBounds((midPt - lDim.width - this.hGap / 2), top,
lDim.width, lDim.height);
// scale right side, if necessary; then position
if (rDim.width > desiredWidth || rDim.height > rowHeight) {
rDim.width = (int) (rDim.width * scale);
rDim.height = (int) (rDim.height * scale);
}
right.setBounds(midPt + this.hGap / 2, top, rDim.width,
rDim.height);
top = top + rowHeight + this.vGap;
}
if (nComponents % 2 == 1) { // odd number of components -- get the
// last one on the left
Component left = parent.getComponent(nComponents - 1);
Dimension lDim = left.getPreferredSize();
lDim.width = (int) (lDim.width * scale);
lDim.height = (int) (lDim.height * scale);
left.setBounds((midPt - lDim.width - this.hGap / 2), top,
lDim.width, lDim.height);
}
}
}
private static class ColDims {
int left = 0;
int right = 0;
int height = 0;
}
}
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import javax.swing.SpringLayout;
import java.awt.LayoutManager;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
/* Demonstrate various layout managers available in Java,
* including defining your own layout manager.
*
* @author Byron Weber Becker
*/
public class LayoutDemo {
public static void main(String[] args) {
// These are stacked bottom to top, so put the first one
// to be discussed last.
new LayoutFrame("RandomLayout", new DemoRandomLayout());
new LayoutFrame("SpringLayout", new DemoSpringLayout());
new LayoutFrame("GridBagLayout", new DemoGridBagLayout());
new LayoutFrame("BorderLayout", new DemoBorderLayout());
new LayoutFrame("BoxLayout3", new DemoBoxLayout3());
new LayoutFrame("BoxLayout2", new DemoBoxLayout2());
new LayoutFrame("BoxLayout1", new DemoBoxLayout1());
new LayoutFrame("GridLayout", new DemoGridLayout());
new LayoutFrame("FlowLayout", new DemoFlowLayout());
new LayoutFrame("NullLayout", new DemoNullLayout());
}
}
class LayoutFrame extends JFrame {
private static int xPos = 10;
private static int yPos = 10;
private static final int OFFSET = 50;
public LayoutFrame(String title, JPanel contents) {
super(title);
this.setContentPane(contents);
this.setSize(300, 130);
// set this to keep the JFrame from shrinking too small
//this.setMinimumSize(new Dimension(150, 100));
this.setLocation(xPos, yPos);
xPos += OFFSET;
yPos += OFFSET;
//this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
// give positions and sizes for all widgets
class DemoNullLayout extends JPanel {
public DemoNullLayout() {
this.setBackground(Color.DARK_GRAY);
// clear the layout
this.setLayout(null);
// Add the components
JButton b1 = new JButton("Button1");
JButton b2 = new JButton("Button2");
b1.setBounds(10, 20, 150, 50); // x, y, width, height
b2.setBounds(40, 60, 100, 30);
this.add(b2);
this.add(b1);
}
}
class DemoFlowLayout extends JPanel {
public DemoFlowLayout() {
this.setBackground(Color.DARK_GRAY);
// note also FlowLayout(int alignment, int horizontalSpace, int verticalSpace);
this.setLayout(new FlowLayout());
// Add the components
this.add(new JButton(" One "));
this.add(new JTextField(" Two ", 10));
this.add(new JButton(" Three "));
this.add(new JButton(" Four "));
}
}
class DemoGridLayout extends JPanel {
public DemoGridLayout() {
this.setBackground(Color.DARK_GRAY);
// Set the layout strategy to a grid with 2 rows and 3 columns.
this.setLayout(new GridLayout(2, 3));
// Add the components
// (always added in left-to-right, top-to-bottom reading order )
this.add(new JButton("One"));
this.add(new JButton("Two"));
this.add(new JButton("Three"));
this.add(new JButton("Four"));
this.add(new JButton("Five"));
// this.add(new JButton("Six"));
}
}
class DemoBoxLayout1 extends JPanel {
// Default behaviour of BoxLayout
public DemoBoxLayout1() {
this.setBackground(Color.DARK_GRAY);
// use BoxLayout
// try BoxLayout.X_AXIS as well
this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
// Add the components
this.add(new JButton("One"));
this.add(new JButton("Two"));
this.add(new JTextArea("TextArea"));
this.add(new JButton("Three"));
}