您的位置:首页 > 编程语言 > Java开发

java添加背景图片

2016-09-25 10:18 465 查看
JPANEL 类 , 重写 paintCompontent 类,在paintCompontent()方法中添加图片

 

 
class Welcome extends JPanel{
    private static final long serialVersionUID = 1L;
    intwidth=0, height=0;

   Welcome(int x, int y, int width, int height){

        this.width= width;

       this.height = height;

       this.setBounds(x, y, width, height);

}

 
@Override
public void paintComponent(Graphics g){
   Graphics2D g2d = (Graphics2D) g;
   ImageIcon icon = null;
    try{
       icon = new ImageIcon("filepath\\filename");
    } catch(Exception e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
    }
   g2d.drawImage(icon.getImage(), 0, 0,this.getSize().width,this.getSize().height, this);
   g2d.setColor(new Color(150, 150, 180));
   g2d.drawRect(0, 0, width-1, height-1);
   g2d.setColor(Color.RED);
   g2d.setFont(g.getFont().deriveFont(Font.BOLD|Font.ITALIC,28f));
   g2d.drawString("演示", 230, 60);
   g2d.setFont(g.getFont().deriveFont(Font.BOLD|Font.ITALIC,22f));
   g2d.drawString("WINLAB", 600, 90);
   //改变颜色和字体
   g2d.setColor(Color.BLUE);
   g2d.drawLine(590, 92, 690, 92);
  }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: