swing storm

          java桌面應(yīng)用

          導(dǎo)航

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

          統(tǒng)計(jì)

          常用鏈接

          留言簿(15)

          隨筆檔案

          搜索

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          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當(dāng)我在踏過這條奈何橋之前\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( " 風(fēng) " ));
          ????????
          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;
          ????????????
          /*
          ?????????????*?調(diào)整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)?{
          ????????????????
          // ?將控件的透明度設(shè)置為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)?{
          ????????????????
          // ?將控件的透明度設(shè)置為不透明
          ????????????????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);
          ????????????
          // ?恢復(fù)到默認(rèn)情況
          ????????????g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
          ????????????????????RenderingHints.VALUE_ANTIALIAS_OFF);
          ????????????
          /*
          ?????????????*?畫上字符內(nèi)容
          ?????????????
          */
          ????????????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 閱讀(3655) 評(píng)論(4)  編輯  收藏

          評(píng)論

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

          Java 2D 畫圖值得關(guān)注.. 目前 SWT 這方面還是有點(diǎn)弱.  回復(fù)  更多評(píng)論   

          # re: swing components-半透明ToolTip 2007-03-13 19:19 山風(fēng)小子

          博主,您好!
          以后貼代碼時(shí)能否不要附帶‘展開’功能和‘行號(hào)’,這令我哭笑不得。看到好的代碼想自己運(yùn)行一下,復(fù)制來的都是帶'012..9'的代碼,還要用ultraedit來取范圍,去除行號(hào)。先謝謝了 :)  回復(fù)  更多評(píng)論   

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

          受不了!強(qiáng)!強(qiáng)!強(qiáng)!UI是我的最愛......
          你住哪呀?你那里人?你吃了沒?  回復(fù)  更多評(píng)論   

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

          頂呀頂呀頂!經(jīng)典!  回復(fù)  更多評(píng)論   


          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 班玛县| 灵武市| 常熟市| 桂阳县| 班戈县| 富民县| 吉安县| 渝北区| 邮箱| 阜南县| 余干县| 竹山县| 昭平县| 武宣县| 六枝特区| 唐山市| 元氏县| 皋兰县| 绿春县| 泌阳县| 舞钢市| 东阳市| 陈巴尔虎旗| 江达县| 团风县| 伊金霍洛旗| 内江市| 深圳市| 阳江市| 淮北市| 巴青县| 都昌县| 台江县| 浮山县| 邓州市| 比如县| 秦安县| 信阳市| 百色市| 麟游县| 锦屏县|