basictabbedpaneui.java
来自「linux下建立JAVA虚拟机的源码KAFFE」· Java 代码 · 共 2,001 行 · 第 1/5 页
JAVA
2,001 行
{ LookAndFeel.installColorsAndFont(tabPane, "TabbedPane.background", "TabbedPane.foreground", "TabbedPane.font"); tabPane.setOpaque(false); highlight = UIManager.getColor("TabbedPane.highlight"); lightHighlight = UIManager.getColor("TabbedPane.lightHighlight"); shadow = UIManager.getColor("TabbedPane.shadow"); darkShadow = UIManager.getColor("TabbedPane.darkShadow"); focus = UIManager.getColor("TabbedPane.focus"); textIconGap = UIManager.getInt("TabbedPane.textIconGap"); tabRunOverlay = UIManager.getInt("TabbedPane.tabRunOverlay"); tabInsets = UIManager.getInsets("TabbedPane.tabInsets"); selectedTabPadInsets = UIManager.getInsets("TabbedPane.tabbedPaneTabPadInsets"); tabAreaInsets = UIManager.getInsets("TabbedPane.tabAreaInsets"); contentBorderInsets = UIManager.getInsets("TabbedPane.tabbedPaneContentBorderInsets"); tabsOpaque = UIManager.getBoolean("TabbedPane.tabsOpaque"); calcRect = new Rectangle(); tabRuns = new int[10]; tabAreaRect = new Rectangle(); contentRect = new Rectangle(); } /** * This method uninstalls defaults for the Look and Feel. */ protected void uninstallDefaults() { calcRect = null; tabAreaRect = null; contentRect = null; tabRuns = null; contentBorderInsets = null; tabAreaInsets = null; selectedTabPadInsets = null; tabInsets = null; focus = null; darkShadow = null; shadow = null; lightHighlight = null; highlight = null; tabPane.setBackground(null); tabPane.setForeground(null); tabPane.setFont(null); } /** * 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.isValid()) tabPane.validate(); 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(); for (int i = runCount - 1; i >= 0; --i) { int start = tabRuns[i]; int next; if (i == runCount - 1) next = tabRuns[0]; else next = tabRuns[i + 1]; int end = (next != 0 ? next - 1 : tabCount - 1); for (int j = start; j <= end; ++j) { if (j != selectedIndex) { paintTab(g, tabPlacement, rects, j, ir, tr); } } } // Paint selected tab in front of every other tab. if (selectedIndex >= 0) 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) { Rectangle rect = rects[tabIndex]; boolean isSelected = tabIndex == tabPane.getSelectedIndex(); // Paint background if necessary. if (tabsOpaque || tabPane.isOpaque()) { paintTabBackground(g, tabPlacement, tabIndex, rect.x, rect.y, rect.width, rect.height, isSelected); } // Paint border. paintTabBorder(g, tabPlacement, tabIndex, rect.x, rect.y, rect.width, rect.height, isSelected); // Layout label. FontMetrics fm = getFontMetrics(); Icon icon = getIconForTab(tabIndex); String title = tabPane.getTitleAt(tabIndex); layoutLabel(tabPlacement, fm, tabIndex, title, icon, rect, iconRect, textRect, isSelected); // Paint the text. paintText(g, tabPlacement, tabPane.getFont(), fm, tabIndex, title, textRect, isSelected); // Paint icon if necessary. paintIcon(g, tabPlacement, tabIndex, icon, iconRect, isSelected); // Paint focus indicator. paintFocusIndicator(g, tabPlacement, rects, tabIndex, iconRect, 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) { if (icon != null) 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) { g.setFont(font); View textView = getTextViewForTab(tabIndex); if (textView != null) { textView.paint(g, textRect); return; } int mnemIndex = tabPane.getDisplayedMnemonicIndexAt(tabIndex); if (tabPane.isEnabled() && tabPane.isEnabledAt(tabIndex)) { Color fg = tabPane.getForegroundAt(tabIndex); if (isSelected && (fg instanceof UIResource)) { Color selectionForeground = UIManager.getColor("TabbedPane.selectionForeground"); if (selectionForeground != null) fg = selectionForeground; } g.setColor(fg); 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 { Color bg = tabPane.getBackgroundAt(tabIndex); g.setColor(bg.brighter()); 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); } } /** * 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 labe
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?