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

📄 clabel.java

📁 大家共享愉快, 共享愉快, 共享愉快, 共享愉快,共享愉快
💻 JAVA
字号:
/******************************************************************************
 * The contents of this file are subject to the   Compiere License  Version 1.1
 * ("License"); You may not use this file except in compliance with the License
 * You may obtain a copy of the License at http://www.compiere.org/license.html
 * Software distributed under the License is distributed on an  "AS IS"  basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 * the specific language governing rights and limitations under the License.
 * The Original Code is Compiere ERP & CRM Smart Business Solution. The Initial
 * Developer of the Original Code is Jorg Janke. Portions created by Jorg Janke
 * are Copyright (C) 1999-2005 Jorg Janke.
 * All parts are Copyright (C) 1999-2005 ComPiere, Inc.  All Rights Reserved.
 * Contributor(s): ______________________________________.
 *****************************************************************************/
package org.compiere.swing;

import java.awt.*;

import javax.swing.*;

import org.compiere.plaf.*;

/**
 *  Label with Mnemonics interpretation
 *
 *  @author     Jorg Janke
 *  @version    $Id: CLabel.java,v 1.13 2005/12/31 06:35:31 jjanke Exp $
 */
public class CLabel extends JLabel
{
	public static int   DEFAULT_ALIGNMENT = JLabel.TRAILING;

	/**
	 * Creates a <code>Label</code> instance with the specified
	 * text, image, and horizontal alignment.
	 * The label is centered vertically in its display area.
	 * The text is on the trailing edge of the image.
	 *
	 * @param text  The text to be displayed by the label.
	 * @param icon  The image to be displayed by the label.
	 * @param horizontalAlignment  One of the following constants
	 *           defined in <code>SwingConstants</code>:
	 *           <code>LEFT</code>,
	 *           <code>CENTER</code>,
	 *           <code>RIGHT</code>,
	 *           <code>LEADING</code> or
	 *           <code>TRAILING</code>.
	 */
	public CLabel (String text, Icon icon, int horizontalAlignment)
	{
		super (text, icon, horizontalAlignment);
		init();
	}

	/**
	 * Creates a <code>Label</code> instance with the specified
	 * text and horizontal alignment.
	 * The label is centered vertically in its display area.
	 *
	 * @param text  The text to be displayed by the label.
	 * @param horizontalAlignment  One of the following constants
	 *           defined in <code>SwingConstants</code>:
	 *           <code>LEFT</code>,
	 *           <code>CENTER</code>,
	 *           <code>RIGHT</code>,
	 *           <code>LEADING</code> or
	 *           <code>TRAILING</code>.
	 */
	public CLabel (String text, int horizontalAlignment)
	{
		super(text, horizontalAlignment);
		init();
	}

	/**
	 * Creates a <code>Label</code> instance with the specified text.
	 * The label is aligned against the leading edge of its display area,
	 * and centered vertically.
	 *
	 * @param text  The text to be displayed by the label.
	 */
	public CLabel (String text)
	{
		super(text, DEFAULT_ALIGNMENT);
		init();
	}

	/**
	 * Creates a <code>Label</code> instance with the specified
	 * image and horizontal alignment.
	 * The label is centered vertically in its display area.
	 *
	 * @param image  The image to be displayed by the label.
	 * @param horizontalAlignment  One of the following constants
	 *           defined in <code>SwingConstants</code>:
	 *           <code>LEFT</code>,
	 *           <code>CENTER</code>,
	 *           <code>RIGHT</code>,
	 *           <code>LEADING</code> or
	 *           <code>TRAILING</code>.
	 */
	public CLabel (Icon image, int horizontalAlignment)
	{
		super (image, horizontalAlignment);
		init();
	}

	/**
	 * Creates a <code>Label</code> instance with the specified image.
	 * The label is centered vertically and horizontally
	 * in its display area.
	 *
	 * @param image  The image to be displayed by the label.
	 */
	public CLabel (Icon image)
	{
		super (image, DEFAULT_ALIGNMENT);
		init();
	}

	/**
	 * Creates a <code>JLabel</code> instance with
	 * no image and with an empty string for the title.
	 * The label is centered vertically
	 * in its display area.
	 * The label's contents, once set, will be displayed on the leading edge
	 * of the label's display area.
	 */
	public CLabel ()
	{
		super("", DEFAULT_ALIGNMENT);
		init();
	}

	/**
	 * Creates a <code>Label</code> instance with the specified text.
	 * The label is aligned against the leading edge of its display area,
	 * and centered vertically.
	 *
	 * @param label  The text to be displayed by the label.
	 * @param toolTip   The optional Tooltip text
	 */
	public CLabel (String label, String toolTip)
	{
		super (label, DEFAULT_ALIGNMENT);
		if (toolTip != null && toolTip.length() > 0)
			super.setToolTipText(toolTip);
		init();
	}   //  CLabel

	/**
	 * 	Trailing Label for Field
	 *	@param label label
	 *	@param field field
	 */
	public CLabel (String label, Component field)
	{
		this (label, TRAILING);
		setLabelFor(field);
	}	//	CLabel
	
	
	/** Mnemonic saved			*/
	private char	m_savedMnemonic = 0;
	
	/**
	 *  Common init
	 */
	private void init()
	{
		setFocusable (false);
		setOpaque(false);
		if (getToolTipText() == null)	//	force Tool Tip
			setToolTipText(getText());
		//
		setForeground(CompierePLAF.getTextColor_Label());
		setFont(CompierePLAF.getFont_Label());
	}   //  init


	/**
	 *  Set Background
	 *  @param bg background
	 */
	public void setBackground (Color bg)
	{
		if (bg.equals(getBackground()))
			return;
		super.setBackground(bg);
	}   //  setBackground

	/**
	 * 	Set Font to Bold
	 *	@param bold true bold false normal
	 */
	public void setFontBold (boolean bold)
	{
		Font font = getFont();
		if (bold != font.isBold())
		{
			font = new Font (font.getName(), bold ? 
				Font.BOLD : Font.PLAIN,
				font.getSize());
			setFont (font);
		}
	}	//	setFontBold
	
	/**************************************************************************
	 *  Set label text - if it includes &, the next character is the Mnemonic
	 *  @param mnemonicLabel Label containing Mnemonic
	 */
	public void setText (String mnemonicLabel)
	{
		String text = createMnemonic (mnemonicLabel);
		super.setText (text);
		if (text != null && getName() == null)
			setName(text);
	}   //  setText

	/**
	 *  Create Mnemonics of text containing "&".
	 *	Based on MS notation of &Help => H is Mnemonics
	 *  @param text test with Mnemonics
	 *  @return text w/o &
	 *  @see JLabel#setLabelFor(java.awt.Component)
	 */
	private String createMnemonic(String text)
	{
		if (text == null)
			return text;
		int pos = text.indexOf("&");
		if (pos != -1)					//	We have a nemonic
		{
			char ch = text.charAt(pos+1);
			if (ch != ' ')              //  &_ - is the & character
			{
				setDisplayedMnemonic(ch);
				setSavedMnemonic(ch);
				return text.substring(0, pos) + text.substring(pos+1);
			}
		}
		return text;
	}   //  createMnemonic

	/**
	 *  Set ReadWrite
	 *  @param rw enabled
	 */
	public void setReadWrite (boolean rw)
	{
		this.setEnabled(rw);
	}   //  setReadWrite

	/**
	 * 	Set Label For
	 *	@param c component
	 */
	public void setLabelFor (Component c)
	{
		super.setLabelFor (c);
		if (c.getName() == null)
			c.setName(getName());
	}	//	setLabelFor

	
	/**
	 * @return Returns the savedMnemonic.
	 */
	public char getSavedMnemonic ()
	{
		return m_savedMnemonic;
	}	//	getSavedMnemonic
	
	/**
	 * @param savedMnemonic The savedMnemonic to set.
	 */
	public void setSavedMnemonic (char savedMnemonic)
	{
		m_savedMnemonic = savedMnemonic;
	}	//	getSavedMnemonic
	
}   //  CLabel

⌨️ 快捷键说明

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