⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 j2xwindowtitlebar.java

📁 本程序是有IBM开发的一个基于数据表格的组件,里面有相关的例子和DOC,本站资料仅为大家学习之用
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	 */
	public void setRestoreButtonState(boolean isMaximized)
	{
		for (int i=0; i<vctWindowButtons.size(); i++)
		{
			J2XWindowButton iButton = (J2XWindowButton)vctWindowButtons.get(i);
			if (iButton.getButtonType() == J2XWindowButton.RESTORE_MAX && !isMaximized)
				iButton.setButtonType(J2XWindowButton.RESTORE_MIN)	;
			else if (iButton.getButtonType() == J2XWindowButton.RESTORE_MIN && isMaximized)
				iButton.setButtonType(J2XWindowButton.RESTORE_MAX);
		}
	}
	
	/**
	 * Changes the restore button from either maximized to minimized, or minimized
	 * to maximized.  
	 * <p>This changes only the button and has no effect on the frame itself.
	 */
	public void changeRestoreButton()
	{
		for (int i=0; i<vctWindowButtons.size(); i++)
		{
			J2XWindowButton iButton = (J2XWindowButton)vctWindowButtons.get(i);
			if (iButton.getButtonType() == J2XWindowButton.RESTORE_MAX)
				iButton.setButtonType(J2XWindowButton.RESTORE_MIN)	;
			else if (iButton.getButtonType() == J2XWindowButton.RESTORE_MIN)
				iButton.setButtonType(J2XWindowButton.RESTORE_MAX);
		}
	}
	
	/**
	 * Removes all the default settings from the title bar including
	 * the title, all the title bar buttons, and the border.
	 */
	public void removeWindowDecorations()
	{
		removeTitle();
		removeAllWindowButtons();
		setBorder(null);
	}
	
	/**
	 * Sets the title on this title bar and by default adds it to the left side of
	 * the title bar.
	 * @param title the window's title
	 */
	public void setTitle(String title)
	{
		if (lblTitle == null)
		{
			lblTitle = new JLabel();
			lblTitle.setOpaque(false);
			lblTitle.setForeground(Color.white);
			add(lblTitle, new GroupFlowLayoutConstraints(SwingConstants.LEFT, new Insets(3, 6, 3, 3)));
		}
		lblTitle.setText(title);	
	}
	
	/**
	 * Removes the title from the title bar.
	 */
	public void removeTitle()
	{
		if (lblTitle == null)
			lblTitle = new JLabel();
		remove(lblTitle);
	}
	
	/**
	 * Adds a title to the title bar.
	 * @param title the frame's title
	 * @param alignment where the title should align (RIGHT, CENTER, LEFT)
	 * @param f the font of the title
	 * @param foreground the foreground color of the title
	 */
	public void addTitle(String title, int alignment, Font f, Color foreground)
	{
		if (lblTitle != null)
			remove(lblTitle);
		lblTitle = new JLabel(title);
		lblTitle.setFont(f);
		lblTitle.setForeground(foreground);
		add(lblTitle, new GroupFlowLayoutConstraints(alignment, new Insets(3,6,3,3)));
	}
	
	/**
	 * Sets the logo on the title bar and adds it to the left side.
	 * @param i the icon used as the logo
	 */
	public void setLogo(Icon i)
	{
		if (lblLogo == null)
		{
			lblLogo = new javax.swing.JLabel();
			lblLogo.setText("");
			lblLogo.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
		}
		lblLogo.setPreferredSize(new Dimension(i.getIconWidth(), i.getIconHeight()));
		lblLogo.setIcon(i);
	}
	
	/**
	 * Adds a logo to the title bar on the left side.
	 * @param i the icon used as the logo
	 */
	public void addLogo(Icon i)
	{
		lblLogo = new javax.swing.JLabel();
		lblLogo.setText("");
		lblLogo.setPreferredSize(new Dimension(i.getIconWidth(), i.getIconHeight()));
		lblLogo.setIcon(i);
		lblLogo.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
		add(lblLogo, new GroupFlowLayoutConstraints(SwingConstants.LEFT, new Insets(3, 6, 3, 3)));
	}
	
	/**
	 * Sets the foreground color on the window buttons.
	 * @param foreground the window buttons' foreground color
	 */
	public void setWindowButtonForeground(Color foreground)
	{
		for (int i=0; i<vctWindowButtons.size(); i++)
		{
			((J2XWindowButton)vctWindowButtons.get(i)).setForeground(foreground);
		}	
	}
	
	/**
	 * Sets the background color on the window buttons.
	 * @param background the window buttons' background color
	 */
	public void setWindowButtonBackground(Color background)
	{
		for (int i=0; i<vctWindowButtons.size(); i++)
		{
			((J2XWindowButton)vctWindowButtons.get(i)).setBackground(background);
		}	
	}
	
	/**
	 * Sets the background and foreground colors on the window buttons.
	 * @param background the window buttons' background color
	 * @param foreground the window buttons' foreground color
	 */
	public void setWindowButtonColors(Color background, Color foreground)
	{
		for (int i=0; i<vctWindowButtons.size(); i++)
		{
			((J2XWindowButton)vctWindowButtons.get(i)).setBackground(background);
			((J2XWindowButton)vctWindowButtons.get(i)).setForeground(foreground);
		}	
	}
	
	/**
	 * Returns the background color of the first window button on the title bar.
	 * Since every button can be a different color, this function loses
	 * some of its effectiveness when multiple background colors are used
	 * on window buttons.
	 * @return the background color of the first window button
	 */
	public Color getWindowButtonBackground()
	{
		if (vctWindowButtons.size() > 0)
			return ((J2XWindowButton)vctWindowButtons.get(0)).getBackground();
		return Color.BLACK;
	}
	
	/**
	 * Returns the foreground color of the first window button on the title bar.
	 * Since every button can be a different color, this function loses
	 * some of its effectiveness when multiple foreground colors are used
	 * on window buttons.
	 * @return the foreground color of the first window button
	 */
	public Color getWindowButtonForeground()
	{
		if (vctWindowButtons.size() > 0)
			return ((J2XWindowButton)vctWindowButtons.get(0)).getForeground();
		return Color.WHITE;
	}
	
	/**
	 * Returns the preferred size of the first window button on the title bar.
	 * Since every button can be a different size, this function loses
	 * some of its effectiveness when multiple sizes are used
	 * on window buttons.
	 * @return the size of the first window button
	 */
	public Dimension getWindowButtonSize()
	{
		if (vctWindowButtons.size() > 0)
			return ((J2XWindowButton)vctWindowButtons.get(0)).getPreferredSize();
		return new Dimension(0,0);
	}
	
	/**
	 * Adds a button on the title bar on the right side.
	 * @param iButton the window button
	 */
	public void addWindowButton(J2XWindowButton iButton)
	{
		iButton.addActionListener(this);
		vctWindowButtons.add(iButton);
		add(iButton, new GroupFlowLayoutConstraints(SwingConstants.RIGHT, new Insets(3, 1, 3, 1)));
	}
	
	/**
	 * Adds a button on the title bar on the right side.
	 * @param buttonType the type of button to add
	 */
	public void addWindowButton(int buttonType)
	{
		J2XWindowButton iButton = new J2XWindowButton(buttonType);		
		vctWindowButtons.add(iButton);
		addWindowButton(buttonType, iButton.getForeground(), iButton.getBackground());
	}
	
	/**
	 * Adds a button on the title bar on the right side.
	 * @param buttonType the type of button to add
	 * @param foreground the foreground color of the button
	 * @param background the background color of the button
	 */
	public void addWindowButton(int buttonType, Color foreground, Color background)
	{
		J2XWindowButton iButton = new J2XWindowButton(buttonType);
		iButton.setForeground(foreground);
		iButton.setBackground(background);
		iButton.addActionListener(this);
		vctWindowButtons.add(iButton);
		add(iButton, new GroupFlowLayoutConstraints(SwingConstants.RIGHT, new Insets(3, 3, 3, 3)));
	}
	
	/**
	 * Adds a button on the title bar with the desired orientation on the title bar.
	 * @param buttonType the type of button to add
	 * @param orientation the orientation the title where the button should be added
	 */
	public void addWindowButton(int buttonType, int orientation)
	{
		addWindowButton(new J2XWindowButton(buttonType), orientation);
	}
	
	/**
	 * Adds a button on the title bar with the desired orientation on the title bar.
	 * @param button the button to add
	 * @param orientation the orientation the title where the button should be added
	 */
	public void addWindowButton(J2XWindowButton button, int orientation)
	{
		vctWindowButtons.add(button);
		button.addActionListener(this);
		add(button, new GroupFlowLayoutConstraints(orientation, new Insets(3,3,3,3)));
	}
	
	/**
	 * Adds all the window buttons that are default displayed in a Windows OS.
	 */
	public void addAllWindowButtons()
	{
		removeAllWindowButtons();
		addWindowButton(J2XWindowButton.MINIMIZE);
		addWindowButton(J2XWindowButton.RESTORE_MIN);
		addWindowButton(J2XWindowButton.CLOSE);
	}
	
	/**
	 * Adds all the window buttons that are default displayed in a Windows OS
	 * with the specified colors.
	 * @param foreground the foreground color of the buttons
	 * @param background the background colors of the buttons
	 */
	public void addAllWindowButtons(Color foreground, Color background)
	{
		removeAllWindowButtons();
		addWindowButton(J2XWindowButton.MINIMIZE, foreground, background);
		addWindowButton(J2XWindowButton.RESTORE_MIN, foreground, background);
		addWindowButton(J2XWindowButton.CLOSE, foreground, background);
	}
	
	/**
	 * Removes all the window buttons from the title bar.
	 */
	public void removeAllWindowButtons()
	{
		for (int i=0; i<vctWindowButtons.size(); i++)
		{
			J2XWindowButton b = (J2XWindowButton)vctWindowButtons.get(i);
			remove(b);
			b.removeActionListener(this);			 
		}	
		vctWindowButtons.removeAllElements();
	}
	
	/** 
	 * Removes the specified window button from the title bar.
	 * @param iButton the button to be removed
	 */
	public void removeWindowButton(J2XWindowButton iButton)
	{
		remove(iButton);
		iButton.removeActionListener(this);
		vctWindowButtons.remove(iButton);	
	}
	
	/**
	 * Sets the size of the buttons on the title bar.
	 * @param size the size of the buttons
	 */
	public void setWindowButtonSize(Dimension size)
	{
		for (int i=0; i<vctWindowButtons.size(); i++)
		{
			 ((J2XWindowButton)vctWindowButtons.get(i)).setPreferredSize(size);
		}			
	}
	
	/**
	 * The default border that appears around the title bar in Windows 2000.
	 * @author MAbernethy
	 */
	protected class DefaultBorder extends AbstractBorder
	{	
		public void paintBorder(Component c, Graphics g, int x, int y, int w, int h)  
		{
			g.setColor(c.getBackground().brighter().brighter());
	        g.drawLine(0, 0, 0, h-1);
        	g.drawLine(1, 0, w-1, 0);
	
	        g.setColor(c.getBackground().brighter());
	        g.drawLine(1, 1, 1, h-2);
        	g.drawLine(2, 1, w-2, 1);
	
	        g.setColor(c.getBackground().darker().darker());
	        g.drawLine(w-1, 1, w-1, h-2);
	
	        g.setColor(c.getBackground().darker());
	        g.drawLine(w-2, 2, w-2, h-3);	
		}
	} 
	
}

⌨️ 快捷键说明

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