📄 basictabbedpaneui.java
字号:
* This method creates and installs the listeners for this UI. */ protected void installListeners() { mouseListener = createMouseListener(); tabChangeListener = createChangeListener(); propertyChangeListener = createPropertyChangeListener(); focusListener = createFocusListener(); tabPane.addMouseListener(mouseListener); tabPane.addChangeListener(tabChangeListener); tabPane.addPropertyChangeListener(propertyChangeListener); tabPane.addFocusListener(focusListener); } /** * This method removes and nulls the listeners for this UI. */ protected void uninstallListeners() { tabPane.removeFocusListener(focusListener); tabPane.removePropertyChangeListener(propertyChangeListener); tabPane.removeChangeListener(tabChangeListener); tabPane.removeMouseListener(mouseListener); focusListener = null; propertyChangeListener = null; tabChangeListener = null; mouseListener = null; } /** * This method creates a new MouseListener. * * @return A new MouseListener. */ protected MouseListener createMouseListener() { return new MouseHandler(); } /** * This method creates a new FocusListener. * * @return A new FocusListener. */ protected FocusListener createFocusListener() { return new FocusHandler(); } /** * This method creates a new ChangeListener. * * @return A new ChangeListener. */ protected ChangeListener createChangeListener() { return new TabSelectionHandler(); } /** * This method creates a new PropertyChangeListener. * * @return A new PropertyChangeListener. */ protected PropertyChangeListener createPropertyChangeListener() { return new PropertyChangeHandler(); } /** * This method installs keyboard actions for the JTabbedPane. */ protected void installKeyboardActions() { // FIXME: Implement. } /** * This method uninstalls keyboard actions for the JTabbedPane. */ protected void uninstallKeyboardActions() { // FIXME: Implement. } /** * This method returns the minimum size of the JTabbedPane. * * @param c The JComponent to find a size for. * * @return The minimum size. */ public Dimension getMinimumSize(JComponent c) { return layoutManager.minimumLayoutSize(tabPane); } /** * This method returns the maximum size of the JTabbedPane. * * @param c The JComponent to find a size for. * * @return The maximum size. */ public Dimension getMaximumSize(JComponent c) { return new Dimension(Short.MAX_VALUE, Short.MAX_VALUE); } /** * This method paints the JTabbedPane. * * @param g The Graphics object to paint with. * @param c The JComponent to paint. */ public void paint(Graphics g, JComponent c) { if (tabPane.getTabCount() == 0) return; if (tabPane.getTabLayoutPolicy() == JTabbedPane.WRAP_TAB_LAYOUT) paintTabArea(g, tabPane.getTabPlacement(), tabPane.getSelectedIndex()); paintContentBorder(g, tabPane.getTabPlacement(), tabPane.getSelectedIndex()); } /** * This method paints the tab area. This includes painting the rectangles * 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 > runCount) runCount = tabCount; 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.RIGHT, 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,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -