swing components-半透明ToolTip
效果:
代碼:
package
?azure.ui.swing.components.tooltip;
import ?java.awt.AlphaComposite;
import ?java.awt.Color;
import ?java.awt.FlowLayout;
import ?java.awt.Graphics;
import ?java.awt.Graphics2D;
import ?java.awt.Point;
import ?java.awt.RenderingHints;
import ?java.awt.event.MouseEvent;
import ?java.awt.event.MouseListener;
import ?java.awt.geom.RoundRectangle2D;
import ?java.util.StringTokenizer;
import ?javax.swing.JButton;
import ?javax.swing.JFrame;
import ?javax.swing.JLayeredPane;
import ?javax.swing.JPanel;
import ?javax.swing.SwingUtilities;
import ?javax.swing.border.LineBorder;
/**
?*? @author ?Azure
?*? @version ?1.0?13/03/07
? */
public ? class ?AzHOpaqueToolTipExample? extends ?JFrame?{
???? public ?AzHOpaqueToolTipExample()?{
????????init();
????}
???? public ? void ?init()?{
????????HalfOpaqueToolTip?tooltip? = ? new ?HalfOpaqueToolTip(
???????????????? " 我們還能不能能不能再見面\n我在佛前苦苦求了幾千年\n當我在踏過這條奈何橋之前\n讓我再吻一吻你的臉 " ,
???????????????? new ?Color( 250 ,? 250 ,? 200 ),?Color.RED,?Color.BLACK,?OPAQUE,? this );
????????JButton?button? = ? new ?JButton( " swing " );
????????button.addMouseListener(tooltip);
???????? this .setLayout( new ?FlowLayout());
???????? this .add( new ?JButton( " 金 " ));
???????? this .add( new ?JButton( " 木 " ));
???????? this .add( new ?JButton( " 水 " ));
???????? this .add( new ?JButton( " 火 " ));
???????? this .add( new ?JButton( " 土 " ));
???????? this .add(button);
???????? this .add( new ?JButton( " 風 " ));
???????? this .add( new ?JButton( " 雨 " ));
???????? this .add( new ?JButton( " 雷 " ));
???????? this .add( new ?JButton( " 電 " ));
???????? this .setSize( 360 ,? 200 );
???????? this .setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
???????? this .setLocationRelativeTo( null );
???????? this .setVisible( true );
????}
???? public ? static ? void ?main(String?args[])?{
???????? new ?AzHOpaqueToolTipExample();
????}
???? public ? final ? static ? int ?NOT_OPAQUE? = ? 0 ; // ?不透明
???? public ? final ? static ? int ?HALF_OPAQUE? = ? 1 ; // ?半透明
???? public ? final ? static ? int ?OPAQUE? = ? 2 ; // ?透明(有邊)
???? public ? final ? static ? int ?COMPLETE_OPAQUE? = ? 3 ; // ?完全透明
???? class ?HalfOpaqueToolTip? extends ?JPanel? implements ?MouseListener?{
???????? private ?Color?backGroundCol? = ? null ;
???????? private ?Color?foregroundColorCol? = ? null ;
???????? private ?JFrame?frame? = ? null ;
???????? private ?String?content? = ? null ;
???????? private ? int ?opaqueT? = ? 0 ;
???????? public ?HalfOpaqueToolTip(String?tooltipContent,?Color?backGroundColor,
????????????????Color?foregroundColor,?Color?borderColor,? int ?opaqueType,
????????????????JFrame?frame)?{
????????????content? = ?tooltipContent;
????????????backGroundCol? = ?backGroundColor;
????????????foregroundColorCol? = ?foregroundColor;
????????????opaqueT? = ?opaqueType;
???????????? /*
?????????????*?調整tooltip的大小
????????????? */
???????????? int ?fristRowIndex? = ?tooltipContent.indexOf( " \n " );
????????????String?tooltipStr? = ? "" ;
???????????? if ?(fristRowIndex? > ? 0 )?{
????????????????tooltipStr? = ?tooltipContent.substring( 0 ,?tooltipContent
????????????????????????.indexOf( " \n " ));
????????????}? else ?{
????????????????tooltipStr? = ?tooltipContent;
????????????}
???????????? this
????????????????????.setSize(tooltipStr.length()? * ? this .getFont().getSize()
???????????????????????????? + ? 20 ,? new ?StringTokenizer(tooltipContent,? " \n " )
????????????????????????????.countTokens()? * ? 20 ? + ? 15 ); // ?左右各留10的空白空間
???????????? this .setOpaque( false );
???????????? if ?(opaqueType? != ?COMPLETE_OPAQUE)?{
???????????????? this .setBorder( new ?LineBorder(borderColor));
????????????}
???????????? this .setVisible( false );
???????????? // ?將本控件置于界面的最頂層
????????????frame.getLayeredPane().add( this ,?JLayeredPane.POPUP_LAYER);
???????????? this .frame? = ?frame;
????????}
???????? protected ? void ?paintComponent(Graphics?g)?{
???????????? super .paintComponent(g);
????????????Graphics2D?g2d? = ?(Graphics2D)?g;
???????????? // ?開始畫平滑的圖形
????????????g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
????????????????????RenderingHints.VALUE_ANTIALIAS_ON);
????????????g2d.setColor(backGroundCol);
???????????? if ?(opaqueT? == ?HALF_OPAQUE)?{
???????????????? // ?將控件的透明度設置為60%
????????????????AlphaComposite?composite? = ?AlphaComposite.getInstance(
????????????????????????AlphaComposite.SRC_OVER,? 60 ? / ? 100.0F );
????????????????g2d.setComposite(composite);
????????????????g2d.fill( new ?RoundRectangle2D.Float( 0 ,? 0 ,? this .getWidth(),? this
????????????????????????.getHeight(),? 0 ,? 0 ));
????????????}? else ? if ?(opaqueT? == ?NOT_OPAQUE)?{
???????????????? // ?將控件的透明度設置為不透明
????????????????AlphaComposite?composite? = ?AlphaComposite.getInstance(
????????????????????????AlphaComposite.SRC_OVER,? 100 ? / ? 100.0F );
????????????????g2d.setComposite(composite);
????????????????g2d.fill( new ?RoundRectangle2D.Float( 0 ,? 0 ,? this .getWidth(),? this
????????????????????????.getHeight(),? 0 ,? 0 ));
????????????}
????????????g2d.setColor(foregroundColorCol);
???????????? // ?恢復到默認情況
????????????g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
????????????????????RenderingHints.VALUE_ANTIALIAS_OFF);
???????????? /*
?????????????*?畫上字符內容
????????????? */
????????????StringTokenizer?contentTokenizer? = ? new ?StringTokenizer(content,
???????????????????? " \n " );
???????????? int ?contentStartY? = ? 20 ;
???????????? while ?(contentTokenizer.hasMoreElements())?{
????????????????g2d.drawString(contentTokenizer.nextToken(),? 10 ,?contentStartY);
????????????????contentStartY? = ?contentStartY? + ? 20 ;
????????????}
????????}
???????? public ? void ?mouseEntered(MouseEvent?e)?{
????????????Point?p? = ?e.getLocationOnScreen();
????????????SwingUtilities.convertPointFromScreen(p,?e.getComponent()
????????????????????.getParent());
???????????? if ?(p.x? + ? this .getWidth()? > ?frame.getX())?{
???????????????? this .setLocation( new ?Point(p.x? - ? this .getWidth(),?p.y));
????????????}? else ?{
???????????????? this .setLocation( new ?Point(p.x,?p.y));
????????????}
???????????? this .setVisible( true );
????????}
???????? public ? void ?mouseExited(MouseEvent?e)?{
???????????? this .setVisible( false );
????????}
???????? public ? void ?mouseClicked(MouseEvent?e)?{
????????}
???????? public ? void ?mousePressed(MouseEvent?e)?{
????????}
???????? public ? void ?mouseReleased(MouseEvent?e)?{
????????}
????}
}
import ?java.awt.AlphaComposite;
import ?java.awt.Color;
import ?java.awt.FlowLayout;
import ?java.awt.Graphics;
import ?java.awt.Graphics2D;
import ?java.awt.Point;
import ?java.awt.RenderingHints;
import ?java.awt.event.MouseEvent;
import ?java.awt.event.MouseListener;
import ?java.awt.geom.RoundRectangle2D;
import ?java.util.StringTokenizer;
import ?javax.swing.JButton;
import ?javax.swing.JFrame;
import ?javax.swing.JLayeredPane;
import ?javax.swing.JPanel;
import ?javax.swing.SwingUtilities;
import ?javax.swing.border.LineBorder;
/**
?*? @author ?Azure
?*? @version ?1.0?13/03/07
? */
public ? class ?AzHOpaqueToolTipExample? extends ?JFrame?{
???? public ?AzHOpaqueToolTipExample()?{
????????init();
????}
???? public ? void ?init()?{
????????HalfOpaqueToolTip?tooltip? = ? new ?HalfOpaqueToolTip(
???????????????? " 我們還能不能能不能再見面\n我在佛前苦苦求了幾千年\n當我在踏過這條奈何橋之前\n讓我再吻一吻你的臉 " ,
???????????????? new ?Color( 250 ,? 250 ,? 200 ),?Color.RED,?Color.BLACK,?OPAQUE,? this );
????????JButton?button? = ? new ?JButton( " swing " );
????????button.addMouseListener(tooltip);
???????? this .setLayout( new ?FlowLayout());
???????? this .add( new ?JButton( " 金 " ));
???????? this .add( new ?JButton( " 木 " ));
???????? this .add( new ?JButton( " 水 " ));
???????? this .add( new ?JButton( " 火 " ));
???????? this .add( new ?JButton( " 土 " ));
???????? this .add(button);
???????? this .add( new ?JButton( " 風 " ));
???????? this .add( new ?JButton( " 雨 " ));
???????? this .add( new ?JButton( " 雷 " ));
???????? this .add( new ?JButton( " 電 " ));
???????? this .setSize( 360 ,? 200 );
???????? this .setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
???????? this .setLocationRelativeTo( null );
???????? this .setVisible( true );
????}
???? public ? static ? void ?main(String?args[])?{
???????? new ?AzHOpaqueToolTipExample();
????}
???? public ? final ? static ? int ?NOT_OPAQUE? = ? 0 ; // ?不透明
???? public ? final ? static ? int ?HALF_OPAQUE? = ? 1 ; // ?半透明
???? public ? final ? static ? int ?OPAQUE? = ? 2 ; // ?透明(有邊)
???? public ? final ? static ? int ?COMPLETE_OPAQUE? = ? 3 ; // ?完全透明
???? class ?HalfOpaqueToolTip? extends ?JPanel? implements ?MouseListener?{
???????? private ?Color?backGroundCol? = ? null ;
???????? private ?Color?foregroundColorCol? = ? null ;
???????? private ?JFrame?frame? = ? null ;
???????? private ?String?content? = ? null ;
???????? private ? int ?opaqueT? = ? 0 ;
???????? public ?HalfOpaqueToolTip(String?tooltipContent,?Color?backGroundColor,
????????????????Color?foregroundColor,?Color?borderColor,? int ?opaqueType,
????????????????JFrame?frame)?{
????????????content? = ?tooltipContent;
????????????backGroundCol? = ?backGroundColor;
????????????foregroundColorCol? = ?foregroundColor;
????????????opaqueT? = ?opaqueType;
???????????? /*
?????????????*?調整tooltip的大小
????????????? */
???????????? int ?fristRowIndex? = ?tooltipContent.indexOf( " \n " );
????????????String?tooltipStr? = ? "" ;
???????????? if ?(fristRowIndex? > ? 0 )?{
????????????????tooltipStr? = ?tooltipContent.substring( 0 ,?tooltipContent
????????????????????????.indexOf( " \n " ));
????????????}? else ?{
????????????????tooltipStr? = ?tooltipContent;
????????????}
???????????? this
????????????????????.setSize(tooltipStr.length()? * ? this .getFont().getSize()
???????????????????????????? + ? 20 ,? new ?StringTokenizer(tooltipContent,? " \n " )
????????????????????????????.countTokens()? * ? 20 ? + ? 15 ); // ?左右各留10的空白空間
???????????? this .setOpaque( false );
???????????? if ?(opaqueType? != ?COMPLETE_OPAQUE)?{
???????????????? this .setBorder( new ?LineBorder(borderColor));
????????????}
???????????? this .setVisible( false );
???????????? // ?將本控件置于界面的最頂層
????????????frame.getLayeredPane().add( this ,?JLayeredPane.POPUP_LAYER);
???????????? this .frame? = ?frame;
????????}
???????? protected ? void ?paintComponent(Graphics?g)?{
???????????? super .paintComponent(g);
????????????Graphics2D?g2d? = ?(Graphics2D)?g;
???????????? // ?開始畫平滑的圖形
????????????g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
????????????????????RenderingHints.VALUE_ANTIALIAS_ON);
????????????g2d.setColor(backGroundCol);
???????????? if ?(opaqueT? == ?HALF_OPAQUE)?{
???????????????? // ?將控件的透明度設置為60%
????????????????AlphaComposite?composite? = ?AlphaComposite.getInstance(
????????????????????????AlphaComposite.SRC_OVER,? 60 ? / ? 100.0F );
????????????????g2d.setComposite(composite);
????????????????g2d.fill( new ?RoundRectangle2D.Float( 0 ,? 0 ,? this .getWidth(),? this
????????????????????????.getHeight(),? 0 ,? 0 ));
????????????}? else ? if ?(opaqueT? == ?NOT_OPAQUE)?{
???????????????? // ?將控件的透明度設置為不透明
????????????????AlphaComposite?composite? = ?AlphaComposite.getInstance(
????????????????????????AlphaComposite.SRC_OVER,? 100 ? / ? 100.0F );
????????????????g2d.setComposite(composite);
????????????????g2d.fill( new ?RoundRectangle2D.Float( 0 ,? 0 ,? this .getWidth(),? this
????????????????????????.getHeight(),? 0 ,? 0 ));
????????????}
????????????g2d.setColor(foregroundColorCol);
???????????? // ?恢復到默認情況
????????????g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
????????????????????RenderingHints.VALUE_ANTIALIAS_OFF);
???????????? /*
?????????????*?畫上字符內容
????????????? */
????????????StringTokenizer?contentTokenizer? = ? new ?StringTokenizer(content,
???????????????????? " \n " );
???????????? int ?contentStartY? = ? 20 ;
???????????? while ?(contentTokenizer.hasMoreElements())?{
????????????????g2d.drawString(contentTokenizer.nextToken(),? 10 ,?contentStartY);
????????????????contentStartY? = ?contentStartY? + ? 20 ;
????????????}
????????}
???????? public ? void ?mouseEntered(MouseEvent?e)?{
????????????Point?p? = ?e.getLocationOnScreen();
????????????SwingUtilities.convertPointFromScreen(p,?e.getComponent()
????????????????????.getParent());
???????????? if ?(p.x? + ? this .getWidth()? > ?frame.getX())?{
???????????????? this .setLocation( new ?Point(p.x? - ? this .getWidth(),?p.y));
????????????}? else ?{
???????????????? this .setLocation( new ?Point(p.x,?p.y));
????????????}
???????????? this .setVisible( true );
????????}
???????? public ? void ?mouseExited(MouseEvent?e)?{
???????????? this .setVisible( false );
????????}
???????? public ? void ?mouseClicked(MouseEvent?e)?{
????????}
???????? public ? void ?mousePressed(MouseEvent?e)?{
????????}
???????? public ? void ?mouseReleased(MouseEvent?e)?{
????????}
????}
}