z.En(zenwong)用NetBeans写的Java Swing透明窗体 第2版 (模仿Vista磨沙玻璃效果)

11 篇文章 0 订阅

 

 

预览效果图:

 



源代码: ↓

 

 

 

/*
 * NewJFrame.java
 *
 * Created on 2007年10月27日, 下午5:23
 */

package javaapplication1;
import java.awt.Robot;
import java.awt.*;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.UIManager;
import javax.swing.*;
import java.awt.image.*;
import java.awt.color.*;
/**
 *
 * @author  mm
 */
public class NewJFrame extends javax.swing.JFrame {
    boolean tf=true;
    /** Creates new form NewJFrame */
    public NewJFrame() {//構造方法
        this.setUndecorated(true);//去掉JFrame的标题栏
        Image ico=Toolkit.getDefaultToolkit().getImage("g:/h2.jpg");
        this.setIconImage(ico);
        try {
            UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");//UI设置
            //UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());//windows樣式
        }catch (Exception ex) {
      javax.swing.JOptionPane.showMessageDialog(this,"發生UI加載錯誤,您可能無法看到程式預想的UI效果,錯誤信息如下:/n"+ex.toString());
        }
        initComponents();
        this.setLocation(100,100);
    }
   
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" 生成的代码 ">                         
    private void initComponents() {
        jlb = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        addWindowFocusListener(new java.awt.event.WindowFocusListener() {
            public void windowGainedFocus(java.awt.event.WindowEvent evt) {
                formWindowGainedFocus(evt);
            }
            public void windowLostFocus(java.awt.event.WindowEvent evt) {
                formWindowLostFocus(evt);
            }
        });
        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentMoved(java.awt.event.ComponentEvent evt) {
                formComponentMoved(evt);
            }
            public void componentResized(java.awt.event.ComponentEvent evt) {
                formComponentResized(evt);
            }
        });

        jlb.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(128, 128, 128)));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jlb, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 289, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jlb, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 762, Short.MAX_VALUE)
        );
        pack();
    }// </editor-fold>                       

    private void formWindowLostFocus(java.awt.event.WindowEvent evt) {                                    
// TODO 将在此处添加您的处理代码://失去焦點
       
    }                                   

    private void formWindowGainedFocus(java.awt.event.WindowEvent evt) {                                      
// TODO 将在此处添加您的处理代码://得到焦點
        this.selectBgimg();
       
    }                                     

    private void formComponentResized(java.awt.event.ComponentEvent evt) {                                     
// TODO 将在此处添加您的处理代码://儅窗口改變大小
       
        this.selectBgimg();
    }                                    

    private void formComponentMoved(java.awt.event.ComponentEvent evt) {                                   
// TODO 将在此处添加您的处理代码://儅窗口移動
        if(this.tf=false){
            this.tf=true;
        this.selectBgimg();
        }else{
            this.tf=false;
        }
    }                                  

   

   
    private void selectBgimg(){//截屏代码
          try {
        Point p=this.getLocation();//移動方式,用p
        this.setLocation(-2000,-2000);//移動方式
              //this.setVisible(false);//隱藏方式,用this
       Robot rbt = new Robot();
   Toolkit tk = Toolkit.getDefaultToolkit();
   Dimension dim = tk.getScreenSize();
   BufferedImage background = rbt.createScreenCapture(new Rectangle((int)p.getX()+1,(int)p.getY()+1, this.getWidth()-2,this.getHeight()-2));
   background = rbt.createScreenCapture(new Rectangle((int)p.getX()+1,(int)p.getY()+1, this.getWidth()-2,this.getHeight()-2));
   background = rbt.createScreenCapture(new Rectangle((int)p.getX()+1,(int)p.getY()+1, this.getWidth()-2,this.getHeight()-2));
   background = rbt.createScreenCapture(new Rectangle((int)p.getX()+1,(int)p.getY()+1, this.getWidth()-2,this.getHeight()-2));
  
   float[] data={
 0.0625f,0.125f,0.0625f,
 0.125f,0.125f,0.125f,
 0.0625f,0.125f,0.0625f,
       
       

       
        //0.170f,0.06f,0.170f,
        //0.06f,0.06f,0.06f,
        //0.170f,0.06f,0.170f,
};
Kernel kernel= new Kernel(3,3,data);

ConvolveOp co= new ConvolveOp(kernel,ConvolveOp.EDGE_NO_OP,null);
BufferedImage background2=null;
background2=co.filter(background,background2);
  
   ImageIcon bg=new ImageIcon(background2);
   jlb.setIcon(bg);  
  
   //this.setVisible(true);//隱藏方式
        this.setLocation(p);//移動方式
     } catch (Exception ex) {
      javax.swing.JOptionPane.showMessageDialog(this,"透明效果發生錯誤,程序無法啓動,錯誤信息如下:/n"+ex.toString());
   }
    }
   
   
       
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new NewJFrame().setVisible(true);
            }
        });
    }
   
    // 变量声明 - 不进行修改                    
    private javax.swing.JLabel jlb;
    // 变量声明结束                  
   
}
 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值