jnbutton.java

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

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

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

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.peer.ButtonPeer;

/**
 * @author kishore
 */
//public class JNButton extends JNComponent {

public class JNButton extends JNAbstractButton implements ButtonPeer{

    private boolean pressed = false;
    private JNLabel aLabel;

    // Each sides it is two pixels, 2+2 = 4;
    private int button_total_border_width = 4;

    private int button_style = JNDefaultLookAndFeel.WIN_STYLE_BUTTON;

    public JNButton() {
        this("");

    }


    /**
     * KButton constructor comment.
     */
    public JNButton(String s) {
        this(s, -1, -1);

    }

    /**
     * KButton constructor comment.
     */
    public JNButton(String text, int w, int h) {
        super();

        /* This is not acutal size calculation,
           This is only to calculate the required width and hight. */
        aLabel = new JNLabel(text);//, w-1, h-1);

        init();
    }

    protected Dimension calcPreferedsize(JNLabel akl) {

        Dimension labelD = akl.getPreferredSize();

        int required_Width = labelD.width + button_total_border_width;
        int required_Height = labelD.height + button_total_border_width;

        super.setPreferredSize(required_Width, required_Height);

        return new Dimension(required_Width, required_Height);

    }

    protected Dimension calculatePreferredSize() {

        Dimension labelD = aLabel.getPreferredSize();

        int required_Width = labelD.width + button_total_border_width;
        int required_Height = labelD.height + button_total_border_width;

        super.setPreferredSize(new Dimension(required_Width, required_Height));

        return new Dimension(required_Width, required_Height);
    }


    protected void drawJavaTypeButton(Graphics g) {
        int width = this.getWidth();
        int height = this.getHeight();

        if (pressed) {
            // top grey.
            g.setColor(Color.gray);
            g.fillRect(0, 0, width - 1, height - 1);

            aLabel.setBackground(Color.darkGray);

            aLabel.paintLabel(g);

        } else {
            // top grey
            g.setColor(JNDefaultLookAndFeel.JavaGray);
            g.drawRect(0, 0, width - 1, height - 1);
            g.setColor(Color.white);
            g.drawRect(1, 1, width - 1, height - 1);


            aLabel.setBackground(JNDefaultLookAndFeel.BackgroundColor);
            aLabel.paintLabel(g);

        }
    }

    protected void drawMenuTypeButton(Graphics g) {
        int width = this.getWidth();
        int height = this.getHeight();

        if (pressed) {
            // top grey.
            g.setColor(Color.gray);
            g.fillRect(0, 0, width - 1, height - 1);
//		g.fillRect(1,1,width-2,height-2);

            aLabel.setBackground(Color.darkGray);

            aLabel.paintLabel(g);

        } else {
            // top grey
/*		g.setColor(JNDefaultLookAndFeel.JavaGray);
		g.drawRect(0,0,width-1,height-1);
		g.setColor(Color.white);
		g.drawRect(1,1,width-1,height-1); */


            aLabel.setBackground(JNDefaultLookAndFeel.BackgroundColor);
            aLabel.paintLabel(g);

        }
    }

    protected void drawWinButton(Graphics g) {
        int width = this.getWidth();
        int height = this.getHeight();

        if (pressed) {
            // top grey.
            g.setColor(Color.darkGray);
            g.drawRect(1, 1, width - 1, height - 1);

            // top white
            g.setColor(Color.white);
            g.drawRect(0, 0, width - 1, height - 1);

            aLabel.setLocation(3, 3);
            g.translate(3, 3);
            aLabel.paintLabel(g);
            g.translate(-3, -3);
        } else {
            // top white.
            g.setColor(Color.white);
            g.drawRect(1, 1, width - 1, height - 1);

            // top grey
            g.setColor(Color.darkGray);
            g.drawRect(0, 0, width - 1, height - 1);

            g.translate(2, 2);
            aLabel.paintLabel(g);
            g.translate(-2, -2);
        }
    }

    public int getAlignment() {
        return aLabel.getAlignment();
    }

    protected JNLabel getButtonLabel() {
        return aLabel;
    }

    public int getButtonStyle() {
        return button_style;
    }
/* This is used in sub-classes of JNButton, like JNToggleButton.
 */
    protected int getButtonTotalBorderWidth() {
        return button_total_border_width;
    }

    public String getText() {
        return aLabel.getText();
    }

    protected void init() {
        Dimension d = calcPreferedsize(aLabel);

        this.setSize(d.width, d.height);
        //-*-//

        aLabel.setEnableBorderLine(false);
        aLabel.setOpaque(false);
        aLabel.setLocation(0, 0);

//	this.setBackground(new Color(207, 207, 207));
        this.setBackground(aLabel.getBackground());
        this.setVisible(true);

    }

    protected void internallyPaint(Graphics g) {
        super.internallyPaint(g);

        g.setColor(getBackground());
//		g.clearRect(0, 0, getSize().width, getSize().height);
        g.fillRect(0, 0, this.getWidth(), this.getHeight());
//	g.setColor( getForeground() );

/*
		if (pressed) {
			// top grey.
			g.setColor(Color.darkGray);
			g.drawRect(1, 1, getSize().width - 1, getSize().height - 1);

			// top white
			g.setColor(Color.white);
			g.drawRect(0, 0, getSize().width - 1, getSize().height - 1);

			//aKLabel.setLocation(1, 1);
			//aKLabel.paintLabel(g);
		} else {
			// top white.
			g.setColor(Color.white);
			g.drawRect(1, 1, getSize().width - 1, getSize().height - 1);

			// top grey
			g.setColor(Color.darkGray);
			g.drawRect(0, 0, getSize().width - 1, getSize().height - 1);

			//aKLabel.setLocation(0, 0);
			//aKLabel.paintLabel(g);
		}

		// out line.
		g.setColor(Color.black);
		g.drawRect(0, 0, getSize().width, getSize().height);
		*/


        paintButton(g);

    }

    protected void notifyListeners() {
        int id = JNActionEvent.ACTION_EVENT;
        //int type = -1;

//	System.out.println(value+" &&&&");

        JNActionEvent ae = new JNActionEvent(this, id, this.getText());

        for (int i = 0; i < listenerList.size(); i++) {
            ((JNActionListener) listenerList.get(i)).actionPerformed(ae);
        }
    }

    protected void paintButton(Graphics g) {
        int width = this.getWidth();
        int height = this.getHeight();


//	g.setColor(Color.gray);
//	g.clearRect(0,0,width,height);

/*	if(pressed)
	{
		// top grey.
		g.setColor(Color.darkGray);
		g.drawRect(1,1,width-1,height-1);

		// top white
		g.setColor(Color.white);
		g.drawRect(0,0,width-1,height-1);

		aLabel.setLocation(3, 3);
		g.translate(3,3);
		aLabel.paintLabel(g);
		g.translate(-3,-3);
	}
	else {
		// top white.
		g.setColor(Color.white);
		g.drawRect(1,1,width-1,height-1);

		// top grey
		g.setColor(Color.darkGray);
		g.drawRect(0,0,width-1,height-1);

		g.translate(2,2);
		aLabel.paintLabel(g);
		g.translate(-2,-2);
	}*/

//	drawWinButton(g);
        if (button_style == JNDefaultLookAndFeel.WIN_STYLE_BUTTON) {
            drawWinButton(g);
            // out line.
            g.setColor(Color.black);
            g.drawRect(0, 0, width, height);
        } else if (button_style == JNDefaultLookAndFeel.JAVA_STYLE_BUTTON) {
            drawJavaTypeButton(g);
        } else if (button_style == JNDefaultLookAndFeel.MENU_BUTTON) {
            drawMenuTypeButton(g);
        }


    }

    /*
    public void paintButton(Graphics g) {
        int width = this.getWidth();
        int height = this.getHeight();

        //	g.setColor(Color.gray);
        //	g.clearRect(0,0,width,height);

        if (pressed) {
            // top grey.
            g.setColor(Color.darkGray);
            g.drawRect(1, 1, width - 1, height - 1);

            // top white
            g.setColor(Color.white);
            g.drawRect(0, 0, width - 1, height - 1);

            //		aLabel.setLocation(3, 3);
            g.translate(3, 3);
            aLabel.paintLabel(g);
            g.translate(-3, -3);
        } else {
            // top white.
            g.setColor(Color.white);
            g.drawRect(1, 1, width - 1, height - 1);

            // top grey
            g.setColor(Color.darkGray);
            g.drawRect(0, 0, width - 1, height - 1);

            g.translate(2, 2);
            aLabel.paintLabel(g);
            g.translate(-2, -2);
        }

        // out line.
        g.setColor(Color.black);
        g.drawRect(0, 0, width, height);

    }*/


    protected void processMouseEvent(JNodeMouseEvent event) {
//	System.out.print("x,y=("+this.getX()+","+this.getY()+")");
//	System.out.println("| mouse x,y=("+event.getX()+","+event.getY()+")");

        if (event.getID() == JNodeMouseEvent.MOUSE_PRESSED) {
            pressed = true;
            aLabel.setSelected(true);
            Graphics gfx = getGraphics().create();
            gfx.translate(getAbsLocation().x, getAbsLocation().y);
            update(gfx);
            update(gfx);
        }
        if (event.getID() == JNodeMouseEvent.MOUSE_RELEASED) {
            pressed = false;
            aLabel.setSelected(false);
            Graphics gfx = getGraphics().create();
            gfx.translate(getAbsLocation().x, getAbsLocation().y);
            update(gfx);
            update(gfx);

            notifyListeners();
        }
        if (event.getID() == JNodeMouseEvent.MOUSE_EXITED) {
            pressed = false;
            aLabel.setSelected(false);
            Graphics gfx = getGraphics().create();
            gfx.translate(getAbsLocation().x, getAbsLocation().y);
            update(gfx);
            update(gfx);

            //notifyListeners();
        }
        super.processMouseEvent(event);



//	notifyListeners();
    }
/*
public void processMouseEvent(JNodeMouseEvent event)
{

	if(event.getID()== JNodeMouseEvent.MOUSE_PRESSED ) //&& titleBar.getAbsBounds().contains(event.getX(),event.getY()))
	{
		this.mousePressed( event );
	}
	else if(event.getID()== JNodeMouseEvent.MOUSE_RELEASED )// && titleBar.getAbsBounds().contains(event.getX(),event.getY()))
	{
		this.mouseReleased(  event );
	}
//	else	super.processMouseEvent(event);

}*/
    public void recalculate() {

    }

    /*
     * (non-Javadoc)
     *
     * @see org.jnode.wt.components.JNComponent#resize()
     */
    public void resize() {
        // TODO Auto-generated method stub

    }

    public void setAlignment(int alignment) {
        aLabel.setAlignment(alignment);
    }

    public void setBackground(Color c) {
        super.setBackground(c);
        aLabel.setBackground(c);
    }

    public void setButtonStyle(int style) {
        button_style = style;

        if (style == JNDefaultLookAndFeel.JAVA_STYLE_BUTTON) {
            setButtonTotalBorderWidth(0);
            Font f = aLabel.getFont();
            Font newf = new Font(f.getName(), Font.BOLD, f.getSize());
            aLabel.setFont(newf);

            Dimension d = calcPreferedsize(aLabel);
            super.setSize(d);


        } else {
            setButtonTotalBorderWidth(4);
        }
    }
/* This is used in sub-classes of JNButton, like JNToggleButton.
 */
    protected void setButtonTotalBorderWidth(int bw) {
        button_total_border_width = bw;

        int btb = button_total_border_width;
        aLabel.setSize(this.getWidth() - btb, this.getHeight() - btb);
    }

    public void setImage(JNImage img) {
        if (null != aLabel) {
            aLabel.setImage(img);
        }
    }

    public void setImageInsets(Insets ins) {
        aLabel.setImageInsets(ins);
    }

    public void setMouseOverEffect(boolean b) {
        aLabel.setMouseOverEffect(b);
    }

    public void setSelected(boolean b) {
        super.setSelected(b);

        aLabel.setSelected(b);
    }

    public void setSelectedImage(JNImage img) {
        if (null != aLabel) {
            aLabel.setSelectedImage(img);
        }
    }

    public void setSize(int w, int h) {
        super.setSize(w, h);

//	aKLabel.setSize( w-1, h-1);

        int btb = button_total_border_width;

        aLabel.setSize(w - btb, h - btb);
    }

    public void setText(String s) {
        aLabel.setText(s);

        calcPreferedsize(aLabel);

    }

    public void setLabel(String label) {
        //TODO: implement it
    }

}

⌨️ 快捷键说明

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