titlepane.java

来自「用于java swing的皮肤软件」· Java 代码 · 共 1,126 行 · 第 1/2 页

JAVA
1,126 行
字号
				return;
			}
			Frame frame = getFrame();

			if (frame != null)
			{
				JRootPane rootPane = getRootPane();

				if (((state & Frame.MAXIMIZED_BOTH) != 0)
					&& (rootPane.getBorder() == null
						|| (rootPane.getBorder() instanceof UIResource))
					&& frame.isShowing())
				{
					rootPane.setBorder(null);
				}
				else if ((state & Frame.MAXIMIZED_BOTH) == 0)
				{
					// This is a croak, if state becomes bound, this can
					// be nuked.
					rootPaneUI.installBorder(rootPane);
				}
				if (frame.isResizable())
				{
					if ((state & Frame.MAXIMIZED_BOTH) != 0)
					{
						updateToggleButton(restoreAction, minimizeIcon);
						maximizeAction.setEnabled(false);
						restoreAction.setEnabled(true);
					}
					else
					{
						updateToggleButton(maximizeAction, maximizeIcon);
						maximizeAction.setEnabled(true);
						restoreAction.setEnabled(false);
					}
					if (toggleButton.getParent() == null
						|| iconifyButton.getParent() == null)
					{
						add(toggleButton);
						add(iconifyButton);
						revalidate();
						repaint();
					}
					toggleButton.setText(null);
				}
				else
				{
					maximizeAction.setEnabled(false);
					restoreAction.setEnabled(false);
					if (toggleButton.getParent() != null)
					{
						remove(toggleButton);
						revalidate();
						repaint();
					}
				}
			}
			else
			{
				// Not contained in a Frame
				maximizeAction.setEnabled(false);
				restoreAction.setEnabled(false);
				iconifyAction.setEnabled(false);
				remove(toggleButton);
				remove(iconifyButton);
				revalidate();
				repaint();
			}
			closeAction.setEnabled(true);
			this.state = state;
		}
	}

	/**
	 * Updates the toggle button to contain the Icon <code>icon</code>, and
	 * Action <code>action</code>.
	 */
	private void updateToggleButton(Action action, Icon icon)
	{
		toggleButton.setAction(action);
		toggleButton.setIcon(icon);
		toggleButton.setText(null);
	}

	/**
	 * Returns the Frame rendering in. This will return null if the <code>JRootPane</code>
	 * is not contained in a <code>Frame</code>.
	 */
	private Frame getFrame()
	{
		Window window = getWindow();

		if (window instanceof Frame)
		{
			return (Frame) window;
		}
		return null;
	}

	/**
	 * Returns the <code>Window</code> the <code>JRootPane</code> is
	 * contained in. This will return null if there is no parent ancestor of the
	 * <code>JRootPane</code>.
	 */
	private Window getWindow()
	{
		return window;
	}

	/**
	 * Returns the String to display as the title.
	 */
	private String getTitle()
	{
		Window w = getWindow();

		if (w instanceof Frame)
		{
			return ((Frame) w).getTitle();
		}
		else if (w instanceof Dialog)
		{
			return ((Dialog) w).getTitle();
		}
		return "";
	}

	
	/**
	 * Renders the TitlePane.
	 */
	/**	Paints this component. */
	public void paintComponent(Graphics g)
	{
		if (getFrame() != null) 
		{
			setState(getFrame().getExtendedState());
		}
		
		Color gradStartColor= null;
		Color gradEndColor= null;
		Color fontColor= null;

		// Draw gradient	
		if (window.isActive())
		{
			gradStartColor=
				UIManager.getColor("InternalFrame.activeTitleBackground");
			gradEndColor=
				UIManager.getColor("InternalFrame.activeTitleGradientColor");
			fontColor= UIManager.getColor("InternalFrame.activeTitleForeground");
		}
		else
		{
			gradStartColor=
				UIManager.getColor("InternalFrame.inactiveTitleBackground");
			gradEndColor=
				UIManager.getColor("InternalFrame.inactiveTitleGradientColor");
			fontColor= UIManager.getColor("InternalFrame.inactiveTitleForeground");
		}

		g.setColor(gradEndColor);
		g.fillRect(0, 0, getWidth(), getHeight());

		drawGradient(
			g,
			0,
			Math.max(60, getWidth() - 60),
			0,
			getHeight(),
			gradStartColor,
			gradEndColor);

		// Draw icon and text
		if (getTitle()!=null)
		{		
			g.setFont(UIManager.getFont("InternalFrame.font"));
			g.setColor(fontColor);
			FontMetrics fm= g.getFontMetrics();
			int yOffset= ((getHeight() - fm.getHeight()) / 2) + fm.getAscent();
			int xOffset=8+IMAGE_WIDTH;
			
			g.drawString(getTitle(), xOffset, yOffset);
		}

		// Draw border
		g.setColor(UIManager.getColor("Button.borderColor"));
		g.drawLine(0, getHeight() - 1, getWidth(), getHeight() - 1);
	}
	
	
	/**	Draws a horizontal gradient.
	 * 
	 * 	@param	g			Graphics context into which to draw
	 * 	@param	x1			The left edge of the gradient
	 * 	@param	x2			The right edge of the gradient
	 * 	@param	x			The top edge of the gradient
	 * 	@param	w			The height of the gradient
	 * 	@param	c1			The color of the left edge of the gradient
	 * 	@param	c2			The color of the right edge of the gradient
	 */
	private void drawGradient(Graphics g, int x1, int x2, int y, int height, Color c1, Color c2)
	{
		int step= 1;
		if (x2 - x1 + 1 > 256)
			step= (x2 - x1 + 1) / 256;

		for (int i= x1; i < x2; i += step)
		{
			Color c= blendColors(c1, c2, (double) (i - x1) / (x2 - x1));
			g.setColor(c);
			g.fillRect(i, y, step, height);
		}
	}
	

	/**	Calculates a color blended from the specified two colors.
	 * 	
	 * 	@param	c1			First color for blending
	 * 	@param	c2			Second color for blending
	 * 	@param	fraction	The ratio of second to first color. If this is 0.0, 
	 * 							only the first color will be used, if it is 1.0,
	 * 							only the second color will be used.
	 * 
	 * 	@return				A color blended from c1 and c2
	 */
	private Color blendColors(Color c1, Color c2, double fraction)
	{
		if (fraction < 0.0d)
			fraction= 0.0d;
		else if (fraction > 1.0d)
			fraction= 1.0d;

		int r= (int) (c1.getRed() * (1.0 - fraction) + c2.getRed() * fraction);
		int g=
			(int) (c1.getGreen() * (1.0 - fraction) + c2.getGreen() * fraction);
		int b= (int) (c1.getBlue() * (1.0 - fraction) + c2.getBlue() * fraction);

		return new Color(r, g, b);
	}
	
	
	/**
	 * Convenience method to clip the passed in text to the specified size.
	 */
	private String clippedText(String text, FontMetrics fm, int availTextWidth)
	{
		if ((text == null) || (text.equals("")))
		{
			return "";
		}
		int textWidth = SwingUtilities.computeStringWidth(fm, text);
		String clipString = "...";
		if (textWidth > availTextWidth)
		{
			int totalWidth = SwingUtilities.computeStringWidth(fm, clipString);
			int nChars;
			for (nChars = 0; nChars < text.length(); nChars++)
			{
				totalWidth += fm.charWidth(text.charAt(nChars));
				if (totalWidth > availTextWidth)
				{
					break;
				}
			}
			text = text.substring(0, nChars) + clipString;
		}
		return text;
	}

	/**
	 * Actions used to <code>close</code> the <code>Window</code>.
	 */
	private class CloseAction extends AbstractAction
	{
		public CloseAction()
		{
			super(
				UIManager.getString("MetalTitlePane.closeTitle", getLocale()));
		}

		public void actionPerformed(ActionEvent e)
		{
			close();
		}
	}

	/**
	 * Actions used to <code>iconfiy</code> the <code>Frame</code>.
	 */
	private class IconifyAction extends AbstractAction
	{
		public IconifyAction()
		{
			super(
				UIManager.getString(
					"MetalTitlePane.iconifyTitle",
					getLocale()));
		}

		public void actionPerformed(ActionEvent e)
		{
			iconify();
		}
	}

	/**
	 * Actions used to <code>restore</code> the <code>Frame</code>.
	 */
	private class RestoreAction extends AbstractAction
	{
		public RestoreAction()
		{
			super(UIManager.getString("MetalTitlePane.restoreTitle",
				getLocale()));
		}

		public void actionPerformed(ActionEvent e)
		{
			restore();
		}
	}

	/**
	 * Actions used to <code>restore</code> the <code>Frame</code>.
	 */
	private class MaximizeAction extends AbstractAction
	{
		public MaximizeAction()
		{
			super(
				UIManager.getString(
					"MetalTitlePane.maximizeTitle",
					getLocale()));
		}

		public void actionPerformed(ActionEvent e)
		{
			maximize();
		}
	}

	/**
	 * Class responsible for drawing the system menu. Looks up the image to draw
	 * from the Frame associated with the <code>JRootPane</code>.
	 */
	private class SystemMenuBar extends JMenuBar
	{
		public void paint(Graphics g)
		{
			Frame frame = getFrame();

			if (isOpaque())
			{
				g.setColor(getBackground());
				g.fillRect(0, 0, getWidth(), getHeight());
			}
			Image image = (frame != null) ? frame.getIconImage() : null;

			if (image != null)
			{
				g.drawImage(image, 0, 0, IMAGE_WIDTH, IMAGE_HEIGHT, null);
			}
			else
			{
				Icon icon = UIManager.getIcon("InternalFrame.icon");

				if (icon != null)
				{
					icon.paintIcon(this, g, 0, 0);
				}
			}
		}
		public Dimension getMinimumSize()
		{
			return getPreferredSize();
		}
		public Dimension getPreferredSize()
		{
			Dimension size = super.getPreferredSize();

			return new Dimension(
				Math.max(IMAGE_WIDTH, size.width),
				Math.max(size.height, IMAGE_HEIGHT));
		}
	}

	/**
	 * This inner class is marked &quot;public&quot; due to a compiler bug. This
	 * class should be treated as a &quot;protected&quot; inner class.
	 * Instantiate it only within subclasses of <Foo>.
	 */
	private class TitlePaneLayout implements LayoutManager
	{
		public void addLayoutComponent(String name, Component c)
		{
		}
		public void removeLayoutComponent(Component c)
		{
		}
		public Dimension preferredLayoutSize(Container c)
		{
			int height = computeHeight();
			return new Dimension(height, height);
		}

		public Dimension minimumLayoutSize(Container c)
		{
			return preferredLayoutSize(c);
		}

		private int computeHeight()
		{
			FontMetrics fm =
				Toolkit.getDefaultToolkit().getFontMetrics(getFont());
			int fontHeight = fm.getHeight();
			fontHeight += 7;
			int iconHeight = 0;
			if (getWindowDecorationStyle() == JRootPane.FRAME)
			{
				iconHeight = IMAGE_HEIGHT;
			}

			int finalHeight = Math.max(fontHeight, iconHeight);
			return finalHeight;
		}

		public void layoutContainer(Container c)
		{
			if (getWindowDecorationStyle() != JRootPane.FRAME)
			{
				buttonsWidth = 0;
				return;
			}
			boolean leftToRight =
				(window == null)
					? getRootPane().getComponentOrientation().isLeftToRight()
					: window.getComponentOrientation().isLeftToRight();

			int w = getWidth();
			int x;
			int y = 3;
			int h=getHeight();
			int spacing;
			int buttonHeight;
			int buttonWidth;

			if (closeButton != null && closeButton.getIcon() != null)
			{
				buttonHeight = closeButton.getIcon().getIconHeight();
				buttonWidth = closeButton.getIcon().getIconWidth();
			}
			else
			{
				buttonHeight = IMAGE_HEIGHT;
				buttonWidth = IMAGE_WIDTH;
			}

			// assumes all buttons have the same dimensions
			// these dimensions include the borders

			x = leftToRight ? w : 0;

			spacing = 5;
			x = leftToRight ? spacing : w - buttonWidth - spacing;
			menuBar.setBounds(x, y, IMAGE_WIDTH, IMAGE_HEIGHT);

			y=h/2-buttonHeight/2;
			
			x = leftToRight ? w : 0;
			spacing = 4;
			x += leftToRight ? -spacing - buttonWidth : spacing;
			if (closeButton != null)
			{
				closeButton.setBounds(x, y, buttonWidth, buttonHeight);
			}

			if (!leftToRight)
				x += buttonWidth;

			if (Toolkit
				.getDefaultToolkit()
				.isFrameStateSupported(Frame.MAXIMIZED_BOTH))
			{
				if (toggleButton.getParent() != null)
				{
					spacing = 10;
					x += leftToRight ? -spacing - buttonWidth : spacing;
					toggleButton.setBounds(x, y, buttonWidth, buttonHeight);
					if (!leftToRight)
					{
						x += buttonWidth;
					}
				}
			}

			if (iconifyButton != null && iconifyButton.getParent() != null)
			{
				spacing = 2;
				x += leftToRight ? -spacing - buttonWidth : spacing;
				iconifyButton.setBounds(x, y, buttonWidth, buttonHeight);
				if (!leftToRight)
				{
					x += buttonWidth;
				}
			}
			buttonsWidth = leftToRight ? w - x : x;
		}
	}

	/**
	 * PropertyChangeListener installed on the Window. Updates the necessary
	 * state as the state of the Window changes.
	 */
	private class PropertyChangeHandler implements PropertyChangeListener
	{
		public void propertyChange(PropertyChangeEvent pce)
		{
			String name = pce.getPropertyName();

			// Frame.state isn't currently bound.
			if ("resizable".equals(name) || "state".equals(name))
			{
				Frame frame = getFrame();

				if (frame != null)
				{
					setState(frame.getExtendedState(), true);
				}
				if ("resizable".equals(name))
				{
					getRootPane().repaint();
				}
			}
			else if ("title".equals(name))
			{
				repaint();
			}
			else if ("componentOrientation".equals(name))
			{
				revalidate();
				repaint();
			}
		}
	}

	/**
	 * WindowListener installed on the Window, updates the state as necessary.
	 */
	private class WindowHandler extends WindowAdapter
	{
		public void windowActivated(WindowEvent ev)
		{
			setActive(true);
		}

		public void windowDeactivated(WindowEvent ev)
		{
			setActive(false);
		}
	}
}

⌨️ 快捷键说明

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