⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 basicscrollpaneui.java

📁 java1.6众多例子参考
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
                if (baseline > 0) {                    return y + baseline;                }            }        }        return -1;    }    /**     * Returns an enum indicating how the baseline of the component     * changes as the size changes.     *     * @throws NullPointerException {@inheritDoc}     * @see javax.swing.JComponent#getBaseline(int, int)     * @since 1.6     */    public Component.BaselineResizeBehavior getBaselineResizeBehavior(            JComponent c) {        super.getBaselineResizeBehavior(c);        // Baseline is either from the header, in which case it's always        // the same size and therefor can be created as CONSTANT_ASCENT.        // If the header doesn't have a baseline than the baseline will only        // be valid if it's BaselineResizeBehavior is        // CONSTANT_ASCENT, so, return CONSTANT_ASCENT.        return Component.BaselineResizeBehavior.CONSTANT_ASCENT;    }    /**     * Listener for viewport events.     */    public class ViewportChangeHandler implements ChangeListener    {        // NOTE: This class exists only for backward compatability. All        // its functionality has been moved into Handler. If you need to add        // new functionality add it to the Handler, but make sure this              // class calls into the Handler.	public void stateChanged(ChangeEvent e) {            getHandler().stateChanged(e);	}    }    protected ChangeListener createViewportChangeListener() {	return getHandler();    }    /**     * Horizontal scrollbar listener.     */    public class HSBChangeListener implements ChangeListener    {        // NOTE: This class exists only for backward compatability. All        // its functionality has been moved into Handler. If you need to add        // new functionality add it to the Handler, but make sure this              // class calls into the Handler.	public void stateChanged(ChangeEvent e)	{            getHandler().stateChanged(e);	}    }    /**     * Returns a <code>PropertyChangeListener</code> that will be installed     * on the horizontal <code>JScrollBar</code>.     */    private PropertyChangeListener createHSBPropertyChangeListener() {        return getHandler();    }    protected ChangeListener createHSBChangeListener() {	return getHandler();    }    /**     * Vertical scrollbar listener.     */    public class VSBChangeListener implements ChangeListener    {        // NOTE: This class exists only for backward compatability. All        // its functionality has been moved into Handler. If you need to add        // new functionality add it to the Handler, but make sure this              // class calls into the Handler.	public void stateChanged(ChangeEvent e)	{            getHandler().stateChanged(e);	}    }    /**     * Returns a <code>PropertyChangeListener</code> that will be installed     * on the vertical <code>JScrollBar</code>.     */    private PropertyChangeListener createVSBPropertyChangeListener() {        return getHandler();    }    protected ChangeListener createVSBChangeListener() {	return getHandler();    }    /**     * MouseWheelHandler is an inner class which implements the      * MouseWheelListener interface.  MouseWheelHandler responds to      * MouseWheelEvents by scrolling the JScrollPane appropriately.     * If the scroll pane's     * <code>isWheelScrollingEnabled</code>     * method returns false, no scrolling occurs.     *      * @see javax.swing.JScrollPane#isWheelScrollingEnabled     * @see #createMouseWheelListener     * @see java.awt.event.MouseWheelListener     * @see java.awt.event.MouseWheelEvent     * @since 1.4     */    protected class MouseWheelHandler implements MouseWheelListener {        // NOTE: This class exists only for backward compatability. All        // its functionality has been moved into Handler. If you need to add        // new functionality add it to the Handler, but make sure this              // class calls into the Handler.        /**         * Called when the mouse wheel is rotated while over a         * JScrollPane.         *         * @param e     MouseWheelEvent to be handled         * @since 1.4         */        public void mouseWheelMoved(MouseWheelEvent e) {            getHandler().mouseWheelMoved(e);        }    }    /**     * Creates an instance of MouseWheelListener, which is added to the     * JScrollPane by installUI().  The returned MouseWheelListener is used     * to handle mouse wheel-driven scrolling.     *     * @return      MouseWheelListener which implements wheel-driven scrolling     * @see #installUI     * @see MouseWheelHandler     * @since 1.4     */    protected MouseWheelListener createMouseWheelListener() {        return getHandler();    }    protected void updateScrollBarDisplayPolicy(PropertyChangeEvent e) {	scrollpane.revalidate();	scrollpane.repaint();    }    protected void updateViewport(PropertyChangeEvent e)     {	JViewport oldViewport = (JViewport)(e.getOldValue());	JViewport newViewport = (JViewport)(e.getNewValue());	if (oldViewport != null) {	    oldViewport.removeChangeListener(viewportChangeListener);	}		if (newViewport != null) {	    Point p = newViewport.getViewPosition();	    if (scrollpane.getComponentOrientation().isLeftToRight()) {		p.x = Math.max(p.x, 0);	    } else {		int max = newViewport.getViewSize().width;		int extent = newViewport.getExtentSize().width;		if (extent > max) {		    p.x = max - extent;		} else {		    p.x = Math.max(0, Math.min(max - extent, p.x));		}	    }	    p.y = Math.max(p.y, 0);	    newViewport.setViewPosition(p);	    newViewport.addChangeListener(viewportChangeListener);	}    }    protected void updateRowHeader(PropertyChangeEvent e)     {	JViewport newRowHead = (JViewport)(e.getNewValue());	if (newRowHead != null) {	    JViewport viewport = scrollpane.getViewport();	    Point p = newRowHead.getViewPosition();	    p.y = (viewport != null) ? viewport.getViewPosition().y : 0;	    newRowHead.setViewPosition(p);	}    }    protected void updateColumnHeader(PropertyChangeEvent e)     {	JViewport newColHead = (JViewport)(e.getNewValue());	if (newColHead != null) {	    JViewport viewport = scrollpane.getViewport();	    Point p = newColHead.getViewPosition();	    if (viewport == null) {		p.x = 0;	    } else {		if (scrollpane.getComponentOrientation().isLeftToRight()) {		    p.x = viewport.getViewPosition().x;		} else {		    p.x = Math.max(0, viewport.getViewPosition().x);		}	    }	    newColHead.setViewPosition(p);	    scrollpane.add(newColHead, COLUMN_HEADER);	}    }    private void updateHorizontalScrollBar(PropertyChangeEvent pce) {        updateScrollBar(pce, hsbChangeListener, hsbPropertyChangeListener);    }    private void updateVerticalScrollBar(PropertyChangeEvent pce) {        updateScrollBar(pce, vsbChangeListener, vsbPropertyChangeListener);    }    private void updateScrollBar(PropertyChangeEvent pce, ChangeListener cl,                                 PropertyChangeListener pcl) {        JScrollBar sb = (JScrollBar)pce.getOldValue();        if (sb != null) {            if (cl != null) {                sb.getModel().removeChangeListener(cl);            }            if (pcl != null) {                sb.removePropertyChangeListener(pcl);            }        }        sb = (JScrollBar)pce.getNewValue();        if (sb != null) {            if (cl != null) {                sb.getModel().addChangeListener(cl);            }            if (pcl != null) {                sb.addPropertyChangeListener(pcl);            }        }    }    public class PropertyChangeHandler implements PropertyChangeListener    {        // NOTE: This class exists only for backward compatability. All        // its functionality has been moved into Handler. If you need to add        // new functionality add it to the Handler, but make sure this              // class calls into the Handler.        public void propertyChange(PropertyChangeEvent e)        {            getHandler().propertyChange(e);	}    }    /**     * Creates an instance of PropertyChangeListener that's added to      * the JScrollPane by installUI().  Subclasses can override this method     * to return a custom PropertyChangeListener, e.g.     * <pre>     * class MyScrollPaneUI extends BasicScrollPaneUI {     *    protected PropertyChangeListener <b>createPropertyChangeListener</b>() {     *        return new MyPropertyChangeListener();     *    }     *    public class MyPropertyChangeListener extends PropertyChangeListener {     *        public void propertyChange(PropertyChangeEvent e) {     *            if (e.getPropertyName().equals("viewport")) {     *                // do some extra work when the viewport changes     *            }     *            super.propertyChange(e);     *        }     *    }     * }     * </pre>     *      * @see java.beans.PropertyChangeListener     * @see #installUI     */    protected PropertyChangeListener createPropertyChangeListener() {        return getHandler();    }    private static class Actions extends UIAction {        private static final String SCROLL_UP = "scrollUp";        private static final String SCROLL_DOWN = "scrollDown";        private static final String SCROLL_HOME = "scrollHome";        private static final String SCROLL_END = "scrollEnd";        private static final String UNIT_SCROLL_UP = "unitScrollUp";        private static final String UNIT_SCROLL_DOWN = "unitScrollDown";        private static final String SCROLL_LEFT = "scrollLeft";        private static final String SCROLL_RIGHT = "scrollRight";        private static final String UNIT_SCROLL_LEFT = "unitScrollLeft";        private static final String UNIT_SCROLL_RIGHT = "unitScrollRight";        Actions(String key) {            super(key);        }        public void actionPerformed(ActionEvent e) {            JScrollPane scrollPane = (JScrollPane)e.getSource();            boolean ltr = scrollPane.getComponentOrientation().isLeftToRight();            String key = getName();            if (key == SCROLL_UP) {                scroll(scrollPane, SwingConstants.VERTICAL, -1, true);            }            else if (key == SCROLL_DOWN) {                scroll(scrollPane, SwingConstants.VERTICAL, 1, true);            }            else if (key == SCROLL_HOME) {                scrollHome(scrollPane);            }            else if (key == SCROLL_END) {                scrollEnd(scrollPane);            }            else if (key == UNIT_SCROLL_UP) {                scroll(scrollPane, SwingConstants.VERTICAL, -1, false);            }            else if (key == UNIT_SCROLL_DOWN) {                scroll(scrollPane, SwingConstants.VERTICAL, 1, false);            }            else if (key == SCROLL_LEFT) {                scroll(scrollPane, SwingConstants.HORIZONTAL, ltr ? -1 : 1,                       true);            }            else if (key == SCROLL_RIGHT) {                scroll(scrollPane, SwingConstants.HORIZONTAL, ltr ? 1 : -1,                       true);            }            else if (key == UNIT_SCROLL_LEFT) {                scroll(scrollPane, SwingConstants.HORIZONTAL, ltr ? -1 : 1,                       false);            }            else if (key == UNIT_SCROLL_RIGHT) {                scroll(scrollPane, SwingConstants.HORIZONTAL, ltr ? 1 : -1,                       false);            }        }        private void scrollEnd(JScrollPane scrollpane) {	    JViewport vp = scrollpane.getViewport();	    Component view;	    if (vp != null && (view = vp.getView()) != null) {		Rectangle visRect = vp.getViewRect();		Rectangle bounds = view.getBounds();		if (scrollpane.getComponentOrientation().isLeftToRight()) {		    vp.setViewPosition(new Point(bounds.width - visRect.width,					     bounds.height - visRect.height));		} else {		    vp.setViewPosition(new Point(0, 					     bounds.height - visRect.height));		}	    }        }        private void scrollHome(JScrollPane scrollpane) {	    JViewport vp = scrollpane.getViewport();	    Component view;	    if (vp != null && (view = vp.getView()) != null) {		if (scrollpane.getComponentOrientation().isLeftToRight()) {		    vp.setViewPosition(new Point(0, 0));		} else {		    Rectangle visRect = vp.getViewRect();		    Rectangle bounds = view.getBounds();		    vp.setViewPosition(new Point(bounds.width - visRect.width, 0));		}            }        }	private void scroll(JScrollPane scrollpane, int orientation,

⌨️ 快捷键说明

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