当前位置: 代码迷 >> Java相关 >> 来人挑错~~
  详细解决方案

来人挑错~~

热度:199   发布时间:2010-10-31 13:19:33.0
来人挑错~~
提示: 作者被禁止或删除 内容自动屏蔽
2010-10-31 05:19:33
linjx0123

等 级:贵宾
威 望:14
帖 子:279
专家分:1362
注 册:2006-4-7
  得分:20 
package hack.genius.com;

import javax.swing.*;
import java.awt.image.*;
import java.awt.*;
import java.io.*;
import javax.imageio.*;

public class WaterMarkTextField extends JTextField{
    BufferedImage img;
    TexturePaint texture;
   
    public WaterMarkTextField(File file) throws IOException{
        super();
        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) { //方法名错误
        super.paintComponent(g);
      
        Graphics2D g2 = (Graphics2D) g;
        g2.setPaint(texture);
        g.fillRect(0,0,getWidth(),getHeight()); //这两句话看不懂。其实觉得直接调用g.drawImage就行了
    }
   
    public static void main(String argv[]) throws Exception{
        JFrame frame = new JFrame("A text field");
        JTextField textfield = new WaterMarkTextField(
                new File("bg.png"));
        textfield.setText("Hello world");
        frame.getContentPane().add(textfield);
        frame.pack();
        frame.setVisible(true);
    }
}


----------------解决方案--------------------------------------------------------
这样的活动蛮好的,知识难度太高了,看不懂,有最简单的么
----------------解决方案--------------------------------------------------------
  相关解决方案