jnlabel.java

来自「纯java操作系统jnode,安装简单和操作简单的个人使用的Java操作系统」· Java 代码 · 共 591 行 · 第 1/2 页

JAVA
591
字号
package org.jnode.wt.components;

import org.jnode.wt.events.JNodeMouseEvent;
import org.jnode.wt.image.JNImage;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.peer.LabelPeer;

/**
 * @author Kishore
 */

public class JNLabel extends JNAbstractButton implements LabelPeer
{


    public static String STRING_DOTS = "...";


    protected String text = "";

/* These two variables are moved into superClass KComponent
	along with the method getRequiredSize().
protected int required_Width  = 0;
protected int required_Height = 0; */

    protected int _visibleText_X = comp_insets.left;
    protected int _visibleText_Y = comp_insets.top;
    protected String _visibleText;


//protected Color foregroundColor = Color.black;
//protected Font font = new Font("dialog",0,12);

//protected int location_X = 0;
//protected int location_Y = 0;



// Constants for Text Alignment.
    public static final int LEFT = 0;
    public static final int CENTER = 1;
    public static final int RIGHT = 2;

    private int text_alignment = org.jnode.wt.components.JNLabel.CENTER;

    private int required_Width = 0;
    private int required_Height = 0;

// mouseOver changes like icon, or background,foreground.


    private Color mouseOverBackground = null;
    private Color mouseOverForeground = null;

//private boolean isSelected = false;

    private JNImage labelImage = null;
    private int img_lab_width = 0;
    private int img_lab_height = 0;

    private JNImage selectedImage = null;
    private JNImage unselectedImage = null;

    private boolean isMouseOver = false;
    private boolean isMouseOverEffectEnabled = false;
    protected int mouseOverX1 = 0;
    protected int mouseOverX2 = 0;

    protected int mouseOverY1 = 0;
    protected int mouseOverY2 = 0;

    protected Insets imageInsets = new Insets(0, 0, 0, 0);

    /**
     * KLabel constructor comment.
     */
    public JNLabel(String s) {
        super();
        this.setText(s);
        this.setSize(getPreferredSize());
        this.setOpaque(true);
        init();
    }

    public JNLabel(String s, int w, int h) {
        super();

        this.setText(s);
        this.setSize(w, h);

        init();
    }

    protected Dimension _calculate_RequiredSize() {
        FontMetrics fm = getFontMetrics();

        required_Width = fm.stringWidth(text) + (comp_insets.left + comp_insets.right);
        required_Height = fm.getHeight() + (comp_insets.top * comp_insets.bottom);


//	required_Height = fm.getHeight() + (INSETS_HEIGHT * 2) + fm.getAscent();

// -------------------
/*	 if width,height is less than zero it means the value is being set for the first time.
		User code cant set size values for lessthan zero, see setSize() -@- MODIFIED -@- */

        int wid = this.getWidth();
        int hei = this.getHeight();

        boolean isfirsttime = false;

        if (wid < 0) {
            wid = preferredSize.width;
            isfirsttime = true;
        }
        if (hei < 0) {
            hei = preferredSize.height;
            isfirsttime = true;
        }

        if (isfirsttime) {
            this.setSize(wid, hei);
        }
        //--------
        Dimension rs = new Dimension(required_Width, required_Height);

//	if( null == preferedSize)
        super.setPreferredSize(rs);

        return rs;
    }
/* calculate Text (DOTS ...) X Y. */
    protected void _calculate_Text_And_Dimensions() {
        int wid = this.getWidth();
        int hei = this.getHeight();

        /* IF text is empty nothing is displayed, not even DOTS.  */
        if (text.length() <= 0) {
            _visibleText = "";
            return;
        }

        /* [1] Calculate whether all char's can be visible else set text as DOTS (...) */
        // -*IF*- HEIGHT IS LESS THAN NEEDED(prefered size), visible text is DOTS (...)
        if (hei < preferredSize.height) {
            // Visible Text is
            _visibleText = "...";

            // Location of Dots is
            _visibleText_X = wid / 2 - comp_insets.left;  // 5 pixels for 3 Dots (...)
            if (_visibleText_X < comp_insets.left)
                _visibleText_X = comp_insets.left;

            _visibleText_Y = hei / 2 + comp_insets.top;

            return;
        }


        /*  -*ELSE*- THERE ARE FEW CONDITIONS */

        // For all conditions Y math is same,

        int total_vertical_GAP_by_2 = (hei - preferredSize.height) / 2;

        _visibleText_Y = total_vertical_GAP_by_2 + preferredSize.height;

        /* Loop and find how many charecters will fit in given WIDTH */

        if (wid >= preferredSize.width)  // Text can be displayed properly 'FULL TEXT'.
        {
            // text Fits inside the given width.
            _visibleText = text;

            int horizontal_GAP_by_2 = (wid - preferredSize.width) / 2;


            if (horizontal_GAP_by_2 < comp_insets.left) {
                _visibleText_X = comp_insets.left;
            } else {
                /* ALIGNMENT is CENTER */
                if (getAlignment() == JNLabel.CENTER)
                    _visibleText_X = horizontal_GAP_by_2;

                /* ALIGNMENT is LEFT */
                if (getAlignment() == JNLabel.LEFT)
                    _visibleText_X = comp_insets.left;

                /* ALIGNMENT is CENTER */
                if (getAlignment() == JNLabel.RIGHT)
                    _visibleText_X = wid - (preferredSize.width - comp_insets.left);
            }

            setMouseOverDetails(getFontMetrics().stringWidth(_visibleText),
                    getFontMetrics().getHeight());

            return;
        } else /*if(this.width < (required_Width) )*/  // Partial Text can be displayed  'FULL T...'.
        {
            _visibleText_X = comp_insets.left;

            // calcuate and compare text size by each char.
            int currentWidth = (comp_insets.left * 2);

            if (wid < (comp_insets.left * 2)) {
                _visibleText = "";
                return;
            }

            int indexofchar = 0;

            while (currentWidth <= wid) {
                if (indexofchar >= text.length()) {
                    System.out.println("This exit should never happen, KLabel::_calculate_Text_And_Dimensions()");
                    _visibleText = "";
                    return;   //break;		// Special exit.
                }

                currentWidth = currentWidth + getFontMetrics().charWidth(text.charAt(indexofchar));

                indexofchar++;
            }

//       _visibleText = text.substring(0, indexofchar) + STRING_DOTS;
            _visibleText = text.substring(0, indexofchar - 1) + STRING_DOTS;

            setMouseOverDetails(getFontMetrics().stringWidth(_visibleText),
                    getFontMetrics().getHeight());


        }// END of main IF-ELSE
    }

    protected Color _getInternalBGColor() {
//System.out.print("  isMouseOver={ "+isMouseOver+" } ");

        if ((isSelected()) && (mouseOverBackground != null))
//	if( (isSelected) || (mouseOverBackground != null) )
        {
//System.out.println( "  -$-  "+ "'"+_visibleText+"'");
            return mouseOverBackground;
        }
//System.out.println("  -*U*-  "+ "'"+_visibleText+"'");
        return getBackground();
    }

    protected Color _getInternalFGColor() {
        if ((isSelected()) && (null != mouseOverForeground))
//	if( (isSelected) || (null != mouseOverForeground) )
        {
            return mouseOverForeground;
        }

        return getForeground();
    }

    public int getAlignment() {
        return this.text_alignment;
    }

    public boolean getMouseOverEffect() {
        return isMouseOverEffectEnabled;
    }

    public Dimension getPreferredSize() {
        Dimension d = super.getPreferredSize();

//	if( null == d)
        {
            d = _calculate_RequiredSize();
        }
        // Also update super
        /* IF prefereSize is null, it is updated in the above method call to
            _calculate_RequiredSize();   so no need to set again */
// 	super.setPreferredSize( d ); */

        return d;
    }

    public String getText() {
        return text;
    }

    private void init() {

//	this.setBackground(new Color(192, 192, 192));
//	this.setBackground(new Color(207, 207, 207));

        this.setBackground(JNDefaultLookAndFeel.BackgroundColor);

⌨️ 快捷键说明

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