z.En(zenwong)用NetBeans写的Swing透明窗体

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.*;
/**
 *
 * @author  mm
 */
public class NewJFrame extends javax.swing.JFrame {
   
    /** Creates new form NewJFrame */
    public NewJFrame() {//構造方法
        //this.setUndecorated(true);//去掉JFrame的标题栏
       
       
        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));
        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);
            }
        });

        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.DEFAULT_SIZE, 706, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jlb, javax.swing.GroupLayout.DEFAULT_SIZE, 643, Short.MAX_VALUE)
        );
        pack();
    }// </editor-fold>                       

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

    private void formComponentMoved(java.awt.event.ComponentEvent evt) {                                   
// TODO 将在此处添加您的处理代码:
        selectBgimg();
    }                                  

   

   
    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();
   java.awt.Image background = rbt.createScreenCapture(new Rectangle((int)this.getX(),(int)this.getY(), this.getWidth(),this.getHeight()));
   background = rbt.createScreenCapture(new Rectangle((int)this.getX(),(int)this.getY(), this.getWidth(),this.getHeight()));
   background = rbt.createScreenCapture(new Rectangle((int)this.getX(),(int)this.getY(), this.getWidth(),this.getHeight()));
   background = rbt.createScreenCapture(new Rectangle((int)this.getX(),(int)this.getY(), this.getWidth(),this.getHeight()));
   ImageIcon bg=new ImageIcon(background);
   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;
    // 变量声明结束                  
   
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值