Java JFrame和可见性:淡出和截图的问题

Java JFrame和可见性:淡出和截图的问题,java,swing,jframe,screenshot,visibility,Java,Swing,Jframe,Screenshot,Visibility,在通过按下部署到JFrame中的按钮执行的操作(即方法)中,我想隐藏java应用程序,然后获取屏幕截图。最后,在截图完成后,我需要使JFrame可见 方法如下: public void myButtonPressedAction(){ //Hiding the JFrame this.setVisible(false); //Now I use Robot to get a screenshot using another method

在通过按下部署到
JFrame
中的按钮执行的操作(即方法)中,我想隐藏java应用程序,然后获取屏幕截图。最后,在截图完成后,我需要使
JFrame
可见

方法如下:

  public void myButtonPressedAction(){
       //Hiding the JFrame
       this.setVisible(false);
       //Now I use Robot to get a screenshot using another method
       //not reported for simplicity
       myMethodToGetScreenshot();
       //Making the JFrame visible
       this.setVisible(true);
  }
发生的情况是,一旦可见性设置为false,应用程序开始变得不可见,我立即得到屏幕截图:不幸的是,屏幕截图在淡出时也捕获了
JFrame
(即,它将变得不可见,
isVisible
方法返回
true
,但
JFrame
并非完全不可见)

一个可能的解决方案是插入一个计时器,在调用
setVisible(false)
和调用
myMethodToGetScreenshot()
之间设置一个延迟。但是,假设系统忙,延迟可能被低估;相反,更大的延迟会使我的应用程序变慢

如何获得准确的时间瞬间,使
JFrame
完全淡出,即它确实不可见

编辑

这将在构造函数中初始化:

String myPath= ...;//here I have a String that represent a path to a folder.
JPEGImageWriteParam JPEG_PARAMS_BEST_QUALITY = new JPEGImageWriteParam(null);
JPEG_PARAMS_BEST_QUALITY.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
JPEG_PARAMS_BEST_QUALITY.setCompressionQuality(1f);
这是myMethodToGetScreenshot()的代码。:

这是我得到的截图。你可以看到JFrame逐渐消失


您可以使用
setExtendedState

//Minimize the JFrame
this.setExtendedState(JFrame.ICONIFIED);
//Now I use Robot to get a screenshot using another method
//not reported for simplicity
myMethodToGetScreenshot();
//Restore the JFrame
this.setExtendedState(JFrame.NORMAL);

您可以使用
setExtendedState

//Minimize the JFrame
this.setExtendedState(JFrame.ICONIFIED);
//Now I use Robot to get a screenshot using another method
//not reported for simplicity
myMethodToGetScreenshot();
//Restore the JFrame
this.setExtendedState(JFrame.NORMAL);

然后延迟一些时间。你可以使用

下面是一个小演示:

import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class ScreenshotDemo {
    JFrame frame = new JFrame();
    JButton button = new JButton("Catch the screenshot");
    Timer timer;
    Robot robot;
    JLabel label = new JLabel();

    public ScreenshotDemo() {
        try {
            robot = new Robot();
        } catch (AWTException e1) {
            e1.printStackTrace();
        }

        // Keeps frame disposed for 3 seconds
        timer = new Timer(3000, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Rectangle size = new Rectangle(Toolkit.getDefaultToolkit()
                        .getScreenSize());
                Image image = robot.createScreenCapture(size);
                label.setIcon(new ImageIcon(image));
                frame.setVisible(true);
            }
        });
        timer.setRepeats(false);

        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                frame.setVisible(false);
                timer.start();
            }
        });

        frame.add(button, BorderLayout.NORTH);
        frame.add(label, BorderLayout.CENTER);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        // frame.pack();
        frame.setSize(1024, 768);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new ScreenshotDemo();
            }
        });
    }

}

基本上,您将隐藏帧一段时间(在本演示中为3秒)。隐藏帧时,您将拍摄快照。

然后放置一些延迟时间。您可以使用

下面是一个小演示:

import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class ScreenshotDemo {
    JFrame frame = new JFrame();
    JButton button = new JButton("Catch the screenshot");
    Timer timer;
    Robot robot;
    JLabel label = new JLabel();

    public ScreenshotDemo() {
        try {
            robot = new Robot();
        } catch (AWTException e1) {
            e1.printStackTrace();
        }

        // Keeps frame disposed for 3 seconds
        timer = new Timer(3000, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Rectangle size = new Rectangle(Toolkit.getDefaultToolkit()
                        .getScreenSize());
                Image image = robot.createScreenCapture(size);
                label.setIcon(new ImageIcon(image));
                frame.setVisible(true);
            }
        });
        timer.setRepeats(false);

        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                frame.setVisible(false);
                timer.start();
            }
        });

        frame.add(button, BorderLayout.NORTH);
        frame.add(label, BorderLayout.CENTER);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        // frame.pack();
        frame.setSize(1024, 768);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new ScreenshotDemo();
            }
        });
    }

}

基本上,您将隐藏帧一段时间(在本演示中为3秒)。当帧被隐藏时,您将拍摄快照。

我将尝试ComponentListener(假设此代码属于JFrame扩展类的成员):


我将尝试一下ComponentListener(假设此代码属于JFrame扩展类的成员):



您必须在隐藏帧后和拍摄屏幕截图之前添加合理的延迟。最简单的方法是在之前插入调用。

您必须在隐藏帧后和拍摄屏幕截图之前添加合理的延迟。最简单的方法是在调用
setVisible(false)之后插入对之前的调用。

isVisible方法返回
true
未更改!淡出时屏幕截图仍捕获JFrame。我不知道淡出是由Java控制还是由OS控制!
我不知道淡出是由Java控制还是由OS控制!
。它由OS控制。请参阅我的更新..我希望这是您的helpGreat!这个解决方案真的很聪明!正如我所说,在调用
setVisible(false)之后
isVisible方法返回
true
未更改!淡出时屏幕截图仍捕获JFrame。我不知道淡出是由Java控制还是由OS控制!
我不知道淡出是由Java控制还是由OS控制!
。它由OS控制。请参阅我的更新..我希望这是您的helpGreat!这个解决方案真的很聪明!这是我想到的第一个解决方案,你可以从我的问题中读到。正如我指出的,这种方法有一些缺点。但是,谢谢你!@mat_boy这是一个有效的解决方案,我刚刚测试过。只需将你的代码放在Timers actionPerformed方法中(在frame.setVisible(true)之前)瞧!+1的努力。我认为这个答案更优雅。不是吗?这是我想到的第一个解决方案,你可以从我的问题中读到。正如我指出的,这种方法有一些缺点。但是,谢谢你!@mat_boy这是一个可行的解决方案,我刚刚测试过。只需将你的代码放在Timers actionPerformed Method中d(在frame.setVisible(true)之前)和瞧!+1的努力。我认为这个答案更优雅。不是吗?我刚才说使用times是一个可能的解决方案,我希望避免(想象一下当机器忙得很慢时会发生什么)。但是,您的答案与提供的答案相似,但更简洁。我喜欢它!+1,因为答案简洁。如果没有人会回答这个问题而不使用时间,我将接受这个问题!我刚才说使用时间是一个可能的解决方案,我会避免(想象一下当机器繁忙而速度较慢时会发生什么)。但是,您的答案与提供的答案相似,但更紧凑。我喜欢!+1,因为答案紧凑。如果没有人不使用times来回答此问题,我将接受此答案!