📄 tabbedpane.java
字号:
* either of which may be <code>null</code>. * Uses java.util.Vector internally, see <code>insertElementAt</code> * for details of insertion conventions. * * @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. * @param index the position to insert this new tab * * @see #addTab * @see #removeTabAt */ public void insertTab(String title, Image icon, Component component, int index) { checkIndex(index); if (component == null) { return; } Button b = new Button(title != null ? title : "", icon); ((DefaultListModel) tabsList.getModel()).addItemAtIndex(b, index); tabsTable.put(b, component); if (tabsList.size() == 1) { contentPane.addComponent("Center", component); } } /** * Updates the information about the tab details * * @param title the title to be displayed in this tab * @param icon the icon to be displayed in this tab * @param index the position to insert this new tab */ public void setTabTitle(String title, Image icon, int index) { checkIndex(index); Button b = (Button)tabsList.getModel().getItemAt(index); b.setText(title); b.setIcon(icon); ((DefaultListModel) tabsList.getModel()).setItem(index, b); } /** * Removes the tab at <code>index</code>. * After the component associated with <code>index</code> is removed, * its visibility is reset to true to ensure it will be visible * if added to other containers. * @param index the index of the tab to be removed * @exception IndexOutOfBoundsException if index is out of range * (index < 0 || index >= tab count) * * @see #addTab * @see #insertTab */ public void removeTabAt(int index) { checkIndex(index); Component removedComp = (Component) tabsTable.get(((Component) tabsList.getModel().getItemAt(index))); ((DefaultListModel) tabsList.getModel()).removeItem(index); tabsTable.remove(removedComp); } /** * Returns tthe tab at <code>index</code>. * * @param index the index of the tab to be removed * @exception IndexOutOfBoundsException if index is out of range * (index < 0 || index >= tab count) * * @see #addTab * @see #insertTab */ public Component getTabComponentAt(int index) { checkIndex(index); return (Component) tabsTable.get(((Component) tabsList.getModel().getItemAt(index))); } private void checkIndex(int index) { if (index < 0 || index > tabsList.size()) { throw new IndexOutOfBoundsException("Index: " + index); } } /** * Returns the index of the tab for the specified component. * Returns -1 if there is no tab for this component. * * @param component the component for the tab * @return the first tab which matches this component, or -1 * if there is no tab for this component */ public int indexOfComponent(Component component) { for (int i = 0; i < getTabCount(); i++) { Component c = (Component) tabsList.getModel().getItemAt(i); Component content = (Component) tabsTable.get(c); if(component.equals(content)){ return i; } } return -1; } /** * Returns the number of tabs in this <code>tabbedpane</code>. * * @return an integer specifying the number of tabbed pages */ public int getTabCount() { return tabsList.size(); } /** * Returns the currently selected index for this tabbedpane. * Returns -1 if there is no currently selected tab. * * @return the index of the selected tab */ public int getSelectedIndex() { return tabsList.getSelectedIndex(); } /** * @inheritDoc */ protected String getUIID() { return "TabbedPane"; } /** * The prototype is optionally used in calculating the size of an individual * tab and is recommended for performance reasons. You should invoke it with a String * representing the width/height which will be used to calculate * the size required for each element in the list.\ * <p>This operation is not essential and if it isn't invoked the size of the first * few tabs is used to determin the overall size of a tab. * <p>This allows the size calculations to work across look and feels and allows * developers to predetermin size for the tabs. * <p>e.g. For tabs which you would like to always be 5 characters wide * you can use a prototype "XXXXX" which would use the preferred size of the XXXXX * String to determin the size of the tabs.. * * @param title a string to determine the size. */ public void setTabTitlePrototype(String title) { tabsList.setRenderingPrototype(title); } /** * @inheritDoc */ public String toString() { String className = getClass().getName(); className = className.substring(className.lastIndexOf('.') + 1); return className + "[x=" + getX() + " y=" + getY() + " width=" + getWidth() + " height=" + getHeight() + ", tab placement = " + tabPlacement + ", tab count = " + getTabCount() + ", selected index = " + getSelectedIndex() + "]"; } /** * @inheritDoc */ public void paint(Graphics g) { super.paint(g); UIManager.getInstance().getLookAndFeel().drawTabbedPane(g, this); } /** * @inheritDoc */ public void setStyle(Style style) { tabsList.setStyle(style); contentPane.setStyle(style); } /** * Returns the placement of the tabs for this tabbedpane. * * @see #setTabPlacement */ public int getTabPlacement() { return tabPlacement; } /** * The TabbedPane surrounded border width * * @return The TabbedPane surrounded border width */ public int getTabbedPaneBorderWidth() { return tPBorder; } /** * Setting the TabbedPane surrounded border width * * @param tabbedPaneBorderWidth TabbedPane surrounded border width */ public void setTabbedPaneBorderWidth(int tabbedPaneBorderWidth) { this.tPBorder = tabbedPaneBorderWidth; } /** * @inheritDoc */ public void setPadding(int top, int bottom, int left, int right) { if (contentPane != null) { contentPane.getStyle().setPadding(top, bottom, left, right); } } /** * @inheritDoc */ public void setPadding(int orientation, int gap) { if (contentPane != null) { contentPane.getStyle().setPadding(orientation, gap); } } /** * Sets the selected index for this tabbedpane. The index must be a valid * tab index. * @param index the index to be selected * @throws IndexOutOfBoundsException if index is out of range * (index < 0 || index >= tab count) */ public void setSelectedIndex(int index) { if (index < 0 || index >= tabsList.size()) { throw new IndexOutOfBoundsException("Index: "+index+", Tab count: "+tabsList.size()); } tabsList.setSelectedIndex(index); } class TabsRenderer implements ListCellRenderer { public Component getListCellRendererComponent(List list, Object value, int index, boolean isSelected) { // prototype value can cause this if(value == null || (!(value instanceof Button))) { value = new Button("" + value); } return UIManager.getInstance().getLookAndFeel().getTabbedPaneCell( TabbedPane.this, ((Button) value).getText(), ((Button) value).getIcon(), isSelected, list.hasFocus(), list.getStyle(), TabbedPane.this.getStyle(), list.getScrollX(), list.getScrollY(), list.getPreferredSize(), contentPane.getBounds().getSize()); } public Component getListFocusComponent(List list) { return null; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -