JButton with different display styles

Source: Internet
Author: User

Phenomenon:

An extension of the JButton class button that has a different look when the mouse is moved, moved, or clicked

Solution:

import javax.swing.*;


import javax.swing.border.*;


import java.awt.event.*;


import java.awt.*;


/** * Title: * Description: * Copyright: * Company: * @author * @version 1.0/

An extension of the
/** * JButton button that has a different appearance when the mouse is moved, removed, or clicked


public class Zbutton extends JButton


{


private Border borderout;


private Border Borderin;


private Border borderpressed;


public Zbutton () {init ();}/** initialization/


private void init ()


 {


borderout = Borderfactory.createemptyborder ();//(2,2,2,2);


Borderin = Borderfactory.createbevelborder (Bevelborder.raised,color.white,color.white, New Color (148, 145, 140), New Color (103, 101, 98));


borderpressed = Borderfactory.createbevelborder (Bevelborder.lowered,color.white,color.white, New Color (148, 145, 140 ), New Color (103, 101, 98));


This.setborder (borderout);


This.addmouselistener (New Java.awt.event.MouseAdapter ()


  {


public void mouseentered (MouseEvent e)


   {


thisbutton_mouseentered (e);


   }


public void mouseexited (MouseEvent e)


   {


thisbutton_mouseexited (e);


    }


public void mousepressed (MouseEvent e)


   {


thisbutton_mousepressed (e);


   }


public void mousereleased (MouseEvent e)


   {


thisbutton_mousereleased (e);


   }


   });


 }


/** the appearance of mouse move/


void thisbutton_mouseentered (MouseEvent e)


 {


if (this.isenabled ()) This.setborder (Borderin); }


/** The appearance of the mouse when it is removed * *


void thisbutton_mouseexited (MouseEvent e)


  {


This.setborder (borderout);


  }


/** the appearance of mouse when clicked * *


void thisbutton_mousepressed (MouseEvent e)


  {


if (this.isenabled ()) This.setborder (borderpressed);


  }


/** Mouse is released when the appearance * * *


void thisbutton_mousereleased (MouseEvent e)


  {


if (This.getbounds (). Contains (E.getx (), E.gety ())) && this.isenabled ())


This.setborder (Borderin); else This.setborder (borderout); }


  }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.