📄 rowlayout.java
字号:
package ergo.ui;
// $Id: RowLayout.java,v 1.2 1999/08/13 01:20:10 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 row.
// If equalizeWidths is true then all the subcomponents are resized
// to have the width of the widest subcomponent (unless their
// constraint specifies not to).
// ---*** This was copied from ColumnLayout in a hurry. They should probably have
// there commonalities factored out into a superclass.
class RowLayout implements LayoutManager2 {
public static final int TOP = 1;
public static final int CENTER = 2;
public static final int BOTTOM = 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 = TOP;
private int defaultVSpace = 0;
private int margin = 1;
private boolean equalizeHeights = false;
private int maxHeight = 0; // 0 if not equalizing subcomponent heights.
public RowLayout (int alignDef, int vsDef, int marg, boolean equalize) {
if (alignDef == TOP || alignDef == CENTER || alignDef == BOTTOM)
defaultAlignment = alignDef;
defaultVSpace = vsDef;
margin = marg;
equalizeHeights = equalize;
}
public RowLayout () { }
class Constraint extends Object {
int alignment = defaultAlignment;
int spacing = defaultVSpace;
boolean fixedHeight = false;
Constraint () {}
Constraint (int align, int space, boolean fixed) {
alignment = align; spacing = space; fixedHeight = 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 (TOP, CENTER, BOTTOM) and whose 2nd
// element is a horizontal 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 right = csize.width - insets.right - margin;
// Determine the height of the highest component.
if (equalizeHeights)
for (int i = 0; i < specs.size(); i++) {
Vector spec = (Vector) specs.elementAt(i);
maxHeight = Math.max(maxHeight, getComponent(spec).getPreferredSize().height);
}
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 y = ytop;
int alignment = cons.alignment;
int spacing = cons.spacing;
Dimension prefSize = child.getPreferredSize();
int height = prefSize.height;
if (!cons.fixedHeight)
height = Math.max(maxHeight, height); // maybe widen it
if (alignment == CENTER)
y += (csize.height - ytop - insets.bottom - height - margin) / 2;
else if (alignment == BOTTOM)
y = csize.height - insets.bottom - margin - height;
if (spacing < 0) {
right -= prefSize.width + spacing; // spacing negative
child.setBounds(left, y, prefSize.width, height);
}
else {
child.setBounds(left, y, prefSize.width, height);
left += prefSize.width + 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 += d.width + cons.spacing;
size.height = Math.max(maxHeight, Math.max(size.height, d.height));
}
}
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 + -