鼠标的移入移出效果

      继承JButton,使用borderFactory类,当鼠标移入、移出、被点击时有不同的外观。  

import  javax.swing. *
import  javax.swing.border. *
import  java.awt.event. *
import  java.awt. *
/** */ /** 
 * JButton类按钮的一种扩展,当鼠标移入、移出、被点击时有不同的外观 
 
*/
 
public   class  ZButton  extends  JButton 

    
private Border borderOut; 
    
private Border borderIn; 
    
private Border borderPressed; 

    
public ZButton() 
    

        init(); 
    }
 
    
/** *//**初始化*/ 
    
private void init() 
    

        borderOut     
= BorderFactory.createEmptyBorder();         borderIn=BorderFactory.createTitledBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED,Color.white,Color.white, new Color(148145140),new Color(10310198)),"鼠标进入",TitledBorder.CENTER,TitledBorder.BELOW_TOP);
borderPressed
=BorderFactory.createTitledBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED,Color.white,Color.white,new Color(148145140),new Color(10310198)),"鼠标摁下",TitledBorder.CENTER,TitledBorder.BELOW_TOP);
        
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); 
            }
 

        }
); 
    }
 
    
/** *//**鼠标移入时的外观*/ 
    
void thisButton_mouseEntered(MouseEvent e)
        
ifthis.isEnabled() ) 
            
this.setBorder( borderIn ); 
    }
 
    
/** *//**鼠标移出时的外观*/ 
    
void thisButton_mouseExited(MouseEvent e)
        
this.setBorder( borderOut ); 
    }
 
    
/** *//**鼠标被点击时的外观*/ 
    
void thisButton_mousePressed(MouseEvent e)
        
ifthis.isEnabled()) 
            
this.setBorder( borderPressed ); 
    }
 
    
/** *//**鼠标被释放时的外观*/ 
    
void thisButton_mouseReleased(MouseEvent e) 
    

        
if( (this.getBounds().contains(e.getX(), e.getY())) && this.isEnabled() ) 
            
this.setBorder( borderIn ); 
        
else 
            
this.setBorder( borderOut ); 
    }
 


}

       ZButton继承了JButton,可以直接使用ZButton,下面是测试程序,一定要把ZButton的源文件和测试程序放在同一个目录,否则须设置环境变量.
import  javax.swing. *
import  javax.swing.border. *
import  java.awt.event. *
import  java.awt. * ;

public   class  Test {
      
public Test(){
     JFrame f
= new JFrame();
     Container contentPane
=f.getContentPane();
     ZButton a
=new ZButton();
     contentPane.add(a);
     f.setTitle(
"just a test");
     f.setSize(
new Dimension(200,80));
     f.setVisible(
true);

}

 

     
public static void main(String []args){
          Test q
=new Test();
}

}

你可能感兴趣的:(java,swing,F#)