繼續牛刀(JTextField水印);

           

          玩Swing 沒點美感怎么行..嘿嘿 ^v^
          怎么能沉迷與JTextField那種白色的背景呢???太單調的..看不出像我這么有美感的人..怎能讓它顯示出我認為不好看的東西!!下面就來解決JTextFiled!! 為他添加水印!
          代碼很簡單:

          效果圖:


          文本也可以帥起來~~~~~~~

          import java.awt.Color;
          import java.awt.Graphics;
          import java.awt.Graphics2D;
          import java.awt.GridLayout;
          import java.awt.Rectangle;
          import java.awt.TexturePaint;
          import java.awt.image.BufferedImage;
          import java.io.File;
          import java.io.IOException;
          import javax.imageio.ImageIO;
          import javax.swing.JButton;
          import javax.swing.JFrame;
          import javax.swing.JTextField;

          public class WatermarkTextField extends JTextField {
            BufferedImage img;
            TexturePaint texture;
            
          public WatermarkTextField(File file) throws IOException //臨時就把異常往外拋了.
             super();                                                 //方法接收一個Image圖片文件.
             img = ImageIO.read(file);
             Rectangle rect 
          = new Rectangle(0,0,
          img.getWidth(
          null),img.getHeight(null));
             texture 
          = new TexturePaint(img, rect);
             setOpaque(
          false);
            }

            
          public void paintComponent(Graphics g) {
             Graphics2D g2 
          = (Graphics2D)g;
             g2.setPaint(texture);
             g.fillRect(
          0,0,getWidth(),getHeight());
             
          super.paintComponent(g);
            }

          //Main類我就不說了Java的基礎.
            public static void main(String[] args) throws Exception {
             JFrame frame 
          = new JFrame("Watermark JTextField Hack");
             frame.setLayout(
          new GridLayout(1,2));
             JTextField textfield 
          = new WatermarkTextField(new File("Image/水印.JPG"));
             textfield.setForeground(Color.white);
             textfield.setText(
          "A Text Field");
             textfield.setSize(
          120,30);
             frame.getContentPane().add(textfield);
             frame.getContentPane().add(
          new JButton("前面那家伙真漂亮"));
             frame.setSize(
          250,60);
             frame.setVisible(
          true);
            }

          }

          posted on 2008-05-19 16:49 相信 閱讀(703) 評論(0)  編輯  收藏 所屬分類: Swing文章

          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          導航

          統計

          公告

          不顯示applet

          常用鏈接

          留言簿(16)

          我參與的團隊

          隨筆檔案

          文章分類

          文章檔案

          新聞檔案

          相冊

          swingchina 專業搞Swing的網站

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 金沙县| 伊金霍洛旗| 项城市| 彰化县| 齐齐哈尔市| 辽宁省| 沧州市| 上栗县| 永川市| 东乡族自治县| 根河市| 东乡县| 张家界市| 张家港市| 德化县| 宝应县| 昔阳县| 满城县| 白玉县| 仪陇县| 五寨县| 德昌县| 莒南县| 开平市| 平阳县| 本溪市| 长寿区| 平湖市| 贵阳市| 灵丘县| 永州市| 唐山市| 吴川市| 甘泉县| 吴江市| 德兴市| 泸溪县| 泸州市| 同心县| 澄城县| 长阳|