📄 vsplitlayout.java
字号:
package edu.odu.cs.zeil.AlgAE.gui;import edu.odu.cs.zeil.AlgAE.Debug;import java.awt.Component;import java.awt.Container;import java.awt.Dimension;import java.awt.Insets;import java.awt.LayoutManager;import edu.odu.cs.zeil.AlgAE.gui.SplitLayout;public class VSplitLayout implements SplitLayout { private double topFraction = 0.5; private int topSize = -1; private int bottomSize = -1; private Dimension minSize; private Dimension preferredSize; private Component top; private Component center; private Component bottom; public VSplitLayout(double topFract) { topFraction = topFract; topSize = -1; minSize = new Dimension(); preferredSize = null; top = center = bottom = null; } public void setTopFraction (double fraction) { topFraction = fraction; topSize = -1; } public void alterSize (Dimension change) { int topH = top.getSize().height; int botH = bottom.getSize().height; int h = Math.max(-topH, Math.min(change.height, botH)); topSize = topH + h; bottomSize = botH - h; topFraction = -1.0; top.invalidate(); bottom.invalidate(); top.getParent().validate(); } /* Required by LayoutManager. */ public void addLayoutComponent(String name, Component comp) { } /* Required by LayoutManager. */ public void removeLayoutComponent(Component comp) { } private void setSizes(Container target) { if ((preferredSize == null) || (!target.isValid())) { top = target.getComponent(0); center = target.getComponent(1); bottom = target.getComponent(2); if (preferredSize == null) preferredSize = new Dimension(); Dimension dtop = new Dimension(); Dimension dcenter = new Dimension(); Dimension dbottom = new Dimension(); // Compute minimum size dtop = top.getMinimumSize(); dcenter = center.getMinimumSize(); dbottom = top.getMinimumSize(); minSize.width = Math.max (dtop.width, dbottom.width); minSize.height = dtop.height + dcenter.height + dbottom.height; // Compute preferred size dtop = top.getPreferredSize(); dbottom = top.getPreferredSize(); preferredSize.width = Math.max (dtop.width, dbottom.width); if (topSize < 0) { topFraction = (topFraction < 0.01)? 0.01 : topFraction; topFraction = (topFraction > 0.99)? 0.99 : topFraction; preferredSize.height = dcenter.height + (int) Math.max (dtop.height / topFraction, dbottom.height / (1.0 - topFraction)); } else { preferredSize.height = topSize + dcenter.height + bottomSize; } Debug.show (Debug.splitters, "VSL preferredSize ", preferredSize); Debug.show (Debug.splitters, "VSL minSize ", minSize); } } /* Required by LayoutManager. */ public Dimension preferredLayoutSize(Container target) { setSizes(target); Insets insets = target.getInsets(); Dimension dim = new Dimension (preferredSize.width + insets.left + insets.right, preferredSize.height + insets.top + insets.bottom); return dim; } /* Required by LayoutManager. */ public Dimension minimumLayoutSize(Container target) { setSizes(target); Insets insets = target.getInsets(); Dimension dim = new Dimension (minSize.width + insets.left + insets.right, minSize.height + insets.top + insets.bottom); return dim; } private void sanityCheck (Dimension proposedSize, Component c) { Dimension minSize = c.getMinimumSize(); Dimension maxSize = c.getMaximumSize(); proposedSize.width = Math.max(minSize.width, Math.min(maxSize.width, proposedSize.width)); proposedSize.height = Math.max(minSize.height, Math.min(maxSize.height, proposedSize.height)); } /* Required by LayoutManager. */ /* This is called when the panel is first displayed, * and every time its size changes. * Note: You CAN'T assume preferredLayoutSize() or minimumLayoutSize() * will be called -- in the case of applets, at least, they probably * won't be. */ public void layoutContainer(Container target) { synchronized (this) { Insets insets = target.getInsets(); Dimension targetSize = target.getSize(); Dimension available = new Dimension (targetSize.width - (insets.left + insets.right), targetSize.height - (insets.top + insets.bottom)); setSizes(target); Dimension dtop = new Dimension(); Dimension dbottom = new Dimension(); Dimension dcenter = center.getMinimumSize(); int x = insets.left; int y = insets.top; int w = available.width; int h = 0; if (topSize < 0) { Debug.show (Debug.splitters, "VSL.layoutContainer topFraction ", topFraction); h = (int)(topFraction * (available.height - dcenter.height)); } else { Debug.show (Debug.splitters, "VSL.layoutContainer topSize ", topSize); h = topSize; } top.setSize (w, h); top.getPreferredSize(); top.setBounds (x, y, w, h); Debug.show (Debug.splitters, "VSL.top y ", y); Debug.show (Debug.splitters, "VSL.top h ", h); y += h; center.setSize (w, dcenter.height); center.getPreferredSize(); center.setBounds (x, y, w, dcenter.height); y += dcenter.height; h = bottomSize = available.height - y; bottom.setSize (w, h); bottom.getPreferredSize(); bottom.setBounds (x, y, w, h); Debug.show (Debug.splitters, "VSL.bottom y ", y); Debug.show (Debug.splitters, "VSL.bottom h ", h); } /* top.validate(); center.validate(); bottom.validate(); */ } public String toString() { return getClass().getName() + "[topFraction=" + topFraction + "]"; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -