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

📄 singlerowtabbedpaneui.java

📁 Swing Examples Sources Codes 里包含了Java Web开发的一些实例代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* (swing1.1.1beta2) */package jp.gr.java_conf.tame.swing.panel;import javax.swing.*;import java.awt.*;import java.awt.event.*;import javax.swing.plaf.basic.BasicTabbedPaneUI;import javax.swing.plaf.metal.MetalTabbedPaneUI;  /** * @version 1.0 07/09/99 */public class SingleRowTabbedPaneUI extends MetalTabbedPaneUI {  protected ActionListener[]  buttonListeners;    public void installUI(JComponent c) {    this.tabPane = (JTabbedPane)c;    c.setLayout(createLayoutManager());    installDefaults();     installComponents();    installListeners();    installKeyboardActions();            runCount    = 1;    selectedRun = 0;     }      public void uninstallUI(JComponent c) {    uninstallComponents();    super.uninstallUI(c);  }    protected LayoutManager createLayoutManager() {    return new SingleRowTabbedLayout(tabPane);  }      protected void installComponents() {    JButton[] buttons = ((SingleRowTabbedPane)tabPane).getButtons();    for (int i=0;i<buttons.length;i++) {      tabPane.add(buttons[i]);    }  }  protected void uninstallComponents() {    JButton[] buttons = ((SingleRowTabbedPane)tabPane).getButtons();    for (int i=0;i<buttons.length;i++) {      tabPane.remove(buttons[i]);    }  }          protected void installListeners() {    super.installListeners();    SingleRowTabbedPane stabPane = (SingleRowTabbedPane)tabPane;    JButton[] buttons = stabPane.getButtons();    int n = buttons.length;    buttonListeners = new ActionListener[n];        for (int i=0;i<n;i++) {      buttonListeners[i] = null;      String str = buttons[i].getActionCommand();            if (str.equals(SingleRowTabbedPane.ROTATE)) {        buttonListeners[i] = new ShiftTabs() {          protected int getStartIndex() {            int index = sPane.getVisibleStartIndex() + sPane.getVisibleCount();                        return (index < sPane.getTabCount())? index: 0;          }        };      }      else if (str.equals(SingleRowTabbedPane.PREVIOUS)) {        buttonListeners[i] = new ShiftTabs() {          protected int getStartIndex() {            return getStartIndex(sPane.getVisibleStartIndex() - 1);          }        };      }      else if (str.equals(SingleRowTabbedPane.NEXT)) {        buttonListeners[i] = new ShiftTabs() {          protected int getStartIndex() {            return sPane.getVisibleStartIndex() + sPane.getVisibleCount();          }        };      }      else if (str.equals(SingleRowTabbedPane.FIRST)) {        buttonListeners[i] = new ShiftTabs();      }      else if (str.equals(SingleRowTabbedPane.LEFT_SHIFT)) {        buttonListeners[i] = new ShiftTabs() {          protected int getStartIndex() {            return sPane.getVisibleStartIndex() - 1;          }        };      }      else if (str.equals(SingleRowTabbedPane.RIGHT_SHIFT)) {        buttonListeners[i] = new ShiftTabs() {          protected int getStartIndex() {            return sPane.getVisibleStartIndex() + 1;          }        };      }      else if (str.equals(SingleRowTabbedPane.LAST)) {        buttonListeners[i] = new ShiftTabs() {          protected int getStartIndex() {            return getStartIndex(sPane.getTabCount() - 1);          }        };      }             buttons[i].addActionListener(buttonListeners[i]);    }  }          protected void uninstallListeners() {    super.uninstallListeners();    JButton[] buttons = ((SingleRowTabbedPane)tabPane).getButtons();    for (int i=0;i<buttons.length;i++) {      buttons[i].removeActionListener(buttonListeners[i]);    }  }    public int tabForCoordinate(JTabbedPane pane, int x, int y) {    int tabCount = tabPane.getTabCount();    SingleRowTabbedPane stabPane = (SingleRowTabbedPane)tabPane;    int visibleCount      = stabPane.getVisibleCount();    int visibleStartIndex = stabPane.getVisibleStartIndex();    for (int i=0,index = visibleStartIndex; i < visibleCount; i++,index++) {      if (rects[index].contains(x, y)) {        return index;      }    }    return -1;  }    public void paint(Graphics g, JComponent c) {    int selectedIndex = tabPane.getSelectedIndex();    int tabPlacement = tabPane.getTabPlacement();    int tabCount = tabPane.getTabCount();            ensureCurrentLayout();    SingleRowTabbedPane stabPane = (SingleRowTabbedPane)tabPane;    int visibleCount      = stabPane.getVisibleCount();    int visibleStartIndex = stabPane.getVisibleStartIndex();        Rectangle iconRect = new Rectangle(),              textRect = new Rectangle();    Rectangle clipRect = g.getClipBounds();      Insets insets = tabPane.getInsets();        tabRuns[0] = visibleStartIndex;    for (int i=0,index=visibleStartIndex; i<visibleCount; i++,index++) {                if (rects[index].intersects(clipRect)) {        paintTab(g, tabPlacement, rects, index, iconRect, textRect);      }    }    if (stabPane.isVisibleTab(selectedIndex)) {      if (rects[selectedIndex].intersects(clipRect)) {        paintTab(g, tabPlacement, rects, selectedIndex, iconRect, textRect);      }    }    paintContentBorder(g, tabPlacement, selectedIndex);  }      protected void paintContentBorderTopEdge( Graphics g,        int tabPlacement, int selectedIndex, int x, int y, int w, int h ) {        g.setColor(selectHighlight);    if (tabPlacement != TOP || selectedIndex < 0 ||         (rects[selectedIndex].y + rects[selectedIndex].height + 1 < y) ||        !((SingleRowTabbedPane)tabPane).isVisibleTab(selectedIndex) ) {      g.drawLine(x, y, x+w-2, y);    } else {      Rectangle selRect = rects[selectedIndex];      g.drawLine(x, y, selRect.x + 1, y);      if (selRect.x + selRect.width < x + w - 2) {        g.drawLine(selRect.x + selRect.width, y, x+w-2, y);      } else {	g.setColor(shadow);         g.drawLine(x+w-2, y, x+w-2, y);      }    }  }  protected void paintContentBorderBottomEdge(Graphics g,        int tabPlacement, int selectedIndex, int x, int y, int w, int h) {     g.setColor(darkShadow);    if (tabPlacement != BOTTOM || selectedIndex < 0 ||        (rects[selectedIndex].y - 1 > h) ||        !((SingleRowTabbedPane)tabPane).isVisibleTab(selectedIndex) ) {      g.drawLine(x, y+h-1, x+w-1, y+h-1);    } else {      Rectangle selRect = rects[selectedIndex];      g.drawLine(x, y+h-1, selRect.x, y+h-1);      if (selRect.x + selRect.width < x + w - 2) {        g.drawLine(selRect.x + selRect.width, y+h-1, x+w-1, y+h-1);      }     }  }          protected Insets getTabAreaInsets(int tabPlacement) {    SingleRowTabbedPane stabPane = (SingleRowTabbedPane)tabPane;    Dimension d = stabPane.getButtonPreferredSize();    int n = stabPane.getButtonCount();          int buttonPlacement = stabPane.getButtonPlacement();          Insets currentInsets = new Insets(0,0,0,0);    if (tabPlacement == TOP) {      currentInsets.top    = tabAreaInsets.top;      currentInsets.bottom = tabAreaInsets.bottom;            } else {      currentInsets.top    = tabAreaInsets.bottom;      currentInsets.bottom = tabAreaInsets.top;            }    if (buttonPlacement == RIGHT) {      currentInsets.left   = tabAreaInsets.left;      currentInsets.right  = tabAreaInsets.right + n * d.width;            } else {      currentInsets.left   = tabAreaInsets.left  + n * d.width;      currentInsets.right  = tabAreaInsets.right;            }    return currentInsets;  }  protected int lastTabInRun(int tabCount, int run) {    SingleRowTabbedPane stabPane = (SingleRowTabbedPane)tabPane;    return stabPane.getVisibleStartIndex() + stabPane.getVisibleCount() -1;              }  protected void ensureCurrentLayout() {             SingleRowTabbedLayout layout = (SingleRowTabbedLayout)tabPane.getLayout();    layout.calculateLayoutInfo();     setButtonsEnabled();    }        protected void setButtonsEnabled() {    SingleRowTabbedPane stabPane = (SingleRowTabbedPane)tabPane;    int visibleCount      = stabPane.getVisibleCount();    int visibleStartIndex = stabPane.getVisibleStartIndex();    JButton[] buttons = stabPane.getButtons();    boolean lEnable = 0 < visibleStartIndex;    boolean rEnable = visibleStartIndex + visibleCount < tabPane.getTabCount();    for (int i=0;i<buttons.length;i++) {      boolean enable = false;      String str = buttons[i].getActionCommand();      if (str.equals(SingleRowTabbedPane.ROTATE)) {        enable = lEnable || rEnable;      }      else if (str.equals(SingleRowTabbedPane.PREVIOUS)) {        enable = lEnable;      }      else if (str.equals(SingleRowTabbedPane.NEXT)) {        enable = rEnable;      }      else if (str.equals(SingleRowTabbedPane.FIRST)) {        enable = lEnable;      }      else if (str.equals(SingleRowTabbedPane.LEFT_SHIFT)) {        enable = lEnable;      }      else if (str.equals(SingleRowTabbedPane.RIGHT_SHIFT)) {        enable = rEnable;      }      else if (str.equals(SingleRowTabbedPane.LAST)) {        enable = rEnable;      }      buttons[i].setEnabled(enable);    }     }        

⌨️ 快捷键说明

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