如何使用Java显示计算机的桌面?

时间:2013-01-20 18:39:35

标签: java screenshot

如何使用Java显示计算机的桌面(屏幕)?

1 个答案:

答案 0 :(得分:0)

如果您想拍摄截图,请尝试使用此代码

 if (argv.length > 0) {
            try {
                long time = Long.parseLong(argv[0]) * 1000L;
                System.out.println("ScreenShot in " + argv[0] + " second(s)...");
                Thread.sleep(time);
                System.out.println("Snap!");
            } catch (NumberFormatException nfe) {
                System.err.println(argv[0] + " is not a valid number of seconds.");
                System.exit(-1);
            }
        }
        Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
        Robot robot = new Robot();
        BufferedImage img = robot.createScreenCapture(new Rectangle(size));
        JFrame frame = new JFrame();
        frame.getContentPane().setLayout(new FlowLayout());
        frame.getContentPane().add(new JLabel(new ImageIcon(img)));

        frame.pack();
        frame.setVisible(true);