windowsinternalframetitlepane.java

来自「JAVA 所有包」· Java 代码 · 共 521 行 · 第 1/2 页

JAVA
521
字号
		    icon.paintIcon(this, g, 0, 0);		}		g.dispose();	    }	};        systemLabel.addMouseListener(new MouseAdapter() {            public void mouseClicked(MouseEvent e) {                if (e.getClickCount() == 2 && frame.isClosable() &&                    !frame.isIcon()) {                    systemPopupMenu.setVisible(false);                    frame.doDefaultCloseAction();                }                else {                    super.mouseClicked(e);                }            }            public void mousePressed(MouseEvent e) {                try {                    frame.setSelected(true);                } catch(PropertyVetoException pve) {                }		showSystemPopupMenu(e.getComponent());            }        });    }    protected void addSystemMenuItems(JPopupMenu menu) {        JMenuItem mi = (JMenuItem)menu.add(restoreAction);        mi.setMnemonic('R');        mi = (JMenuItem)menu.add(moveAction);        mi.setMnemonic('M');        mi = (JMenuItem)menu.add(sizeAction);        mi.setMnemonic('S');        mi = (JMenuItem)menu.add(iconifyAction);        mi.setMnemonic('n');        mi = (JMenuItem)menu.add(maximizeAction);        mi.setMnemonic('x');        systemPopupMenu.add(new JSeparator());        mi = (JMenuItem)menu.add(closeAction);        mi.setMnemonic('C');    }    protected void showSystemMenu(){	showSystemPopupMenu(systemLabel);    }    private void showSystemPopupMenu(Component invoker){	Dimension dim = new Dimension();	Border border = frame.getBorder();	if (border != null) {	    dim.width += border.getBorderInsets(frame).left +		border.getBorderInsets(frame).right;	    dim.height += border.getBorderInsets(frame).bottom +		border.getBorderInsets(frame).top;	}	if (!frame.isIcon()) {	    systemPopupMenu.show(invoker,                getX() - dim.width,                getY() + getHeight() - dim.height);	} else {	    systemPopupMenu.show(invoker,                getX() - dim.width,                getY() - systemPopupMenu.getPreferredSize().height -		     dim.height);	}    }    protected PropertyChangeListener createPropertyChangeListener() {        return new WindowsPropertyChangeHandler();    }    protected LayoutManager createLayout() {        return new WindowsTitlePaneLayout();    }    public class WindowsTitlePaneLayout extends BasicInternalFrameTitlePane.TitlePaneLayout {	private Insets captionMargin = null;	private Insets contentMargin = null;	private XPStyle xp = XPStyle.getXP();	WindowsTitlePaneLayout() {	    if (xp != null) {		Component c = WindowsInternalFrameTitlePane.this;		captionMargin = xp.getMargin(c, Part.WP_CAPTION, null, Prop.CAPTIONMARGINS);		contentMargin = xp.getMargin(c, Part.WP_CAPTION, null, Prop.CONTENTMARGINS);	    }	    if (captionMargin == null) {		captionMargin = new Insets(0, 2, 0, 2);	    }	    if (contentMargin == null) {		contentMargin = new Insets(0, 0, 0, 0);	    }	}	private int layoutButton(JComponent button, Part part,				 int x, int y, int w, int h, int gap,				 boolean leftToRight) {	    if (!leftToRight) {		x -= w;	    }	    button.setBounds(x, y, w, h);	    if (leftToRight) {		x += w + 2;	    } else {		x -= 2;	    }	    return x;	}        public void layoutContainer(Container c) {            boolean leftToRight = WindowsGraphicsUtils.isLeftToRight(frame);	    int x, y;            int w = getWidth();            int h = getHeight();	    // System button	    // Note: this icon is square, but the buttons aren't always.	    int iconSize = (xp != null) ? (h-2)*6/10 : h-4;	    if (xp != null) {		x = (leftToRight) ? captionMargin.left + 2 : w - captionMargin.right - 2;	    } else {		x = (leftToRight) ? captionMargin.left : w - captionMargin.right;	    }	    y = (h - iconSize) / 2;	    layoutButton(systemLabel, Part.WP_SYSBUTTON,			 x, y, iconSize, iconSize, 0,			 leftToRight);	    // Right hand buttons	    if (xp != null) {		x = (leftToRight) ? w - captionMargin.right - 2 : captionMargin.left + 2;		y = 1;	// XP seems to ignore margins and offset here		if (frame.isMaximum()) {		    y += 1;		} else {		    y += 5;		}	    } else {		x = (leftToRight) ? w - captionMargin.right : captionMargin.left;		y = (h - buttonHeight) / 2;	    }	    if(frame.isClosable()) {		x = layoutButton(closeButton, Part.WP_CLOSEBUTTON,				 x, y, buttonWidth, buttonHeight, 2,				 !leftToRight);	    } 	    if(frame.isMaximizable()) {		x = layoutButton(maxButton, Part.WP_MAXBUTTON,				 x, y, buttonWidth, buttonHeight, (xp != null) ? 2 : 0,				 !leftToRight);	    }	    if(frame.isIconifiable()) {		layoutButton(iconButton, Part.WP_MINBUTTON,			     x, y, buttonWidth, buttonHeight, 0,			     !leftToRight);	    } 	}    } // end WindowsTitlePaneLayout    public class WindowsPropertyChangeHandler extends PropertyChangeHandler {        public void propertyChange(PropertyChangeEvent evt) {	    String prop = (String)evt.getPropertyName();            // Update the internal frame icon for the system menu.            if (JInternalFrame.FRAME_ICON_PROPERTY.equals(prop) &&                    systemLabel != null) {                systemLabel.setIcon(frame.getFrameIcon());            }            super.propertyChange(evt);        }    }    /**     * A versatile Icon implementation which can take an array of Icon     * instances (typically <code>ImageIcon</code>s) and choose one that gives the best     * quality for a given Graphics2D scale factor when painting.     * <p>     * The class is public so it can be instantiated by UIDefaults.ProxyLazyValue.     * <p>     * Note: We assume here that icons are square.     */    public static class ScalableIconUIResource implements Icon, UIResource {	// We can use an arbitrary size here because we scale to it in paintIcon()	private static final int SIZE = 16;	private Icon[] icons;	/**	 * @params objects an array of Icon or UIDefaults.LazyValue	 * <p>	 * The constructor is public so it can be called by UIDefaults.ProxyLazyValue.	 */	public ScalableIconUIResource(Object[] objects) {	    this.icons = new Icon[objects.length];	    for (int i = 0; i < objects.length; i++) {		if (objects[i] instanceof UIDefaults.LazyValue) {		    icons[i] = (Icon)((UIDefaults.LazyValue)objects[i]).createValue(null);		} else {		    icons[i] = (Icon)objects[i];		}	    }	    	}	/**	 * @return the <code>Icon</code> closest to the requested size	 */	protected Icon getBestIcon(int size) {	    if (icons != null && icons.length > 0) {		int bestIndex = 0;		int minDiff = Integer.MAX_VALUE;		for (int i=0; i < icons.length; i++) {		    Icon icon = icons[i];		    int iconSize;		    if (icon != null && (iconSize = icon.getIconWidth()) > 0) {			int diff = Math.abs(iconSize - size);			if (diff < minDiff) {			    minDiff = diff;			    bestIndex = i;			}		    }		}		return icons[bestIndex];	    } else {		return null;	    }	}	public void paintIcon(Component c, Graphics g, int x, int y) {	    Graphics2D g2d = (Graphics2D)g.create();	    // Calculate how big our drawing area is in pixels	    // Assume we are square	    int size = getIconWidth();	    double scale = g2d.getTransform().getScaleX();	    Icon icon = getBestIcon((int)(size * scale));	    int iconSize;	    if (icon != null && (iconSize = icon.getIconWidth()) > 0) {		// Set drawing scale to make icon act true to our reported size		double drawScale = size / (double)iconSize;		g2d.translate(x, y);		g2d.scale(drawScale, drawScale);		icon.paintIcon(c, g2d, 0, 0);	    }	    g2d.dispose();	}		public int getIconWidth() {	    return SIZE;	}	public int getIconHeight() {	    return SIZE;	}    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?