📄 singlerowtabbedpaneui.java
字号:
} else if (str.equals(SingleRowTabbedPane.RIGHT_SHIFT)) { enable = rEnable; } else if (str.equals(SingleRowTabbedPane.LAST)) { enable = rEnable; } buttons[i].setEnabled(enable); } } /** Tab Navigation by Key (Not yet done) */ protected void ensureVisibleTabAt(int index) { SingleRowTabbedPane stabPane = (SingleRowTabbedPane)tabPane; int visibleCount = stabPane.getVisibleCount(); int visibleStartIndex = stabPane.getVisibleStartIndex(); int visibleEndIndex = visibleStartIndex + visibleCount -1; if (visibleStartIndex < index && index < visibleEndIndex) { return; } int selectedIndex = tabPane.getSelectedIndex(); boolean directionIsRight = (0 < index - selectedIndex)? true: false; //if (directionIsRight) { if (index <= visibleStartIndex) { //System.out.println("dec"); if (visibleStartIndex == 0) return; stabPane.setVisibleStartIndex( --visibleStartIndex ); ((SingleRowTabbedLayout)tabPane.getLayout()).calculateLayoutInfo(); int count = stabPane.getVisibleCount(); int startIndex = stabPane.getVisibleStartIndex(); if (startIndex <= index && index <= startIndex + count-1) { } else { stabPane.setVisibleStartIndex( ++visibleStartIndex ); } } //} else { if (visibleEndIndex <= index) { //System.out.println("inc"); if (visibleStartIndex == visibleCount+1) return; stabPane.setVisibleStartIndex( ++visibleStartIndex ); ((SingleRowTabbedLayout)tabPane.getLayout()).calculateLayoutInfo(); int count = stabPane.getVisibleCount(); int startIndex = stabPane.getVisibleStartIndex(); if (startIndex <= index && index <= startIndex + count-1) { } else { stabPane.setVisibleStartIndex( --visibleStartIndex ); } } //} int c = stabPane.getVisibleCount(); int s = stabPane.getVisibleStartIndex(); } protected void selectNextTab(int current) { for (int i=current+1;i<tabPane.getTabCount();i++) { if (tabPane.isEnabledAt(i)) { ensureVisibleTabAt(i); tabPane.setSelectedIndex(i); break; } } } protected void selectPreviousTab(int current) { for (int i=current-1;0<=i;i--) { if (tabPane.isEnabledAt(i)) { ensureVisibleTabAt(i); tabPane.setSelectedIndex(i); break; } } } /* not used protected int getPreviousTabIndex(int base) { int tabIndex = base - 1; return (tabIndex < 0? 0: tabIndex); } protected int getNextTabIndex(int base) { int tabIndex = base + 1; return (tabPane.getTabCount() <= tabIndex? tabIndex-1: tabIndex); } */ /** these methods exist for innerclass */ void setMaxTabHeight(int maxTabHeight) { this.maxTabHeight = maxTabHeight; } int getMaxTabHeight() { return maxTabHeight; } Rectangle[] getRects() { return rects; } SingleRowTabbedPane getTabbedPane() { return (SingleRowTabbedPane)tabPane; } protected FontMetrics getFontMetrics() { Font font = tabPane.getFont(); return Toolkit.getDefaultToolkit().getFontMetrics(font); } protected int calculateMaxTabHeight(int tabPlacement) { return super.calculateMaxTabHeight(tabPlacement); } protected int calculateTabWidth(int tabPlacement, int tabIndex, FontMetrics metrics) { return super.calculateTabWidth(tabPlacement, tabIndex, metrics); } protected void assureRectsCreated(int tabCount) { super.assureRectsCreated(tabCount); } /** Layout */ class SingleRowTabbedLayout extends BasicTabbedPaneUI.TabbedPaneLayout { JTabbedPane tabPane; SingleRowTabbedLayout(JTabbedPane tabPane) { this.tabPane = tabPane; } public void layoutContainer(Container parent) { super.layoutContainer(parent); if (tabPane.getComponentCount() < 1) { return; } int tabPlacement = tabPane.getTabPlacement(); int maxTabHeight = calculateMaxTabHeight(tabPlacement); Insets tabAreaInsets = getTabAreaInsets(tabPlacement); Insets insets = tabPane.getInsets(); Rectangle bounds = tabPane.getBounds(); SingleRowTabbedPane stabPane = (SingleRowTabbedPane)tabPane; Dimension d = stabPane.getButtonPreferredSize(); JButton[] buttons = stabPane.getButtons(); int buttonPlacement = stabPane.getButtonPlacement(); int x,y; if (tabPlacement == TOP) { // TOP y = bounds.y + insets.top + tabAreaInsets.top; } else { // BOTTOM y = bounds.y + bounds.height - insets.bottom - tabAreaInsets.bottom - maxTabHeight; } if (buttonPlacement == RIGHT) { // RIGHT x = bounds.x + bounds.width - insets.right; for (int i=buttons.length-1;0<=i;i--) { x -= d.width; buttons[i].setBounds(x, y, d.width, d.height); } } else { // LEFT x = bounds.x + insets.left; for (int i=0;i<buttons.length;i++) { buttons[i].setBounds(x, y, d.width, d.height); x += d.width; } } } public void calculateLayoutInfo() { int tabCount = tabPane.getTabCount(); assureRectsCreated(tabCount); calculateTabWidths(tabPane.getTabPlacement(), tabCount); calculateTabRects(tabPane.getTabPlacement(), tabCount); } protected void calculateTabWidths(int tabPlacement, int tabCount) { if (tabCount == 0) { return; } FontMetrics metrics = getFontMetrics(); int fontHeight = metrics.getHeight(); int maxTabHeight = calculateMaxTabHeight(tabPlacement); setMaxTabHeight(maxTabHeight); Rectangle[] rects = getRects(); for (int i = 0; i < tabCount; i++) { rects[i].width = calculateTabWidth(tabPlacement, i, metrics); rects[i].height = maxTabHeight; } } protected void calculateTabRects(int tabPlacement, int tabCount) { if (tabCount == 0) { return; } SingleRowTabbedPane stabPane = (SingleRowTabbedPane)tabPane; Dimension size = tabPane.getSize(); Insets insets = tabPane.getInsets(); Insets tabAreaInsets = getTabAreaInsets(tabPlacement); int selectedIndex = tabPane.getSelectedIndex(); int maxTabHeight = getMaxTabHeight(); int x = insets.left + tabAreaInsets.left; int y; if (tabPlacement == TOP) { y = insets.top + tabAreaInsets.top; } else { // BOTTOM y = size.height - insets.bottom - tabAreaInsets.bottom - maxTabHeight; } int returnAt = size.width - (insets.right + tabAreaInsets.right); Rectangle[] rects = getRects(); int visibleStartIndex = stabPane.getVisibleStartIndex(); int visibleCount = 0; for (int i = visibleStartIndex; i < tabCount; i++) { Rectangle rect = rects[i]; if (visibleStartIndex < i) { rect.x = rects[i-1].x + rects[i-1].width; } else { rect.x = x; } if (rect.x + rect.width > returnAt) { break; } else { visibleCount++; rect.y = y; } } stabPane.setVisibleCount(visibleCount); stabPane.setVisibleStartIndex(visibleStartIndex); } } /** Listener */ protected class ShiftTabs implements ActionListener { SingleRowTabbedPane sPane; public void actionPerformed(ActionEvent e) { sPane = getTabbedPane(); int index = getStartIndex(); sPane.setVisibleStartIndex(index); sPane.repaint(); } //public abstract int getStartIndex(); protected int getStartIndex() { return 0; // first tab } protected int getStartIndex(int lastIndex) { Insets insets = sPane.getInsets(); Insets tabAreaInsets = getTabAreaInsets(sPane.getTabPlacement()); int width = sPane.getSize().width - (insets.left + insets.right) - (tabAreaInsets.left + tabAreaInsets.right); int index; Rectangle[] rects = getRects(); for (index=lastIndex;0<=index;index--) { width -= rects[index].width; if (width < 0) { break; } } return ++index; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -