预览效果图:
源代码: ↓
/*
* 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;
// 变量声明结束
}