swing storm

          java桌面應用

          導航

          <2007年3月>
          25262728123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          統計

          常用鏈接

          留言簿(15)

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          swing components-半透明ToolTip

          效果:
          AzHOpaqueToolTip.gifAzHOpaqueToolTip1.gif
          AzHOpaqueToolTip2.gifAzHOpaqueToolTip3.gif

          代碼:

          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)?{

          ????????}
          ????}

          }

          posted on 2007-03-13 18:32 azure 閱讀(3654) 評論(4)  編輯  收藏

          評論

          # re: swing components-半透明ToolTip 2007-03-13 19:07 BeanSoft

          Java 2D 畫圖值得關注.. 目前 SWT 這方面還是有點弱.  回復  更多評論   

          # re: swing components-半透明ToolTip 2007-03-13 19:19 山風小子

          博主,您好!
          以后貼代碼時能否不要附帶‘展開’功能和‘行號’,這令我哭笑不得??吹胶玫拇a想自己運行一下,復制來的都是帶'012..9'的代碼,還要用ultraedit來取范圍,去除行號。先謝謝了 :)  回復  更多評論   

          # re: swing components-半透明ToolTip 2007-03-15 00:56 催月淚

          受不了!強!強!強!UI是我的最愛......
          你住哪呀?你那里人?你吃了沒?  回復  更多評論   

          # re: swing components-半透明ToolTip 2007-03-15 08:58 小石頭

          頂呀頂呀頂!經典!  回復  更多評論   


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 新巴尔虎右旗| 治县。| 海宁市| 普宁市| 疏附县| 洪江市| 杭锦旗| 龙川县| 乌兰浩特市| 吐鲁番市| 荥经县| 镶黄旗| 淮滨县| 大姚县| 保山市| 福泉市| 思茅市| 江北区| 东方市| 文昌市| 柘城县| 明光市| 广德县| 鄂托克旗| 海林市| 武乡县| 灵寿县| 广西| 饶平县| 汪清县| 犍为县| 渑池县| 两当县| 绩溪县| 大丰市| 克什克腾旗| 湄潭县| 科技| 大悟县| 隆昌县| 定日县|