站内搜索: 请输入搜索关键词

当前页面: 开发资料首页Java 专题具有不同显示风格的Jbutton

具有不同显示风格的Jbutton

摘要: JButton类按钮的一种扩展,当鼠标移入、移出、被点击时有不同的外观
<iframe align=right marginWidth=0 marginHeight=0 src="http://www.chinabyte.com/tag/cont_flash_software.html" frameBorder=0 width=360 scrolling=no height=300></iframe>  现象:

  JButton类按钮的一种扩展,当鼠标移入、移出、被点击时有不同的外观

  解决方案:

<table width="100%" bgColor=#ffffff> <tr> <td>import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.*;
import java.awt.*;
/** * Title: * Description: * Copyright: * Company: * @author * @version 1.0 */
/** * JButton类按钮的一种扩展,当鼠标移入、移出、被点击时有不同的外观 */
public class ZButton extends JButton
{
 private Border borderOut;
 private Border borderIn;
 private Border borderPressed;
 public ZButton() { init(); } /**初始化*/
 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);
   }
   });
 }
 /**鼠标移入时的外观*/
 void thisButton_mouseEntered(MouseEvent e)
 {
  if( this.isEnabled() ) this.setBorder( borderIn ); }
  /**鼠标移出时的外观*/
  void thisButton_mouseExited(MouseEvent e)
  {
   this.setBorder( borderOut );
  }
  /**鼠标被点击时的外观*/
  void thisButton_mousePressed(MouseEvent e)
  {
   if( this.isEnabled()) this.setBorder( borderPressed );
  }
  /**鼠标被释放时的外观*/
  void thisButton_mouseReleased(MouseEvent e)
  {
   if( (this.getBounds().contains(e.getX(), e.getY())) && this.isEn abled() )
    this.setBorder( borderIn ); else this.setBorder( borderOut ); }
  }</td></tr></table>



↑返回目录
前一篇: JBoss技术支持文档(一)
后一篇: WebSphere Studio和XDE构建应用程序