【问题标题】:display screenshots and split them into two realtime显示屏幕截图并将它们实时拆分为两个
【发布时间】:2015-04-22 20:36:31
【问题描述】:

我是 Java 新手。

我正在尝试显示当前屏幕的屏幕截图并实时更新。 如果我只显示一次它工作正常 但是对于我的大学项目,我应该在两个相邻的屏幕上显示相同的屏幕截图。

我无法弄清楚如何实现这一点。

我找到了两个代码,并试图将它们一起实现,以便获得所需的结果。 http://www.ivoronline.com/Coding/Languages/JAVA/Tutorials/JAVA%20-%20File%20-%20Image%20-%20Display%20Multiple%20Images.php

Capturing and displaying an image using Java

这就是我现在的代码。

     import javax.swing.*;

     import java.awt.Rectangle;
     import java.awt.Robot;
     import java.awt.image.BufferedImage;

     public class OrionCode_v02 {

     public static void main(String[] args) throws Exception {
     Robot robot = new Robot();

    //JPanel panel = new JPanel();

    JFrame theGUI = new JFrame();
    //theGUI.getContentPane().add (panel);
    theGUI.setTitle("TestApp");
    theGUI.setSize(640, 720);        
    theGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    theGUI.setVisible(true);

    JLabel picLabel = new JLabel();
    JLabel picLabel2 = new JLabel();
    theGUI.add(picLabel);
    theGUI.add(picLabel2);

    while (true) 
    {
        BufferedImage screenShot = robot.createScreenCapture(new Rectangle(0,0,320,720)); // x-coord, y-coord, width, height
        BufferedImage screenShot1 = robot.createScreenCapture(new Rectangle(320,0,320,720)); // x-coord, y-coord, width, height

        picLabel.setIcon(new ImageIcon(screenShot1)); 
        picLabel2.setIcon(new ImageIcon(screenShot));

        theGUI.add(picLabel);
        theGUI.add(picLabel2);


      /*  
        panel.add (new JLabel (new ImageIcon (screenShot)));
        panel.add (new JLabel (new ImageIcon (screenShot1)));
    */
    }
}

}

它不显示屏幕截图1。 只显示截图。 如果我交换 picLabel.setIcon 的两个命令,那么它会显示 screenshot1。

我需要同时显示两个屏幕截图。 我该怎么办?

【问题讨论】:

    标签: java screenshot awtrobot


    【解决方案1】:

    JFrame 默认有BorderLayout,这使得add(Component) 方法默认总是将组件添加到框架的中心部分,替换已经放置在那里的任何组件。所以框架中只有picLabel2。您可以使用重载add将组件添加到特定部分:

    theGUI.add(picLabel, BorderLayout.WEST);
    theGUI.add(picLabel2, BorderLayout.EAST);
    

    您的循环中还有不必要的add() 调用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多