📄 closeabletabbedpane.java
字号:
return listenerList.getListeners(CloseableTabbedPaneListener.class);}/** * Notifies all listeners that have registered interest for notification on * this event type. * @param tabIndexToClose the index of the tab which should be closed * @return true if the tab can be closed, false otherwise */protected boolean fireCloseTab(int tabIndexToClose) { boolean closeit = true; // Guaranteed to return a non-null array Object[] listeners = listenerList.getListenerList(); for (Object i : listeners) { if (i instanceof CloseableTabbedPaneListener) { if (!((CloseableTabbedPaneListener) i).closeTab(tabIndexToClose)) { closeit = false; break; } } } return closeit;}/** * The class which generates the 'X' icon for the tabs. The constructor * accepts an icon which is extra to the 'X' icon, so you can have tabs * like in JBuilder. This value is null if no extra icon is required. */class CloseTabIcon implements Icon { /** * the x position of the icon */ private int x_pos; /** * the y position of the icon */ private int y_pos; /** * the width the icon */ private int width; /** * the height the icon */ private int height; /** * the additional fileicon */ private Icon fileIcon; /** * true whether the mouse is over this icon, false otherwise */ private boolean mouseover = false; /** * true whether the mouse is pressed on this icon, false otherwise */ private boolean mousepressed = false; /** * Creates a new instance of <code>CloseTabIcon</code> * @param fileIcon the additional fileicon, if there is one set */ public CloseTabIcon(Icon fileIcon) { this.fileIcon = fileIcon; width = 16; height = 16; } /** * Draw the icon at the specified location. Icon implementations may use the * Component argument to get properties useful for painting, e.g. the * foreground or background color. * @param c the component which the icon belongs to * @param g the graphic object to draw on * @param x the upper left point of the icon in the x direction * @param y the upper left point of the icon in the y direction */ public void paintIcon(Component c, Graphics g, int x, int y) { boolean doPaintCloseIcon = true; try { // JComponent.putClientProperty("isClosable", new Boolean(false)); JTabbedPane tabbedpane = (JTabbedPane) c; int tabNumber = tabbedpane.getUI().tabForCoordinate(tabbedpane, x, y); JComponent curPanel = (JComponent) tabbedpane.getComponentAt(tabNumber); Object prop = null; if ((prop = curPanel.getClientProperty("isClosable")) != null) { doPaintCloseIcon = (Boolean) prop; } } catch (Exception ignored) {/*Could probably be a ClassCastException*/} if (doPaintCloseIcon) { x_pos = x; y_pos = y; int y_p = y + 1; if (normalCloseIcon != null && !mouseover) { normalCloseIcon.paintIcon(c, g, x, y_p); } else if (hooverCloseIcon != null && mouseover && !mousepressed) { hooverCloseIcon.paintIcon(c, g, x, y_p); } else if (pressedCloseIcon != null && mousepressed) { pressedCloseIcon.paintIcon(c, g, x, y_p); } else { y_p++; Color col = g.getColor(); if (mousepressed && mouseover) { g.setColor(Color.WHITE); g.fillRect(x+1, y_p, 12, 13); } g.setColor(Color.black); g.drawLine(x+1, y_p, x+12, y_p); g.drawLine(x+1, y_p+13, x+12, y_p+13); g.drawLine(x, y_p+1, x, y_p+12); g.drawLine(x+13, y_p+1, x+13, y_p+12); g.drawLine(x+3, y_p+3, x+10, y_p+10); if (mouseover) g.setColor(Color.GRAY); g.drawLine(x+3, y_p+4, x+9, y_p+10); g.drawLine(x+4, y_p+3, x+10, y_p+9); g.drawLine(x+10, y_p+3, x+3, y_p+10); g.drawLine(x+10, y_p+4, x+4, y_p+10); g.drawLine(x+9, y_p+3, x+3, y_p+9); g.setColor(col); if (fileIcon != null) { fileIcon.paintIcon(c, g, x+width, y_p); } } } } /** * Returns the icon's width. * @return an int specifying the fixed width of the icon. */ public int getIconWidth() { return width + (fileIcon != null ? fileIcon.getIconWidth() : 0); } /** * Returns the icon's height. * @return an int specifying the fixed height of the icon. */ public int getIconHeight() { return height; } /** * Gets the bounds of this icon in the form of a <code>Rectangle<code> * object. The bounds specify this icon's width, height, and location * relative to its parent. * @return a rectangle indicating this icon's bounds */ public Rectangle getBounds() { return new Rectangle(x_pos, y_pos, width, height); }}/** * A specific <code>BasicTabbedPaneUI</code>. */class CloseableTabbedPaneUI extends BasicTabbedPaneUI { /** * the horizontal position of the text */ private int horizontalTextPosition = SwingUtilities.LEFT; /** * Creates a new instance of <code>CloseableTabbedPaneUI</code> */ public CloseableTabbedPaneUI() { } /** * Creates a new instance of <code>CloseableTabbedPaneUI</code> * @param horizontalTextPosition the horizontal position of the text (e.g. * SwingUtilities.TRAILING or SwingUtilities.LEFT) */ public CloseableTabbedPaneUI(int horizontalTextPosition) { this.horizontalTextPosition = horizontalTextPosition; } /** * Layouts the label * @param tabPlacement the placement of the tabs * @param metrics the font metrics * @param tabIndex the index of the tab * @param title the title of the tab * @param icon the icon of the tab * @param tabRect the tab boundaries * @param iconRect the icon boundaries * @param textRect the text boundaries * @param isSelected true whether the tab is selected, false otherwise */ protected void layoutLabel(int tabPlacement, FontMetrics metrics, int tabIndex, String title, Icon icon, Rectangle tabRect, Rectangle iconRect, Rectangle textRect, boolean isSelected) { textRect.x = textRect.y = iconRect.x = iconRect.y = 0; javax.swing.text.View v = getTextViewForTab(tabIndex); if (v != null) { tabPane.putClientProperty("html", v); } SwingUtilities.layoutCompoundLabel((JComponent) tabPane, metrics, title, icon, SwingUtilities.CENTER, SwingUtilities.CENTER, SwingUtilities.CENTER, //SwingUtilities.TRAILING, horizontalTextPosition, tabRect, iconRect, textRect, textIconGap + 2); tabPane.putClientProperty("html", null); int xNudge = getTabLabelShiftX(tabPlacement, tabIndex, isSelected); int yNudge = getTabLabelShiftY(tabPlacement, tabIndex, isSelected); iconRect.x += xNudge; iconRect.y += yNudge; textRect.x += xNudge; textRect.y += yNudge; }}/** * A specific <code>MetalTabbedPaneUI</code>. */class CloseableMetalTabbedPaneUI extends MetalTabbedPaneUI { /** * the horizontal position of the text */ private int horizontalTextPosition = SwingUtilities.LEFT; /** * Creates a new instance of <code>CloseableMetalTabbedPaneUI</code> */ public CloseableMetalTabbedPaneUI() { } /** * Creates a new instance of <code>CloseableMetalTabbedPaneUI</code> * @param horizontalTextPosition the horizontal position of the text (e.g. * SwingUtilities.TRAILING or SwingUtilities.LEFT) */ public CloseableMetalTabbedPaneUI(int horizontalTextPosition) { this.horizontalTextPosition = horizontalTextPosition; } /** * Layouts the label * @param tabPlacement the placement of the tabs * @param metrics the font metrics * @param tabIndex the index of the tab * @param title the title of the tab * @param icon the icon of the tab * @param tabRect the tab boundaries * @param iconRect the icon boundaries * @param textRect the text boundaries * @param isSelected true whether the tab is selected, false otherwise */ protected void layoutLabel(int tabPlacement, FontMetrics metrics, int tabIndex, String title, Icon icon, Rectangle tabRect, Rectangle iconRect, Rectangle textRect, boolean isSelected) { textRect.x = textRect.y = iconRect.x = iconRect.y = 0; javax.swing.text.View v = getTextViewForTab(tabIndex); if (v != null) { tabPane.putClientProperty("html", v); } SwingUtilities.layoutCompoundLabel((JComponent) tabPane, metrics, title, icon, SwingUtilities.CENTER, SwingUtilities.CENTER, SwingUtilities.CENTER, //SwingUtilities.TRAILING, horizontalTextPosition, tabRect, iconRect, textRect, textIconGap + 2); tabPane.putClientProperty("html", null); int xNudge = getTabLabelShiftX(tabPlacement, tabIndex, isSelected); int yNudge = getTabLabelShiftY(tabPlacement, tabIndex, isSelected); iconRect.x += xNudge; iconRect.y += yNudge; textRect.x += xNudge; textRect.y += yNudge; }}}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -