最近上课的时候老师不给我们传ppt和代码了,说传了你们就不自己写了,全都copy了,所以想写一个屏幕截图的工具,用jdk1.6
发现awt包中有一个Robot类,其中的createScreenCapture(Rectangle rec)可以截取屏幕内容,使用awt中的Tookit.getDefaultTookit().getScreenSize(),得到Dimension,然后new Rectangle(Dimension d)就可以得到当前屏幕的Rectangle,是全屏幕的,这样就可以得到一个BufferedImage,使用ImageIO.write(BufferedImage buf,String type,File output),可以得到一个相应格式的图片(支持很多像bmp,jpg,jpeg,png等),我想程序开始的时候,出现一个JFrame,里面有一个JTextField,一个JButton,JTextField用于输入截图保存的路径,JButton用于保存路径并且使窗口消失(setVisible(false),不是关闭),单击标题栏关闭按钮后,用SysTray 和 TrayIcon 在任务栏托盘里加一个图标,右键有弹出菜单,就一项关闭,可以使程序最终关闭(dispose,System.exit(0)),但是最难的问题是怎样在窗口隐藏后监听所有的Windows的键盘事件,当用户按下crtl+9时能,执行保存图片的工作。这个问题在google上搜索了半天都没找到答案,希望高手们能够答复我,小弟先在这里谢过了。下面两个类是已经写好的代码:import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;import javax.imageio.ImageIO;
public class MethodRelated {
Robot robot = null;
String imagePath = null;
Rectangle rec = null;
public MethodRelated(){
try {
robot = new Robot();
} catch (AWTException e) {
e.printStackTrace();
}
rec = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
}
public void setPath(String inputPath){
imagePath = inputPath;
}
public void saveToImageFile(){
BufferedImage img = robot.createScreenCapture(rec);
String fileName = new SimpleDateFormat("yyyy-MM-dd-hh-mm-ss").format(new Date()).toString();
File outputFile = new File(imagePath+fileName);
try {
ImageIO.write(img,"PNG",outputFile);
} catch (IOException e) {
e.printStackTrace();
}
}
}import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class UI {
JFrame f = new JFrame("设置界面");
JTextField text = new JTextField(15);
JButton btn = new JButton("设置");
MethodRelated methods = new MethodRelated();
MenuItem menuClose = new MenuItem("关闭");
public void setFrame(){
JLabel label = new JLabel("保存路径:");
f.setUndecorated(true);
f.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
f.setLayout(new FlowLayout());
f.add(label);
f.add(text);
f.add(btn);
f.setLocation(300,300);
f.setSize(200,150);
setSysTray();
setListeners();
f.setVisible(true);
text.grabFocus();
f.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
}
public void setSysTray(){
PopupMenu popup = new PopupMenu();
popup.add(menuClose);
menuClose.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
f.dispose();
System.exit(0);
}
});
SystemTray sys = SystemTray.getSystemTray();
Image i = Toolkit.getDefaultToolkit().getImage("icon.jpg");
TrayIcon icon = new TrayIcon(i,"屏幕截图",popup);
try {
sys.add(icon);
} catch (AWTException e1) {
e1.printStackTrace();
}
}
public void setListeners(){
btn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
methods.setPath(text.getText());
JOptionPane.showMessageDialog(null, "路径已设置,请单击关闭按钮隐藏窗口");
}
});
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
if(methods.imagePath==null){
JOptionPane.showMessageDialog(null, "您未设置路径,请设置");
text.grabFocus();
}else{
f.setVisible(false);
}
}
});
}
public static void main(String[] args){
new UI().setFrame();
}
}
剩下的问题就是监听系统按键调用保存图片的方法了。请高手指教,小弟不胜感激。很急。

解决方案 »

  1.   

    你需要从http://code.google.com/p/kbmonitor/downloads/list下载一个压缩包,把里面的jar文件和dll文件拷到你的classpath下
    下面的例子是用来监听键盘X按键的import java.awt.event.KeyEvent;import com.yovn.labs.KeyBoardMonitor;
    import com.yovn.labs.SystemKBListener;public class M {
    public static void main(String[] args) {
    KeyBoardMonitor monitor = KeyBoardMonitor.getInstance();
    System.out.println("1111");
    monitor.setSystemKBListener(new SystemKBListener() {
    public void keyDown(int arg0) {
    if (arg0 == KeyEvent.VK_X)
    System.out.println("sucesss"); } public void keyUp(int arg0) {
    } });
    monitor.enableMonitor();
    }
    }