📄 basictabbedpaneui.java
字号:
Insets tabAreaInsets = getTabAreaInsets(tabPlacement); Insets insets = tabPane.getInsets(); height -= tabAreaInsets.top + tabAreaInsets.bottom + insets.top + insets.bottom; int fontHeight = fm.getHeight(); for (int i = 0; i < tabPane.getTabCount(); i++) { tabHeight = calculateTabHeight(tabPlacement, i, fontHeight); if (runHeight + tabHeight > height) { runHeight = tabHeight; runs++; } else runHeight += tabHeight; } runs++; int maxTabWidth = calculateMaxTabWidth(tabPlacement); int tabAreaWidth = calculateTabAreaWidth(tabPlacement, runs, maxTabWidth); return tabAreaWidth; } /** * This method rotates the places each run in the correct place the * tabRuns array. See the comment for tabRuns for how the runs are placed * in the array. * * @param tabPlacement The JTabbedPane's tab placement. * @param selectedRun The run the current selection is in. */ protected void rotateTabRuns(int tabPlacement, int selectedRun) { if (runCount == 1 || selectedRun == 1 || selectedRun == -1) return; int[] newTabRuns = new int[tabRuns.length]; int currentRun = selectedRun; int i = 1; do { newTabRuns[i] = tabRuns[currentRun]; currentRun = getNextTabRun(currentRun); i++; } while (i < runCount); if (runCount > 1) newTabRuns[0] = tabRuns[currentRun]; tabRuns = newTabRuns; BasicTabbedPaneUI.this.selectedRun = 1; } /** * This method is called when a component is removed from the * JTabbedPane. * * @param comp The component removed. */ public void removeLayoutComponent(Component comp) { // Do nothing. } } /** * This class acts as the LayoutManager for the JTabbedPane in * SCROLL_TAB_MODE. */ private class TabbedPaneScrollLayout extends TabbedPaneLayout { /** * This method returns the preferred layout size for the given container. * * @param parent The container to calculate a size for. * * @return The preferred layout size. */ public Dimension preferredLayoutSize(Container parent) { return super.calculateSize(true); } /** * This method returns the minimum layout size for the given container. * * @param parent The container to calculate a size for. * * @return The minimum layout size. */ public Dimension minimumLayoutSize(Container parent) { return super.calculateSize(true); } /** * This method calculates the tab area height given a desired width. * * @param tabPlacement The JTabbedPane's tab placement. * @param width The expected width. * * @return The tab area height given the width. */ protected int preferredTabAreaHeight(int tabPlacement, int width) { if (tabPane.getTabCount() == 0) return calculateTabAreaHeight(tabPlacement, 0, 0); int runs = 1; int maxTabHeight = calculateMaxTabHeight(tabPlacement); int tabAreaHeight = calculateTabAreaHeight(tabPlacement, runs, maxTabHeight); return tabAreaHeight; } /** * This method calculates the tab area width given a desired height. * * @param tabPlacement The JTabbedPane's tab placement. * @param height The expected height. * * @return The tab area width given the height. */ protected int preferredTabAreaWidth(int tabPlacement, int height) { if (tabPane.getTabCount() == 0) return calculateTabAreaHeight(tabPlacement, 0, 0); int runs = 1; int maxTabWidth = calculateMaxTabWidth(tabPlacement); int tabAreaWidth = calculateTabAreaWidth(tabPlacement, runs, maxTabWidth); return tabAreaWidth; } /** * This method is called to calculate the tab rectangles. This method * will calculate the size and position of all rectangles (taking into * account which ones should be in which tab run). It will pad them and * normalize them as necessary. * * @param tabPlacement The JTabbedPane's tab placement. * @param tabCount The number of tabs. */ protected void calculateTabRects(int tabPlacement, int tabCount) { if (tabCount == 0) return; assureRectsCreated(tabCount); FontMetrics fm = getFontMetrics(); SwingUtilities.calculateInnerArea(tabPane, calcRect); Insets tabAreaInsets = getTabAreaInsets(tabPlacement); Insets insets = tabPane.getInsets(); int max = 0; int runs = 1; int start = 0; int top = 0; if (tabPlacement == SwingConstants.TOP || tabPlacement == SwingConstants.BOTTOM) { int maxHeight = calculateMaxTabHeight(tabPlacement); calcRect.width -= tabAreaInsets.left + tabAreaInsets.right; max = calcRect.width + tabAreaInsets.left + insets.left; start = tabAreaInsets.left + insets.left; int width = 0; int runWidth = start; top = insets.top + tabAreaInsets.top; for (int i = 0; i < tabCount; i++) { width = calculateTabWidth(tabPlacement, i, fm); rects[i] = new Rectangle(runWidth, top, width, maxHeight); runWidth += width; } tabAreaRect.width = tabPane.getWidth() - insets.left - insets.right; tabAreaRect.height = runs * maxTabHeight - (runs - 1) * tabRunOverlay + tabAreaInsets.top + tabAreaInsets.bottom; contentRect.width = tabAreaRect.width; contentRect.height = tabPane.getHeight() - insets.top - insets.bottom - tabAreaRect.height; contentRect.x = insets.left; tabAreaRect.x = insets.left; if (tabPlacement == SwingConstants.BOTTOM) { contentRect.y = insets.top; tabAreaRect.y = contentRect.y + contentRect.height; } else { tabAreaRect.y = insets.top; contentRect.y = tabAreaRect.y + tabAreaRect.height; } } else { int maxWidth = calculateMaxTabWidth(tabPlacement); calcRect.height -= tabAreaInsets.top + tabAreaInsets.bottom; max = calcRect.height + tabAreaInsets.top; int height = 0; start = tabAreaInsets.top + insets.top; int runHeight = start; int fontHeight = fm.getHeight(); top = insets.left + tabAreaInsets.left; for (int i = 0; i < tabCount; i++) { height = calculateTabHeight(tabPlacement, i, fontHeight); rects[i] = new Rectangle(top, runHeight, maxWidth, height); runHeight += height; } tabAreaRect.width = runs * maxTabWidth - (runs - 1) * tabRunOverlay + tabAreaInsets.left + tabAreaInsets.right; tabAreaRect.height = tabPane.getHeight() - insets.top - insets.bottom; tabAreaRect.y = insets.top; contentRect.width = tabPane.getWidth() - insets.left - insets.right - tabAreaRect.width; contentRect.height = tabAreaRect.height; contentRect.y = insets.top; if (tabPlacement == SwingConstants.LEFT) { tabAreaRect.x = insets.left; contentRect.x = tabAreaRect.x + tabAreaRect.width; } else { contentRect.x = insets.left; tabAreaRect.x = contentRect.x + contentRect.width; } } runCount = runs; padSelectedTab(tabPlacement, tabPane.getSelectedIndex()); } /** * This method is called when the JTabbedPane is laid out in * SCROLL_TAB_LAYOUT. It finds the position for all components in the * JTabbedPane. * * @param pane The JTabbedPane to be laid out. */ public void layoutContainer(Container pane) { super.layoutContainer(pane); int tabCount = tabPane.getTabCount(); Point p = null; if (tabCount == 0) return; int tabPlacement = tabPane.getTabPlacement(); incrButton.hide(); decrButton.hide(); if (tabPlacement == SwingConstants.TOP || tabPlacement == SwingConstants.BOTTOM) { if (tabAreaRect.x + tabAreaRect.width < rects[tabCount - 1].x + rects[tabCount - 1].width) { Dimension incrDims = incrButton.getPreferredSize(); Dimension decrDims = decrButton.getPreferredSize(); decrButton.setBounds(tabAreaRect.x + tabAreaRect.width - incrDims.width - decrDims.width, tabAreaRect.y, decrDims.width, tabAreaRect.height); incrButton.setBounds(tabAreaRect.x + tabAreaRect.width - incrDims.width, tabAreaRect.y, decrDims.width, tabAreaRect.height); tabAreaRect.width -= decrDims.width + incrDims.width; incrButton.show(); decrButton.show(); } } if (tabPlacement == SwingConstants.LEFT || tabPlacement == SwingConstants.RIGHT) { if (tabAreaRect.y + tabAreaRect.height < rects[tabCount - 1].y + rects[tabCount - 1].height) { Dimension incrDims = incrButton.getPreferredSize(); Dimension decrDims = decrButton.getPreferredSize(); decrButton.setBounds(tabAreaRect.x, tabAreaRect.y + tabAreaRect.height - incrDims.height - decrDims.height, tabAreaRect.width, decrDims.height); incrButton.setBounds(tabAreaRect.x, tabAreaRect.y + tabAreaRect.height - incrDims.height, tabAreaRect.width, incrDims.height); tabAreaRect.height -= decrDims.height + incrDims.height; incrButton.show(); decrButton.show(); } } viewport.setBounds(tabAreaRect.x, tabAreaRect.y, tabAreaRect.width, tabAreaRect.height); int tabC = tabPane.getTabCount() - 1; if (tabCount > 0) { int w = Math.max(rects[tabC].width + rects[tabC].x, tabAreaRect.width); int h = Math.max(rects[tabC].height, tabAreaRect.height); p = findPointForIndex(currentScrollLocation); // we want to cover that entire space so that borders that run under // the tab area don't show up when we move the viewport around. panel.setSize(w + p.x, h + p.y); } viewport.setViewPosition(p); viewport.repaint(); } } /** * This class handles ChangeEvents from the JTabbedPane. * * @specnote Apparently this class was intended to be protected, * but was made public by a compiler bug and is now * public for compatibility. */ public class TabSelectionHandler implements ChangeListener { /** * This method is called whenever a ChangeEvent is fired from the * JTabbedPane. * * @param e The ChangeEvent fired. */ public void stateChanged(ChangeEvent e) { selectedRun = getRunForTab(tabPane.getTabCount(), tabPane.getSelectedIndex()); tabPane.revalidate(); tabPane.repaint(); } } /** * This helper class is a JPanel that fits inside the ScrollViewport. This * panel's sole job is to paint the tab rectangles inside the viewport so * that it's clipped correctly. */ private class ScrollingPanel extends JPanel { /** * This is a private UI class for our panel. */ private class ScrollingPanelUI extends BasicPanelUI { /** * This method overrides the default paint method. It paints the tab * rectangles for the JTabbedPane in the panel. * * @param g The Graphics object to paint with. * @param c The JComponent to paint. */ public void paint(Graphics g, JComponent c) { paintTabArea(g, tabPane.getTabPlacement(), tabPane.getSelectedIndex()); } } /** * This method overrides the updateUI method. It makes the default UI for * this ScrollingPanel to be a ScrollingPanelUI. */ public void updateUI() { setUI((PanelUI) new ScrollingPanelUI()); } } /** * This is a helper class that paints the panel that paints tabs. This * custom JViewport is used so that the tabs painted in the panel will be * clipped. This class implements UIResource so tabs are not added when * this objects of this class are added to the JTabbedPane. */ private class ScrollingViewport extends JViewport implements UIResource { // TODO: Maybe remove this inner class. } /** * This is a helper class that implements UIResource so it is not added as a * tab when an object of this class is added to the JTabbedPane. */ private class ScrollingButton extends BasicArrowButton implements UIResource { /** * Creates a ScrollingButton given the direction.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -