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

📄 tabbedpane.java

📁 j2me设计的界面包
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * Copyright 2008 Sun Microsystems, Inc.  All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation.  Sun designates this * particular file as subject to the "Classpath" exception as provided * by Sun in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, * CA 95054 USA or visit www.sun.com if you need additional information or * have any questions. */package com.sun.lwuit;import com.sun.lwuit.events.SelectionListener;import com.sun.lwuit.geom.Dimension;import com.sun.lwuit.geom.Rectangle;import com.sun.lwuit.layouts.BorderLayout;import com.sun.lwuit.list.DefaultListModel;import com.sun.lwuit.list.ListCellRenderer;import com.sun.lwuit.plaf.Style;import com.sun.lwuit.plaf.UIManager;import java.util.Hashtable;/** * A component that lets the user switch between a group of components by * clicking on a tab with a given title and/or icon. *  * <p> * Tabs/components are added to a <code>TabbedPane</code> object by using the * <code>addTab</code> and <code>insertTab</code> methods. * A tab is represented by an index corresponding * to the position it was added in, where the first tab has an index equal to 0 * and the last tab has an index equal to the tab count minus 1. * <p> * The <code>TabbedPane</code> uses a <code>SingleSelectionModel</code> * to represent the set of tab indices and the currently selected index.   * If the tab count is greater than 0, then there will always be a selected  * index, which by default will be initialized to the first tab.   * If the tab count is 0, then the selected index will be -1. * <p> *  * @author Tamir Shabat * */public class TabbedPane extends Container {    private Container contentPane = new Container(new BorderLayout());    private List tabsList = new List();    private Hashtable tabsTable = new Hashtable();    /**      * Where the tabs are placed.     */    private int tabPlacement = -1;    /**     * The TabbedPane surrounded border width (contentPane and tabs border)     */    private int tPBorder = 1;    /**     * Creates an empty <code>TabbedPane</code> with a default     * tab placement of <code>Component.TOP</code>.     */    public TabbedPane() {        this(TOP);    }    /**     * Creates an empty <code>TabbedPane</code> with the specified tab placement     * of either: <code>Component.TOP</code>, <code>Component.BOTTOM</code>,     * <code>Component.LEFT</code>, or <code>Component.RIGHT</code>.     *     * @param tabPlacement the placement for the tabs relative to the content     */    public TabbedPane(int tabPlacement) {        super(new BorderLayout());        contentPane.getStyle().setBgPainter(new Painter() {            public void paint(Graphics g, Rectangle rect) {                UIManager.getInstance().getLookAndFeel().                        drawTabbedPaneContentPane(TabbedPane.this, g, rect,                        tabsList.getPreferredSize(), tabsList.size(),                        tabsList.getSelectedIndex(), tabsList.getElementSize(true),                        tabsList.getScrollX(), tabsList.getScrollY());            }        });        super.addComponent(BorderLayout.CENTER, contentPane);                setTabPlacement(tabPlacement);        tabsList.getStyle().setPadding(0, 0, 0, 0);        tabsList.getStyle().setMargin(0, 0, 0, 0);        tabsList.getStyle().setBorder(null);        tabsList.setListCellRenderer(new TabsRenderer());        tabsList.setItemGap(0);        tabsList.setIsScrollVisible(false);        tabsList.setSmoothScrolling(false);        tabsList.setBorderGap(0);                tabsList.addSelectionListener(new SelectionListener() {            public void selectionChanged(int oldSelected, int newSelected) {                Component c = (Component) tabsList.getModel().getItemAt(newSelected);                contentPane.removeAll();                contentPane.addComponent("Center", (Component) tabsTable.get(c));                revalidate();            }        });                    }    /**     * @inheritDoc     */    public void setFocusable(boolean b) {        if(tabsList != null) {            tabsList.setFocusable(b);        }        super.setFocusable(b);    }        /**     * This method adds a listener to the tabs.     *      * @param listener a selection listener to gets the selection     * events     */    public void addTabsListener(SelectionListener listener){        tabsList.addSelectionListener(listener);    }        /**     * @inheritDoc     */    public void requestFocus() {        tabsList.requestFocus();    }        /**     * @inheritDoc     */    protected Dimension calcPreferredSize() {        int maxContentW = 0;        int maxContentH = 0;        int maxW = 0;        int maxH = 0;        for (int i = 0; i < tabsList.size(); i++) {            Component tabsComp = (Component) tabsList.getModel().getItemAt(i);            Component contentComp = (Component) tabsTable.get(tabsComp);            if (contentComp.getPreferredW() > maxContentW) {                maxContentW = contentComp.getPreferredW();            }            if (contentComp.getPreferredH() > maxContentH) {                maxContentH = contentComp.getPreferredH();            }        }        if (tabPlacement == TOP || tabPlacement == BOTTOM) {            maxW = maxContentW;            maxH = tabsList.getPreferredH() + maxContentH;        } else {            maxW = tabsList.getPreferredW() + maxContentW;            maxH = maxContentH;        }        return new Dimension(maxW, maxH);    }    /**     * Sets the tab placement for this tabbedpane.     * Possible values are:<ul>     * <li><code>Component.TOP</code>     * <li><code>Component.BOTTOM</code>     * <li><code>Component.LEFT</code>     * <li><code>Component.RIGHT</code>     * </ul>     * The default value, if not set, is <code>Component.TOP</code>.     *      * @param tabPlacement the placement for the tabs relative to the content     */    public void setTabPlacement(int tabPlacement) {        if (tabPlacement != TOP && tabPlacement != LEFT &&                tabPlacement != BOTTOM && tabPlacement != RIGHT) {            throw new IllegalArgumentException("illegal tab placement: must be TOP, BOTTOM, LEFT, or RIGHT");        }        if (this.tabPlacement == tabPlacement) {            return;        }        this.tabPlacement = tabPlacement;        removeComponent(tabsList);                if (tabPlacement == TOP || tabPlacement == BOTTOM) {            tabsList.setOrientation(List.HORIZONTAL);            if (tabPlacement == TOP) {                super.addComponent(BorderLayout.NORTH, tabsList);            } else if (tabPlacement == BOTTOM) {                super.addComponent(BorderLayout.SOUTH, tabsList);            }        } else {// LEFT Or RIGHT            tabsList.setOrientation(List.VERTICAL);            if (tabPlacement == LEFT) {                super.addComponent(BorderLayout.WEST, tabsList);            } else {// RIGHT                super.addComponent(BorderLayout.EAST, tabsList);            }        }        tabsList.setShouldCalcPreferredSize(true);        contentPane.setShouldCalcPreferredSize(true);        revalidate();    }    /**     * Adds a <code>component</code>      * represented by a <code>title</code> and/or <code>icon</code>,     * either of which can be <code>null</code>.     * Cover method for <code>insertTab</code>.     *      * @param title the title to be displayed in this tab     * @param icon the icon to be displayed in this tab     * @param component the component to be displayed when this tab is clicked     *      * @see #insertTab     * @see #removeTabAt     */    public void addTab(String title, Image icon, Component component) {        insertTab(title, icon, component, tabsList.size());    }    /**     * Adds a <code>component</code>      * represented by a <code>title</code> and no <code>icon</code>.     * Cover method for <code>insertTab</code>.     *      * @param title the title to be displayed in this tab     * @param component the component to be displayed when this tab is clicked     *      * @see #insertTab     * @see #removeTabAt     */    public void addTab(String title, Component component) {        insertTab(title, null, component, tabsList.size());    }    /**     * Inserts a <code>component</code>, at <code>index</code>,     * represented by a <code>title</code> and/or <code>icon</code>,

⌨️ 快捷键说明

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