toolbarlayout.java

来自「Java生成PDF Java生成PDF Java生成PDF」· Java 代码 · 共 111 行

JAVA
111
字号
// $Id: ToolBarLayout.java,v 1.1 2007/08/03 16:45:09 mike Exp $package org.faceless.pdf2.viewer2;import java.awt.*;import javax.swing.JToolBar;/** * Simple class to position toolbar elements so they wrap onto the next line */class ToolBarLayout implements LayoutManager{    /**     * Adds the specified component to the layout.  Sets the orientation to be horizontal.     * @param name the name of the component     * @param comp the component to be added     */    public void addLayoutComponent(String name, Component comp) {        if (comp instanceof JToolBar) {            ((JToolBar)comp).setOrientation(JToolBar.HORIZONTAL);        }    }    public void removeLayoutComponent(Component comp) {    }    public Dimension preferredLayoutSize(Container c) {        synchronized (c.getTreeLock()) {            Dimension dim = new Dimension(0, 0);            Insets insets = c.getInsets();            int numrows	= 1;            int rowwidth = insets.left + insets.right;            int rowheight = 0;            boolean first = true;                         for (int i=0;i<c.getComponentCount();i++) {                Component m = c.getComponent(i);                if (m.isVisible()) {                    Dimension d = m.getPreferredSize();                    if (d.height > rowheight) rowheight = d.height;                    rowwidth += d.width;                    if (rowwidth > c.getSize().width && !first) {                        numrows++;                        rowwidth = insets.left + insets.right + d.width;                        first = true;                    } else {                        first = false;                    }                }            }            dim.width = c.getSize().width;            dim.height = insets.top + insets.bottom + (numrows*rowheight);            return dim;        }    }    public Dimension minimumLayoutSize(Container c) {        synchronized (c.getTreeLock()) {            Dimension dim = new Dimension(0, 0);            for (int i=0;i<c.getComponentCount();i++) {                Component m = c.getComponent(i);                if (m.isVisible()) {                    Dimension d = m.getMinimumSize();                    if (d.height > dim.height) dim.height = d.height;                    dim.width += d.width;                }            }            Insets insets = c.getInsets();            dim.width += insets.left + insets.right;            dim.height += insets.top + insets.bottom;            return dim;        }    }    private void setPositions(Container c, int start, int end, int x, int y, int h) {        for (int i=start;i<end;i++) {            Component m = c.getComponent(i);            if (m.isVisible()) {                m.setLocation(x, y + (h - m.getSize().height) / 2);                x += m.getSize().width;            }        }    }    public void layoutContainer(Container c) {        synchronized (c.getTreeLock()) {            Insets insets = c.getInsets();            int maxwidth = c.getSize().width - insets.left - insets.right;            int rowh = 0, start = 0, x = 0, y = insets.top;            for (int i=0;i<c.getComponentCount();i++) {                Component m = c.getComponent(i);                if (m.isVisible()) {                    Dimension d = m.getPreferredSize();                    m.setSize(d.width, d.height);                    if (x == 0 || x+d.width <= maxwidth) {                        x += d.width;                        if (d.height > rowh) rowh = d.height;                    } else {                        setPositions(c, start, i, insets.left, y, rowh);                        x = d.width;                        y += rowh;                        rowh = d.height;                        start = i;                    }                }            }            setPositions(c, start, c.getComponentCount(), insets.left, y, rowh);        }    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?