centrallayout.java
字号:
package piy;
import java.awt.*;
/**
* Aligns a single component in a container so that it is centered vertically, but is
* stretched to fill all the available horizontal space. The component's preferred size
* is used to determine the height used.
*
* @author David Vivash
* @version 1.0.1, 22/04/01
*/
public class CentralLayout implements LayoutManager {
private Component toAlign = null;
/**
* Adds the specified component with the specified name to the layout.
* @param name the name of the component (NOT USED)
* @param comp the component to add
*/
public void addLayoutComponent(String name, Component comp) {
toAlign = comp;
}
/**
* Lays out the container in the specified panel.
* @param parent the container's parent
*/
public void layoutContainer(Container parent) {
if (toAlign != null) {
Dimension pSize = parent.getSize();
Dimension compSize = toAlign.getPreferredSize();
toAlign.setBounds(0,(int)(pSize.getHeight()/2-compSize.getHeight()/2), (int)pSize.getWidth(), (int)compSize.getHeight());
}
parent.repaint();
}
/**
* Calculates the minimum size dimensions for the specified panel given the components
* in the specified parent container. THIS IS NOT USED.
* @param parent the container's parent
* @return the size requirements of the container
*/
public Dimension minimumLayoutSize(Container parent) {
return (toAlign == null) ? new Dimension(0,0) : toAlign.getPreferredSize();
}
/**
* Calculates the preferred size dimensions for the specified panel given the components
* in the specified parent container. THIS IS NOT USED.
* @param parent the component's parent
* @return the size requirements of the container
*/
public Dimension preferredLayoutSize(Container parent) {
return (toAlign == null) ? new Dimension(0,0) : toAlign.getPreferredSize();
}
/**
* Removes the specified component from the layout.
* @param comp the component to remove
*/
public void removeLayoutComponent(Component comp) {
if (comp == toAlign) toAlign = null;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -