basictabbedpaneui.java
来自「Mac OS X 10.4.9 for x86 Source Code gcc」· Java 代码 · 共 2,129 行 · 第 1/5 页
JAVA
2,129 行
* that make up the tabs. * * @param g The Graphics object to paint with. * @param tabPlacement The JTabbedPane's tab placement. * @param selectedIndex The selected index. */ protected void paintTabArea(Graphics g, int tabPlacement, int selectedIndex) { Rectangle ir = new Rectangle(); Rectangle tr = new Rectangle(); boolean isScroll = tabPane.getTabLayoutPolicy() == JTabbedPane.SCROLL_TAB_LAYOUT; // Please note: the ordering of the painting is important. // we WANT to paint the outermost run first and then work our way in. int tabCount = tabPane.getTabCount(); int currRun = 1; if (tabCount < 1) return; if (runCount > 1) currRun = 0; for (int i = 0; i < runCount; i++) { int first = lastTabInRun(tabCount, getPreviousTabRun(currRun)) + 1; if (isScroll) first = currentScrollLocation; else if (first == tabCount) first = 0; int last = lastTabInRun(tabCount, currRun); if (isScroll) { for (int k = first; k < tabCount; k++) { if (rects[k].x + rects[k].width - rects[first].x > viewport .getWidth()) { last = k; break; } } } for (int j = first; j <= last; j++) { if (j != selectedIndex || isScroll) paintTab(g, tabPlacement, rects, j, ir, tr); } currRun = getPreviousTabRun(currRun); } if (! isScroll) paintTab(g, tabPlacement, rects, selectedIndex, ir, tr); } /** * This method paints an individual tab. * * @param g The Graphics object to paint with. * @param tabPlacement The JTabbedPane's tab placement. * @param rects The array of rectangles that keep the size and position of * the tabs. * @param tabIndex The tab index to paint. * @param iconRect The rectangle to use for the icon. * @param textRect The rectangle to use for the text. */ protected void paintTab(Graphics g, int tabPlacement, Rectangle[] rects, int tabIndex, Rectangle iconRect, Rectangle textRect) { FontMetrics fm = getFontMetrics(); Icon icon = getIconForTab(tabIndex); String title = tabPane.getTitleAt(tabIndex); boolean isSelected = tabIndex == tabPane.getSelectedIndex(); calcRect = getTabBounds(tabPane, tabIndex); int x = calcRect.x; int y = calcRect.y; int w = calcRect.width; int h = calcRect.height; if (getRunForTab(tabPane.getTabCount(), tabIndex) == 1) { Insets insets = getTabAreaInsets(tabPlacement); switch (tabPlacement) { case TOP: h += insets.bottom; break; case LEFT: w += insets.right; break; case BOTTOM: y -= insets.top; h += insets.top; break; case RIGHT: x -= insets.left; w += insets.left; break; } } layoutLabel(tabPlacement, fm, tabIndex, title, icon, calcRect, iconRect, textRect, isSelected); paintTabBackground(g, tabPlacement, tabIndex, x, y, w, h, isSelected); paintTabBorder(g, tabPlacement, tabIndex, x, y, w, h, isSelected); // FIXME: Paint little folding corner and jagged edge clipped tab. if (icon != null) paintIcon(g, tabPlacement, tabIndex, icon, iconRect, isSelected); if (title != null && ! title.equals("")) paintText(g, tabPlacement, tabPane.getFont(), fm, tabIndex, title, textRect, isSelected); } /** * This method lays out the tab and finds the location to paint the icon * and text. * * @param tabPlacement The JTabbedPane's tab placement. * @param metrics The font metrics for the font to paint with. * @param tabIndex The tab index to paint. * @param title The string painted. * @param icon The icon painted. * @param tabRect The tab bounds. * @param iconRect The calculated icon bounds. * @param textRect The calculated text bounds. * @param isSelected Whether this tab is selected. */ protected void layoutLabel(int tabPlacement, FontMetrics metrics, int tabIndex, String title, Icon icon, Rectangle tabRect, Rectangle iconRect, Rectangle textRect, boolean isSelected) { SwingUtilities.layoutCompoundLabel(metrics, title, icon, SwingConstants.CENTER, SwingConstants.CENTER, SwingConstants.CENTER, SwingConstants.CENTER, tabRect, iconRect, textRect, textIconGap); int shiftX = getTabLabelShiftX(tabPlacement, tabIndex, isSelected); int shiftY = getTabLabelShiftY(tabPlacement, tabIndex, isSelected); iconRect.x += shiftX; iconRect.y += shiftY; textRect.x += shiftX; textRect.y += shiftY; } /** * This method paints the icon. * * @param g The Graphics object to paint. * @param tabPlacement The JTabbedPane's tab placement. * @param tabIndex The tab index to paint. * @param icon The icon to paint. * @param iconRect The bounds of the icon. * @param isSelected Whether this tab is selected. */ protected void paintIcon(Graphics g, int tabPlacement, int tabIndex, Icon icon, Rectangle iconRect, boolean isSelected) { icon.paintIcon(tabPane, g, iconRect.x, iconRect.y); } /** * This method paints the text for the given tab. * * @param g The Graphics object to paint with. * @param tabPlacement The JTabbedPane's tab placement. * @param font The font to paint with. * @param metrics The fontmetrics of the given font. * @param tabIndex The tab index. * @param title The string to paint. * @param textRect The bounds of the string. * @param isSelected Whether this tab is selected. */ protected void paintText(Graphics g, int tabPlacement, Font font, FontMetrics metrics, int tabIndex, String title, Rectangle textRect, boolean isSelected) { View textView = getTextViewForTab(tabIndex); if (textView != null) { textView.paint(g, textRect); return; } Color fg = tabPane.getForegroundAt(tabIndex); if (fg == null) fg = tabPane.getForeground(); Color bg = tabPane.getBackgroundAt(tabIndex); if (bg == null) bg = tabPane.getBackground(); Color saved_color = g.getColor(); Font f = g.getFont(); g.setFont(font); if (tabPane.isEnabledAt(tabIndex)) { g.setColor(fg); int mnemIndex = tabPane.getDisplayedMnemonicIndexAt(tabIndex); if (mnemIndex != -1) BasicGraphicsUtils.drawStringUnderlineCharAt(g, title, mnemIndex, textRect.x, textRect.y + metrics.getAscent()); else g.drawString(title, textRect.x, textRect.y + metrics.getAscent()); } else { g.setColor(bg.brighter()); int mnemIndex = tabPane.getDisplayedMnemonicIndexAt(tabIndex); if (mnemIndex != -1) BasicGraphicsUtils.drawStringUnderlineCharAt(g, title, mnemIndex, textRect.x, textRect.y); else g.drawString(title, textRect.x, textRect.y); g.setColor(bg.darker()); if (mnemIndex != -1) BasicGraphicsUtils.drawStringUnderlineCharAt(g, title, mnemIndex, textRect.x + 1, textRect.y + 1); else g.drawString(title, textRect.x + 1, textRect.y + 1); } g.setColor(saved_color); g.setFont(f); } /** * This method returns how much the label for the tab should shift in the X * direction. * * @param tabPlacement The JTabbedPane's tab placement. * @param tabIndex The tab index being painted. * @param isSelected Whether this tab is selected. * * @return The amount the label should shift by in the X direction. */ protected int getTabLabelShiftX(int tabPlacement, int tabIndex, boolean isSelected) { // No reason to shift. return 0; } /** * This method returns how much the label for the tab should shift in the Y * direction. * * @param tabPlacement The JTabbedPane's tab placement. * @param tabIndex The tab index being painted. * @param isSelected Whether this tab is selected. * * @return The amount the label should shift by in the Y direction. */ protected int getTabLabelShiftY(int tabPlacement, int tabIndex, boolean isSelected) { // No reason to shift. return 0; } /** * This method paints the focus rectangle around the selected tab. * * @param g The Graphics object to paint with. * @param tabPlacement The JTabbedPane's tab placement. * @param rects The array of rectangles keeping track of size and position. * @param tabIndex The tab index. * @param iconRect The icon bounds. * @param textRect The text bounds. * @param isSelected Whether this tab is selected. */ protected void paintFocusIndicator(Graphics g, int tabPlacement, Rectangle[] rects, int tabIndex, Rectangle iconRect, Rectangle textRect, boolean isSelected) { Color saved = g.getColor(); calcRect = iconRect.union(textRect); g.setColor(focus); g.drawRect(calcRect.x, calcRect.y, calcRect.width, calcRect.height); g.setColor(saved); } /** * This method paints the border for an individual tab. * * @param g The Graphics object to paint with. * @param tabPlacement The JTabbedPane's tab placement. * @param tabIndex The tab index. * @param x The x position of the tab. * @param y The y position of the tab. * @param w The width of the tab. * @param h The height of the tab. * @param isSelected Whether the tab is selected. */ protected void paintTabBorder(Graphics g, int tabPlacement, int tabIndex, int x, int y, int w, int h, boolean isSelected) { Color saved = g.getColor(); if (! isSelected || tabPlacement != SwingConstants.TOP) { g.setColor(shadow); g.drawLine(x + 1, y + h - 1, x + w - 1, y + h - 1); g.setColor(darkShadow); g.drawLine(x, y + h, x + w, y + h); } if (! isSelected || tabPlacement != SwingConstants.LEFT) { g.setColor(darkShadow); g.drawLine(x + w, y, x + w, y + h); g.setColor(shadow); g.drawLine(x + w - 1, y + 1, x + w - 1, y + h - 1); } if (! isSelected || tabPlacement != SwingConstants.RIGHT) { g.setColor(lightHighlight); g.drawLine(x, y, x, y + h); } if (! isSelected || tabPlacement != SwingConstants.BOTTOM) { g.setColor(lightHighlight); g.drawLine(x, y, x + w, y); } g.setColor(saved); } /** * This method paints the background for an individual tab. * * @param g The Graphics object to paint with. * @param tabPlacement The JTabbedPane's tab placement. * @param tabIndex The tab index. * @param x The x position of the tab. * @param y The y position of the tab. * @param w The width of the tab. * @param h The height of the tab. * @param isSelected Whether the tab is selected. */ protected void paintTabBackground(Graphics g, int tabPlacement, int tabIndex, int x, int y, int w, int h, boolean isSelected) { Color saved = g.getColor(); if (isSelected) g.setColor(Color.LIGHT_GRAY); else { Color bg = tabPane.getBackgroundAt(tabIndex); if (bg == null) bg = Color.GRAY; g.setColor(bg); } g.fillRect(x, y, w, h); g.setColor(saved); } /** * This method paints the border around the content area. * * @param g The Graphics object to paint with. * @param tabPlacement The JTabbedPane's tab placement. * @param selectedIndex The index of the selected tab. */ protected void paintContentBorder(Graphics g, int tabPlacement, int selectedIndex) { Insets insets = getContentBorderInsets(tabPlacement); int x = contentRect.x; int y = contentRect.y; int w = contentRect.width; int h = contentRect.height; paintContentBorderTopEdge(g, tabPlacement, selectedIndex, x, y, w, h); paintContentBorderLeftEdge(g, tabPlacement, selectedIndex, x, y, w, h); paintContentBorderBottomEdge(g, tabPlacement, selectedIndex, x, y, w, h); paintContentBorderRightEdge(g, tabPlacement, selectedIndex, x, y, w, h); } /** * This method paints the top edge of the content border. * * @param g The Graphics object to paint with. * @param tabPlacement The JTabbedPane's tab placement. * @param selectedIndex The selected tab index. * @param x The x coordinate for the content area. * @param y The y coordinate for the content area. * @param w The width of the content area. * @param h The height of the content area. */ protected void paintContentBorderTopEdge(Graphics g, int tabPlacement, int selectedIndex, int x, int y, int w, int h) { Color saved = g.getColor(); g.setColor(lightHighlight); int startgap = rects[selectedIndex].x; int endgap = rects[selectedIndex].x + rects[selectedIndex].width; int diff = 0; if (tabPlacement == SwingConstants.TOP) { if (tabPane.getTabLayoutPolicy(
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?