你可以尝试使用类似的东西Robot#createScreenCapture
如果您不想捕获整个屏幕,您可以使用Component#getLocationOnScreen
查找组件在屏幕上的位置来代替...
public class CaptureScreen {
public static void main(String[] args) {
new CaptureScreen();
}
public CaptureScreen() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
final JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
frame.add(new JLabel("Smile :D"), gbc);
JButton capture = new JButton("Click");
capture.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
Robot robot = new Robot();
Rectangle bounds = frame.getBounds();
bounds.x -= 1;
bounds.y -= 1;
bounds.width += 2;
bounds.height += 2;
BufferedImage snapShot = robot.createScreenCapture(bounds);
ImageIO.write(snapShot, "png", new File("Snapshot.png"));
} catch (Exception ex) {
ex.printStackTrace();
}
}
});
frame.add(capture, gbc);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
使用示例Component#getLocationOnScreen
public class CaptureScreen {
public static void main(String[] args) {
new CaptureScreen();
}
public CaptureScreen() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
final JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
frame.add(new JLabel("Smile :D"), gbc);
JButton capture = new JButton("Click");
capture.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
Robot robot = new Robot();
Container panel = frame.getContentPane();
Point pos = panel.getLocationOnScreen();
Rectangle bounds = panel.getBounds();
bounds.x = pos.x;
bounds.y = pos.y;
bounds.x -= 1;
bounds.y -= 1;
bounds.width += 2;
bounds.height += 2;
BufferedImage snapShot = robot.createScreenCapture(bounds);
ImageIO.write(snapShot, "png", new File("Snapshot.png"));
} catch (Exception ex) {
ex.printStackTrace();
}
}
});
frame.add(capture, gbc);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
另一个例子展示Component#getLocationOnScreen
图像的主要原因 3 相同,是因为示例转储了根窗格、分层窗格和内容窗格......
public class CaptureScreen {
public static void main(String[] args) {
new CaptureScreen();
}
public void save(Component comp, File file) {
if (comp.isVisible()) {
try {
System.out.println(comp);
Robot robot = new Robot();
Rectangle bounds = new Rectangle(comp.getLocationOnScreen(), comp.getSize());
bounds.x -= 1;
bounds.y -= 1;
bounds.width += 2;
bounds.height += 2;
BufferedImage snapShot = robot.createScreenCapture(bounds);
ImageIO.write(snapShot, "png", file);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
private int layer;
public void capture(Container container) {
layer = 1;
captureLayers(container);
}
public void captureLayers(Container container) {
save(container, new File("SnapShot-" + layer + "-0.png"));
int thisLayer = layer;
int count = 1;
for (Component comp : container.getComponents()) {
if (comp instanceof Container) {
layer++;
captureLayers((Container) comp);
} else {
save(comp, new File("SnapShot-" + thisLayer + "-" + count + ".png"));
count++;
}
}
}
public CaptureScreen() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
final JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
frame.add(new JLabel("Smile :D"), gbc);
JButton capture = new JButton("Click");
capture.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
capture(frame);
}
});
frame.add(capture, gbc);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}