basictabbedpaneui.java
来自「linux下建立JAVA虚拟机的源码KAFFE」· Java 代码 · 共 2,001 行 · 第 1/5 页
JAVA
2,001 行
runCount++; rect.x = x; } rect.y = y; rect.height = maxTabHeight; if (i == selectedIndex) selectedRun = runCount - 1; } } else { for (int i = 0; i < tabCount; i++) { rect = rects[i]; if (i > 0) { rect.y = rects[i - 1].y + rects[i - 1].height; } else { tabRuns[0] = 0; runCount = 1; maxTabHeight = 0; rect.y = y; } rect.height = calculateTabHeight(tabPlacement, i, fm.getHeight()); maxTabHeight = Math.max(maxTabHeight, rect.height); if (rect.y != 2 + insets.top && rect.y + rect.height > breakAt) { if (runCount > tabRuns.length - 1) expandTabRunsArray(); tabRuns[runCount] = i; runCount++; rect.y = y; } rect.x = x; rect.width = maxTabWidth; if (i == selectedIndex) selectedRun = runCount - 1; } } if (runCount > 1) { int start; if (tabPlacement == SwingConstants.TOP || tabPlacement == SwingConstants.BOTTOM) start = y; else start = x; normalizeTabRuns(tabPlacement, tabCount, start, breakAt); selectedRun = getRunForTab(tabCount, selectedIndex); if (shouldRotateTabRuns(tabPlacement)) { rotateTabRuns(tabPlacement, selectedRun); } } // Pad the runs. int tabRunOverlay = getTabRunOverlay(tabPlacement); for (int i = runCount - 1; i >= 0; --i) { int start = tabRuns[i]; int nextIndex; if (i == runCount - 1) nextIndex = 0; else nextIndex = i + 1; int next = tabRuns[nextIndex]; int end = (next != 0 ? next - 1 : tabCount - 1); if (tabPlacement == SwingConstants.TOP || tabPlacement == SwingConstants.BOTTOM) { for (int j = start; j <= end; ++j) { rect = rects[j]; rect.y = y; rect.x += getTabRunIndent(tabPlacement, i); } if (shouldPadTabRun(tabPlacement, i)) { padTabRun(tabPlacement, start, end, breakAt); } if (tabPlacement == BOTTOM) y -= (maxTabHeight - tabRunOverlay); else y += (maxTabHeight - tabRunOverlay); } else { for (int j = start; j <= end; ++j) { rect = rects[j]; rect.x = x; rect.y += getTabRunIndent(tabPlacement, i); } if (shouldPadTabRun(tabPlacement, i)) { padTabRun(tabPlacement, start, end, breakAt); } if (tabPlacement == RIGHT) x -= (maxTabWidth - tabRunOverlay); else x += (maxTabWidth - tabRunOverlay); } } padSelectedTab(tabPlacement, selectedIndex); } /** * This method is called when the JTabbedPane is laid out in * WRAP_TAB_LAYOUT. It calls calculateLayoutInfo to find the positions * of all its components. * * @param parent The Container to lay out. */ public void layoutContainer(Container parent) { calculateLayoutInfo(); int tabPlacement = tabPane.getTabPlacement(); Insets insets = tabPane.getInsets(); int childCount = tabPane.getComponentCount(); if (childCount > 0) { int compX; int compY; int tabAreaWidth = 0; int tabAreaHeight = 0; switch (tabPlacement) { case LEFT: tabAreaWidth = calculateTabAreaWidth(tabPlacement, runCount, maxTabWidth); compX = tabAreaWidth + insets.left + contentBorderInsets.left; compY = insets.top + contentBorderInsets.top; break; case RIGHT: tabAreaWidth = calculateTabAreaWidth(tabPlacement, runCount, maxTabWidth); compX = insets.left + contentBorderInsets.left; compY = insets.top + contentBorderInsets.top; break; case BOTTOM: tabAreaHeight = calculateTabAreaHeight(tabPlacement, runCount, maxTabHeight); compX = insets.left + contentBorderInsets.left; compY = insets.top + contentBorderInsets.top; break; case TOP: default: tabAreaHeight = calculateTabAreaHeight(tabPlacement, runCount, maxTabHeight); compX = insets.left + contentBorderInsets.left; compY = tabAreaHeight + insets.top + contentBorderInsets.top; } Rectangle bounds = tabPane.getBounds(); int compWidth = bounds.width - tabAreaWidth - insets.left - insets.right - contentBorderInsets.left - contentBorderInsets.right; int compHeight = bounds.height - tabAreaHeight - insets.top - insets.bottom - contentBorderInsets.top - contentBorderInsets.bottom; for (int i = 0; i < childCount; ++i) { Component c = tabPane.getComponent(i); c.setBounds(compX, compY, compWidth, compHeight); } } } /** * This method returns the minimum layout size for the given container. * * @param parent The container that is being sized. * * @return The minimum size. */ public Dimension minimumLayoutSize(Container parent) { return calculateSize(false); } // If there is more free space in an adjacent run AND the tab in the run can fit in the // adjacent run, move it. This method is not perfect, it is merely an approximation. // If you play around with Sun's JTabbedPane, you'll see that // it does do some pretty strange things with regards to not moving tabs // that should be moved. // start = the x position where the tabs will begin // max = the maximum position of where the tabs can go to (tabAreaInsets.left + the width of the tab area) /** * This method tries to "even out" the number of tabs in each run based on * their widths. * * @param tabPlacement The JTabbedPane's tab placement. * @param tabCount The number of tabs. * @param start The x position where the tabs will begin. * @param max The maximum x position where the tab can run to. */ protected void normalizeTabRuns(int tabPlacement, int tabCount, int start, int max) { Insets tabAreaInsets = getTabAreaInsets(tabPlacement); if (tabPlacement == SwingUtilities.TOP || tabPlacement == SwingUtilities.BOTTOM) { // We should only do this for runCount - 1, cause we can only shift that many times between // runs. for (int i = 1; i < runCount; i++) { Rectangle currRun = rects[lastTabInRun(tabCount, i)]; Rectangle nextRun = rects[lastTabInRun(tabCount, getNextTabRun(i))]; int spaceInCurr = currRun.x + currRun.width; int spaceInNext = nextRun.x + nextRun.width; int diffNow = spaceInCurr - spaceInNext; int diffLater = (spaceInCurr - currRun.width) - (spaceInNext + currRun.width); while (Math.abs(diffLater) < Math.abs(diffNow) && spaceInNext + currRun.width < max) { tabRuns[i]--; spaceInNext += currRun.width; spaceInCurr -= currRun.width; currRun = rects[lastTabInRun(tabCount, i)]; diffNow = spaceInCurr - spaceInNext; diffLater = (spaceInCurr - currRun.width) - (spaceInNext + currRun.width); } // Fix the bounds. int first = lastTabInRun(tabCount, i) + 1; int last = lastTabInRun(tabCount, getNextTabRun(i)); int currX = tabAreaInsets.left; for (int j = first; j <= last; j++) { rects[j].x = currX; currX += rects[j].width; } } } else { for (int i = 1; i < runCount; i++) { Rectangle currRun = rects[lastTabInRun(tabCount, i)]; Rectangle nextRun = rects[lastTabInRun(tabCount, getNextTabRun(i))]; int spaceInCurr = currRun.y + currRun.height; int spaceInNext = nextRun.y + nextRun.height; int diffNow = spaceInCurr - spaceInNext; int diffLater = (spaceInCurr - currRun.height) - (spaceInNext + currRun.height); while (Math.abs(diffLater) < Math.abs(diffNow) && spaceInNext + currRun.height < max) { tabRuns[i]--; spaceInNext += currRun.height; spaceInCurr -= currRun.height; currRun = rects[lastTabInRun(tabCount, i)]; diffNow = spaceInCurr - spaceInNext; diffLater = (spaceInCurr - currRun.height) - (spaceInNext + currRun.height); } int first = lastTabInRun(tabCount, i) + 1; int last = lastTabInRun(tabCount, getNextTabRun(i)); int currY = tabAreaInsets.top; for (int j = first; j <= last; j++) { rects[j].y = currY; currY += rects[j].height; } } } } /** * This method pads the tab at the selected index by the selected tab pad * insets (so that it looks larger). * * @param tabPlacement The placement of the tabs. * @param selectedIndex The selected index. */ protected void padSelectedTab(int tabPlacement, int selectedIndex) { Insets insets = getSelectedTabPadInsets(tabPlacement); rects[selectedIndex].x -= insets.left; rects[selectedIndex].y -= insets.top; rects[selectedIndex].width += insets.left + insets.right; rects[selectedIndex].height += insets.top + insets.bottom; } // If the tabs on the run don't fill the width of the window, make it fit now. // start = starting index of the run // end = last index of the run // max = tabAreaInsets.left + width (or equivalent) // assert start <= end. /** * This method makes each tab in the run larger so that the tabs expand * to fill the runs width/height (depending on tabPlacement). * * @param tabPlacement The placement of the tabs. * @param start The index of the first tab. * @param end The last index of the tab * @param max The amount of space in the run (width for TOP and BOTTOM * tabPlacement). */ protected void padTabRun(int tabPlacement, int start, int end, int max) { if (tabPlacement == SwingConstants.TOP || tabPlacement == SwingConstants.BOTTOM) { int runWidth = rects[end].x + rects[end].width; int spaceRemaining = max - runWidth; int numTabs = end - start + 1; // now divvy up the space. int spaceAllocated = spaceRemaining / numTabs; int currX = rects[start].x; for (int i = start; i <= end; i++) { rects[i].x = currX; rects[i].width += spaceAllocated; currX += rects[i].width; // This is used because since the spaceAllocated // variable is an int, it rounds down. Sometimes, // we don't fill an entire row, so we make it do // so now. if (i == end && rects[i].x + rects[i].width != max) rects[i].width = max - rects[i].x; } } else { int runHeight = rects[end].y + rects[end].height; int spaceRemaining = max - runHeight; int numTabs = end - start + 1; int spaceAllocated = spaceRemaining / numTabs; int currY = rects[start].y; for (int i = start; i <= end; i++) { rects[i].y = currY; rects[i].height += spaceAllocated; currY += rects[i].height; if (i == end && rects[i].y + rects[i].height != max) rects[i].height = max - rects[i].y; } } } /** * This method returns the preferred layout size for the given container. * * @param parent The container to size. * * @return The preferred layout size. */ public Dimension preferredLayoutSize(Container parent) { return calculateSize(false); } /** * This method returns the preferred tab height given a tabPlacement and * width. * * @param tabPlacement The JTabbedPane's tab placement. * @param width The expected width. * * @return The preferred tab area height. */ protected int preferredTabAreaHeight(int tabPlacement, int width) { if (tabPane.getTabCount() == 0) return calculateTabAreaHeight(tabPlacement, 0, 0); int runs = 0; int runWidth = 0; int tabWidth = 0; FontMetrics fm = getFontMetrics(); Insets tabAreaInsets = getTabAreaInsets(tabPlacement); Insets insets = tabPane.getInsets(); // Only interested in width, this is a messed up rectangle now. width -= tabAreaInsets.left + tabAreaInsets.right + insets.left + insets.right;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?