继承JButton,使用borderFactory类,当鼠标移入、移出、被点击时有不同的外观。
import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.*;
import java.awt.*;
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
/** *//**
* JButton类按钮的一种扩展,当鼠标移入、移出、被点击时有不同的外观
*/
public class ZButton extends JButton
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
private Border borderOut;
private Border borderIn;
private Border borderPressed;
![](/Images/OutliningIndicators/InBlock.gif)
public ZButton()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
init();
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/** *//**初始化*/
private void init()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
borderOut = BorderFactory.createEmptyBorder(); borderIn=BorderFactory.createTitledBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED,Color.white,Color.white, new Color(148, 145, 140),new Color(103, 101, 98)),"鼠标进入",TitledBorder.CENTER,TitledBorder.BELOW_TOP);
borderPressed=BorderFactory.createTitledBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED,Color.white,Color.white,new Color(148, 145, 140),new Color(103, 101, 98)),"鼠标摁下",TitledBorder.CENTER,TitledBorder.BELOW_TOP);
this.setBorder( borderOut );
![](/Images/OutliningIndicators/InBlock.gif)
this.addMouseListener(new java.awt.event.MouseAdapter()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public void mouseEntered(MouseEvent e)
{
thisButton_mouseEntered(e);
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public void mouseExited(MouseEvent e)
{
thisButton_mouseExited(e);
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public void mousePressed(MouseEvent e)
{
thisButton_mousePressed(e);
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public void mouseReleased(MouseEvent e)
{
thisButton_mouseReleased(e);
}
![](/Images/OutliningIndicators/InBlock.gif)
});
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/** *//**鼠标移入时的外观*/
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
void thisButton_mouseEntered(MouseEvent e)
{
if( this.isEnabled() )
this.setBorder( borderIn );
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/** *//**鼠标移出时的外观*/
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
void thisButton_mouseExited(MouseEvent e)
{
this.setBorder( borderOut );
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/** *//**鼠标被点击时的外观*/
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
void thisButton_mousePressed(MouseEvent e)
{
if( this.isEnabled())
this.setBorder( borderPressed );
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/** *//**鼠标被释放时的外观*/
void thisButton_mouseReleased(MouseEvent e)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if( (this.getBounds().contains(e.getX(), e.getY())) && this.isEnabled() )
this.setBorder( borderIn );
else
this.setBorder( borderOut );
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/None.gif)
ZButton继承了JButton,可以直接使用ZButton,下面是测试程序,一定要把ZButton的源文件和测试程序放在同一个目录,否则须设置环境变量.
import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.*;
import java.awt.*;
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
public class Test
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
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);
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public static void main(String []args)
{
Test q=new Test();
}
}