JFrame性能低下

时间:2016-02-07 12:30:47

标签: java jframe bufferedimage joptionpane awtrobot

JOptionPane.ShowInputDialog慢慢接受输入。要反映到文本框的用户键操作之间有半秒的差距。

此外,应用程序(.jar文件)需要更多内存。 75mb(应用程序使用JNA Windows低级别Keyhook事件)。

如何优化!

Robot robot = new Robot();
BufferedImage bi=robot.createScreenCapture(new Rectangle(0,25,1366,744));
JFrame frame = new JFrame();
frame.toFront();
frame.requestFocus();
frame.setAlwaysOnTop(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String name = JOptionPane.showInputDialog(frame, "Enter file name");
frame.dispose();
String fileName= dovalidateFile(name);
FileNameExtensionFilter filter = new FileNameExtensionFilter("PNG", ".png");
fileChooser.setFileFilter(filter);
fileChooser.setSelectedFile(new File (fileName));
int returnVal = fileChooser.showSaveDialog(null);
if ( returnVal == JFileChooser.APPROVE_OPTION )
{
    File file = fileChooser.getSelectedFile();
    file = validateFile(file);
    System.out.println(file);
    ImageIO.write(bi, "png", file);
}

1 个答案:

答案 0 :(得分:2)

new Thread(){
    public void run(){
        Robot robot = new Robot();
        BufferedImage bi=robot.createScreenCapture(new Rectangle(0,25,1366,744));
        JFrame frame = new JFrame();
        frame.toFront();
        frame.requestFocus();
        frame.setAlwaysOnTop(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        String name = JOptionPane.showInputDialog(frame, "Enter file name");
        frame.dispose();
        String fileName= dovalidateFile(name);
        FileNameExtensionFilter filter = new FileNameExtensionFilter("PNG", ".png");
        fileChooser.setFileFilter(filter);
        fileChooser.setSelectedFile(new File (fileName));
        int returnVal = fileChooser.showSaveDialog(null);
        if ( returnVal == JFileChooser.APPROVE_OPTION )
        {
            File file = fileChooser.getSelectedFile();
            final File validatedFile = validateFile(file);
            System.out.println(file);
            try{
                ImageIO.write(bi, "png", validatedFile);
            }catch(Excepion e){
                e.printStackTrace();
            }

        }
    }
}.start();