📄 columnlayout.java
字号:
package ergo.ui;
// $Id: ColumnLayout.java,v 1.2 1999/08/13 01:20:07 sigue Exp $
/*
* Copyright (C) 1999 Carl L. Gay and Antranig M. Basman.
* See the file copyright.txt, distributed with this software,
* for further information.
*/
import ergo.util.*;
import java.awt.*;
import java.util.Vector;
// A layout manager that will put components in a single column.
// If equalizeWidths is true then all the subcomponents are resized
// to have the width of the widest subcomponent (unless their
// constraint specifies not to).
class ColumnLayout implements LayoutManager2 {
public static final int LEFT = 1;
public static final int CENTER = 2;
public static final int RIGHT = 3;
// A Vector of specs, one for each component in the layout.
// Each element is a Vector where
// spec.elementAt(0) = the component, and
// spec.elementAt(1) = the constraint
private Vector specs = new Vector(5, 10);
private int defaultAlignment = LEFT;
private int defaultVSpace = 0;
private int margin = 1;
private boolean equalizeWidths = false;
private int maxWidth = 0; // 0 if not equalizing subcomponent widths.
public ColumnLayout (int alignDef, int vsDef, int marg, boolean equalize) {
if (alignDef == LEFT || alignDef == CENTER || alignDef == RIGHT)
defaultAlignment = alignDef;
defaultVSpace = vsDef;
margin = marg;
equalizeWidths = equalize;
}
public ColumnLayout () { }
class Constraint extends Object {
int alignment = defaultAlignment;
int spacing = defaultVSpace;
boolean fixedWidth = false;
Constraint () {}
Constraint (int align, int space, boolean fixed) {
alignment = align; spacing = space; fixedWidth = fixed;
}
}
// Convenience function for callers to make a layout constraint
public Constraint makeConstraint (int align, int space, boolean fixed) {
return new Constraint(align, space, fixed);
}
private Component getComponent (Vector spec) {
return (Component) spec.elementAt(0);
}
private Constraint getConstraint (Vector spec) {
return (Constraint) spec.elementAt(1);
}
// Add a component to this layout. A constraint is a Vector whose
// 1st element is an alignment (LEFT, CENTER, RIGHT) and whose 2nd
// element is a vertical spacing spec. If the latter is negative
// it means "from below". i.e., -1 means 1 pixel above the bottom.
public void addLayoutComponent (Component child, Object constraint) {
removeLayoutComponent(child); // don't add it twice.
Constraint c;
if (!(constraint instanceof Constraint))
c = new Constraint();
else
c = (Constraint) constraint;
Vector spec = new Vector(2);
spec.addElement(child);
spec.addElement(c);
specs.addElement(spec);
}
public void removeLayoutComponent (Component comp) {
for (int i = 0; i < specs.size(); i++) {
Vector spec = (Vector) specs.elementAt(i);
if (spec.elementAt(0) == comp) {
specs.removeElementAt(i);
return;
}
}
}
// Methods we don't implement.
public float getLayoutAlignmentX (Container c) { return 0.5f; }
public float getLayoutAlignmentY (Container c) { return 0.5f; }
public void addLayoutComponent (String s, Component c) {}
public void invalidateLayout (Container c) {}
public void layoutContainer (Container c) {
Dimension csize = c.getSize();
Insets insets = c.getInsets();
int left = insets.left + margin;
int ytop = insets.top + margin;
int ybot = csize.height - insets.bottom - margin;
// Determine the width of the widest component.
if (equalizeWidths)
for (int i = 0; i < specs.size(); i++) {
Vector spec = (Vector) specs.elementAt(i);
maxWidth = Math.max(maxWidth, getComponent(spec).getPreferredSize().width);
}
for(int i = 0; i < specs.size(); i++) {
Vector spec = (Vector) specs.elementAt(i);
Component child = getComponent(spec);
Constraint cons = getConstraint(spec);
if (child.isVisible()) {
int x = left;
int alignment = cons.alignment;
int spacing = cons.spacing;
Dimension prefSize = child.getPreferredSize();
int width = prefSize.width;
if (!cons.fixedWidth)
width = Math.max(maxWidth, width); // maybe widen it
if (alignment == CENTER)
x += (csize.width - left - insets.right - width - margin) / 2;
else if (alignment == RIGHT)
x = csize.width - insets.right - margin - width;
if (spacing < 0) {
ybot -= prefSize.height + spacing; // spacing negative
child.setBounds(x, ybot, width, prefSize.height);
}
else {
child.setBounds(x, ytop, width, prefSize.height);
ytop += prefSize.height + spacing;
}
}
}
}
public Dimension minimumLayoutSize (Container c) {
return getLayoutSize(c, "min");
}
public Dimension preferredLayoutSize (Container c) {
return getLayoutSize(c, "pref");
}
public Dimension maximumLayoutSize (Container c) {
return getLayoutSize(c, "max");
}
private Dimension getLayoutSize (Container c, String kind) {
Dimension size = new Dimension(0,0);
Insets insets = c.getInsets();
for(int i = 0; i < specs.size(); i++) {
Vector spec = (Vector) specs.elementAt(i);
Component child = getComponent(spec);
Constraint cons = getConstraint(spec);
if (child.isVisible()) {
Dimension d;
if ("pref".equals(kind))
d = child.getPreferredSize();
else if ("min".equals(kind))
d = child.getMinimumSize();
else
d = child.getMaximumSize();
size.width = Math.max(maxWidth, Math.max(size.width, d.width));
size.height += d.height + cons.spacing;
}
}
size.width += insets.left + insets.right + margin * 2;
size.height += insets.top + insets.bottom + margin * 2;
return size;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -