📄 springlayout.java
字号:
{ y = s; if ((height != null) && (south != null)) height = Spring.sum(south, Spring.minus(y)); } } /** * Creates a new SpringLayout. */ public SpringLayout() { constraintsMap = new HashMap(); } /** * Adds a layout component and a constraint object to this layout. * This method is usually only called by a {@java.awt.Container}s add * Method. * * @param component the component to be added. * @param constraint the constraint to be set. */ public void addLayoutComponent(Component component, Object constraint) { constraintsMap.put(component, constraint); } /** * Adds a layout component and a constraint object to this layout. * This method is usually only called by a {@java.awt.Container}s add * Method. This method does nothing, since SpringLayout does not manage * String-indexed components. * * @param name the name. * @param c the component to be added. */ public void addLayoutComponent(String name, Component c) { // do nothing here. } /** * Returns the constraint of the edge named by <code>edgeName</code>. * * @param c the component from which to get the constraint. * @param edgeName the name of the edge, one of {@link #EAST}, * {@link #WEST}, {@link #NORTH} or {@link #SOUTH}. * @return the constraint of the edge <code>edgeName</code> of the * component c. */ public Spring getConstraint(String edgeName, Component c) { Constraints constraints = getConstraints(c); return constraints.getConstraint(edgeName); } /** * Returns the {@link Constraints} object associated with the specified * component. * * @param c the component for which to determine the constraint. * @return the {@link Constraints} object associated with the specified * component. */ public SpringLayout.Constraints getConstraints(Component c) { Constraints constraints = (Constraints) constraintsMap.get(c); if (constraints == null) { Container parent = c.getParent(); constraints = new Constraints(); if (parent != null) { constraints.setX(Spring.constant(parent.getInsets().left)); constraints.setY(Spring.constant(parent.getInsets().top)); } else { constraints.setX(Spring.constant(0)); constraints.setY(Spring.constant(0)); } } constraints.setWidth(Spring.constant(c.getMinimumSize().width, c.getPreferredSize().width, c.getMaximumSize().width)); constraints.setHeight(Spring.constant(c.getMinimumSize().height, c.getPreferredSize().height, c.getMaximumSize().height)); constraintsMap.put(c, constraints); return constraints; } /** * Returns the X alignment of the Container <code>p</code>. * * @param p * the {@link java.awt.Container} for which to determine the X * alignment. * @return always 0.0 */ public float getLayoutAlignmentX(Container p) { return 0.0F; } /** * Returns the Y alignment of the Container <code>p</code>. * * @param p the {@link java.awt.Container} for which to determine the Y * alignment. * @return always 0.0 */ public float getLayoutAlignmentY(Container p) { return 0.0F; } /** * Recalculate a possibly cached layout. */ public void invalidateLayout(Container p) { // nothing to do here yet } /** * Lays out the container <code>p</code>. * * @param p the container to be laid out. */ public void layoutContainer(Container p) { addLayoutComponent(p, new Constraints(Spring.constant(0), Spring.constant(0))); int offsetX = p.getInsets().left; int offsetY = p.getInsets().right; Component[] components = p.getComponents(); for (int index = 0; index < components.length; index++) { Component c = components[index]; Constraints constraints = getConstraints(c); int x = constraints.getX().getValue(); int y = constraints.getY().getValue(); int width = constraints.getWidth().getValue(); int height = constraints.getHeight().getValue(); c.setLocation(x + offsetX, y + offsetY); c.setSize(width, height); } } /** * Calculates the maximum size of the layed out container. This * respects the maximum sizes of all contained components. * * @param p the container to be laid out. * @return the maximum size of the container. */ public Dimension maximumLayoutSize(Container p) { int maxX = 0; int maxY = 0; int offsetX = p.getInsets().left; int offsetY = p.getInsets().right; Component[] components = p.getComponents(); for (int index = 0; index < components.length; index++) { Component c = components[index]; Constraints constraints = getConstraints(c); int x = constraints.getX().getMaximumValue(); int y = constraints.getY().getMaximumValue(); int width = constraints.getWidth().getMaximumValue(); int height = constraints.getHeight().getMaximumValue(); int rightEdge = offsetX + x + width; if (rightEdge > maxX) maxX = rightEdge; int bottomEdge = offsetY + y + height; if (bottomEdge > maxY) maxY = bottomEdge; } return new Dimension(maxX, maxY); } /** * Calculates the minimum size of the layed out container. This * respects the minimum sizes of all contained components. * * @param p the container to be laid out. * @return the minimum size of the container. */ public Dimension minimumLayoutSize(Container p) { int maxX = 0; int maxY = 0; int offsetX = p.getInsets().left; int offsetY = p.getInsets().right; Component[] components = p.getComponents(); for (int index = 0; index < components.length; index++) { Component c = components[index]; Constraints constraints = getConstraints(c); int x = constraints.getX().getMinimumValue(); int y = constraints.getY().getMinimumValue(); int width = constraints.getWidth().getMinimumValue(); int height = constraints.getHeight().getMinimumValue(); int rightEdge = offsetX + x + width; if (rightEdge > maxX) maxX = rightEdge; int bottomEdge = offsetY + y + height; if (bottomEdge > maxY) maxY = bottomEdge; } return new Dimension(maxX, maxY); } /** * Calculates the preferred size of the layed out container. This * respects the preferred sizes of all contained components. * * @param p the container to be laid out. * @return the preferred size of the container. */ public Dimension preferredLayoutSize(Container p) { int maxX = 0; int maxY = 0; int offsetX = p.getInsets().left; int offsetY = p.getInsets().right; Component[] components = p.getComponents(); for (int index = 0; index < components.length; index++) { Component c = components[index]; Constraints constraints = getConstraints(c); int x = constraints.getX().getPreferredValue(); int y = constraints.getY().getPreferredValue(); int width = constraints.getWidth().getPreferredValue(); int height = constraints.getHeight().getPreferredValue(); int rightEdge = offsetX + x + width; if (rightEdge > maxX) maxX = rightEdge; int bottomEdge = offsetY + y + height; if (bottomEdge > maxY) maxY = bottomEdge; } return new Dimension(maxX, maxY); } /** * Attaches the edge <code>e1</code> of component <code>c1</code> to * the edge <code>e2</code> of component <code>c2</code> width the * fixed strut <code>pad</code>. * * @param e1 the edge of component 1. * @param c1 the component 1. * @param pad the space between the components in pixels. * @param e2 the edge of component 2. * @param c2 the component 2. */ public void putConstraint(String e1, Component c1, int pad, String e2, Component c2) { Constraints constraints1 = getConstraints(c1); Constraints constraints2 = getConstraints(c2); Spring strut = Spring.constant(pad); Spring otherEdge = constraints2.getConstraint(e2); constraints1.setConstraint(e1, Spring.sum(strut, otherEdge)); } /** * Attaches the edge <code>e1</code> of component <code>c1</code> to * the edge <code>e2</code> of component <code>c2</code> width the * {@link Spring} <code>s</code>. * * @param e1 the edge of component 1. * @param c1 the component 1. * @param s the space between the components as a {@link Spring} object. * @param e2 the edge of component 2. * @param c2 the component 2. */ public void putConstraint(String e1, Component c1, Spring s, String e2, Component c2) { Constraints constraints1 = getConstraints(c1); Constraints constraints2 = getConstraints(c2); Spring otherEdge = constraints2.getConstraint(e2); constraints1.setConstraint(e1, Spring.sum(s, otherEdge)); } /** * Removes a layout component. * @param c the layout component to remove. */ public void removeLayoutComponent(Component c) { // do nothing here }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -