JAVA截屏程序(第二版)

本文介绍了一款改进后的屏幕截取软件,该软件修复了选区调整时的BUG,并新增了跟随鼠标提示、截图后主窗口隐藏等功能,支持多种图片格式保存。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

刚才把截屏程序放出去之后,收到了朋友BeanSoft 的建议,刚来这里就能认识他,很高兴,现在又改进了一下,改进的地方如下

1,修复了选择选区后,再改变大小时,当把右侧的边框拖过左侧或者左侧的边框拖到右侧或者上面的边框拖过下面,只会显示一条边框的BUG.
2,在没有截屏的时候,会有一条提示的字符串跟着鼠标走.
3,在点了截屏之后,主窗口会先隐藏起来,截完图后才会弹出来
4,去掉了截屏时的自定义鼠标显示,因为这样的话,源代码编译之后,运行会出问题,因为别人的电脑上没有我那个鼠标的图片.
5,增加了可保存的图片格式,现在可保存(JPG,GIF,PNG,BMP)格式

源代码附上,可以直接自己编译,打成JAR包.我这里也提供一下可执行JAR包的下载.


点击下载可执行的JAR包
ExpandedBlockStart.gif ContractedBlock.gif /**/ /*
InBlock.gif * CaptureScreen.java
InBlock.gif *
InBlock.gif * Created on 2007年8月30日, 下午12:46
InBlock.gif *
InBlock.gif * To change this template, choose Tools | Template Manager
InBlock.gif * and open the template in the editor.
ExpandedBlockEnd.gif 
*/

None.gif
None.gif
package  hadeslee.swing;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/** */ /**
InBlock.gif *
InBlock.gif * 
@author lbf
ExpandedBlockEnd.gif 
*/

None.gif
import  java.awt. * ;
None.gif
import  java.awt.event. * ;
None.gif
import  javax.swing. * ;
None.gif
import  java.io. * ;
None.gif
import  javax.imageio. * ;
None.gif
import  java.awt.image. * ;
ExpandedBlockStart.gifContractedBlock.gif
public   class  CaptureScreen  extends  JFrame  implements  ActionListener dot.gif {
InBlock.gif    
private JButton start,cancel,save;
InBlock.gif    
private JPanel c;
InBlock.gif    
private BufferedImage get;
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/** *//** Creates a new instance of CaptureScreen */
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public CaptureScreen() dot.gif{
InBlock.gif        
super("屏幕截取软件(第二版)");
InBlock.gif        initWindow();
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
private void initWindow()dot.gif{
InBlock.gif        start
=new JButton("开始截取");
InBlock.gif        cancel
=new JButton("退出");
InBlock.gif        save
=new JButton("保存");
InBlock.gif        save.setEnabled(
false);
InBlock.gif        save.addActionListener(
this);
InBlock.gif        start.addActionListener(
this);
InBlock.gif        cancel.addActionListener(
this);
InBlock.gif        JPanel buttonJP
=new JPanel();
InBlock.gif        c
=new JPanel(new BorderLayout());
InBlock.gif        JLabel jl
=new JLabel("屏幕截取",JLabel.CENTER);
InBlock.gif        JLabel jl1
=new JLabel("作者:千里冰封",JLabel.CENTER);
InBlock.gif        jl.setFont(
new Font("黑体",Font.BOLD,40));
InBlock.gif        jl1.setFont(
new Font("宋体",Font.BOLD,20));
InBlock.gif        jl.setForeground(Color.RED);
InBlock.gif        jl1.setForeground(Color.BLUE);
InBlock.gif        c.add(jl,BorderLayout.CENTER);
InBlock.gif        c.add(jl1,BorderLayout.SOUTH);
InBlock.gif        buttonJP.add(start);
InBlock.gif        buttonJP.add(save);
InBlock.gif        buttonJP.add(cancel);
InBlock.gif        
this.getContentPane().add(c,BorderLayout.CENTER);
InBlock.gif        
this.getContentPane().add(buttonJP,BorderLayout.SOUTH);
InBlock.gif        
this.setSize(300,300);
InBlock.gif        
this.setLocationRelativeTo(null);
InBlock.gif        
this.setVisible(true);
InBlock.gif        
this.setAlwaysOnTop(true);
InBlock.gif        
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
private void updates()dot.gif{
InBlock.gif        
this.setVisible(true);
ExpandedSubBlockStart.gifContractedSubBlock.gif        
if(get!=null)dot.gif{
InBlock.gif            save.setEnabled(
true);
InBlock.gif            ImageIcon ii
=new ImageIcon(get);
InBlock.gif            JLabel jl
=new JLabel(ii);
InBlock.gif            c.removeAll();
InBlock.gif            c.add(
new JScrollPane(jl),BorderLayout.CENTER);
InBlock.gif            SwingUtilities.updateComponentTreeUI(
this);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
private void doStart()dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
trydot.gif{
InBlock.gif            
this.setVisible(false);
InBlock.gif            Thread.sleep(
500);//睡500毫秒是为了让主窗完全不见
InBlock.gif
            Robot ro=new Robot();
InBlock.gif            Toolkit tk
=Toolkit.getDefaultToolkit();
InBlock.gif            Dimension di
=tk.getScreenSize();
InBlock.gif            Rectangle rec
=new Rectangle(0,0,di.width,di.height);
InBlock.gif            BufferedImage bi
=ro.createScreenCapture(rec);
InBlock.gif            JFrame jf
=new JFrame();
InBlock.gif            Temp temp
=new Temp(jf,bi,di.width,di.height);
InBlock.gif            jf.getContentPane().add(temp,BorderLayout.CENTER);
InBlock.gif            jf.setUndecorated(
true);
InBlock.gif            jf.setSize(di);
InBlock.gif            jf.setVisible(
true);
InBlock.gif            jf.setAlwaysOnTop(
true);
ExpandedSubBlockStart.gifContractedSubBlock.gif        }
 catch(Exception exe)dot.gif{
InBlock.gif            exe.printStackTrace();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }
 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
private void doSave()dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
trydot.gif{
InBlock.gif            JFileChooser jfc
=new JFileChooser(".");
InBlock.gif            jfc.addChoosableFileFilter(
new JPGfilter());
InBlock.gif            jfc.addChoosableFileFilter(
new PNGfilter());
InBlock.gif            jfc.addChoosableFileFilter(
new GIFfilter());
InBlock.gif            jfc.addChoosableFileFilter(
new BMPfilter());
InBlock.gif            
int i=jfc.showSaveDialog(this);
ExpandedSubBlockStart.gifContractedSubBlock.gif            
if(i==JFileChooser.APPROVE_OPTION)dot.gif{
InBlock.gif                File file
=jfc.getSelectedFile();
InBlock.gif                String about
="PNG";
InBlock.gif                String ext
=file.toString().toLowerCase();
InBlock.gif                javax.swing.filechooser.FileFilter ff
=jfc.getFileFilter();
ExpandedSubBlockStart.gifContractedSubBlock.gif                
if(ff instanceof JPGfilter)dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
if(!ext.endsWith(".jpg"))dot.gif{
InBlock.gif                        String ns
=ext+".jpg";
InBlock.gif                        file
=new File(ns);
InBlock.gif                        about
="JPG";
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockStart.gifContractedSubBlock.gif                }
 else if(ff instanceof PNGfilter)dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
if(!ext.endsWith(".png"))dot.gif{
InBlock.gif                        String ns
=ext+".png";
InBlock.gif                        file
=new File(ns);
InBlock.gif                        about
="PNG";
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockStart.gifContractedSubBlock.gif                }
else if(ff instanceof BMPfilter)dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
if(!ext.endsWith(".bmp"))dot.gif{
InBlock.gif                        String ns
=ext+".bmp";
InBlock.gif                        file
=new File(ns);
InBlock.gif                        about
="BMP";
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockStart.gifContractedSubBlock.gif                }
else if(ff instanceof GIFfilter)dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
if(!ext.endsWith(".gif"))dot.gif{
InBlock.gif                        String ns
=ext+".gif";
InBlock.gif                        file
=new File(ns);
InBlock.gif                        about
="GIF";
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

ExpandedSubBlockStart.gifContractedSubBlock.gif                
if(ImageIO.write(get,about,file))dot.gif{
InBlock.gif                    JOptionPane.showMessageDialog(
this,"保存成功!");
ExpandedSubBlockEnd.gif                }
 else
InBlock.gif                    JOptionPane.showMessageDialog(
this,"保存失败!");
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockStart.gifContractedSubBlock.gif        }
 catch(Exception exe)dot.gif{
InBlock.gif            exe.printStackTrace();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值