如何在EDT(Java Swing)上运行“另存为”菜单

时间:2013-12-27 20:42:47

标签: java multithreading swing joptionpane jfilechooser

(我正在将全屏Java application升级到在窗口中运行的Java Swing应用程序。)

基本类在EDT(事件调度线程)上作为线程运行,并使用invokeAndWait(this)调用。

活动渲染循环位于同一类的方法体中,但它作为EDT外部的单独线程运行。 (这证明了。)

按下相应的键时,暂停活动渲染循环并调用另存为菜单。 “另存为”菜单在EDT上运行,在Linux中看起来不错,但在Windows中看起来不太好。 Windows中的确认菜单(用于文件覆盖)为空白。

private void save_frame() {
    setVisible(false);
    try {invokeAndWait(new Runnable() {
        @Override
        public void run() {
            try {
                if (current_save_folder == null) {
                    current_save_folder = javax.swing.filechooser.FileSystemView.getFileSystemView().getDefaultDirectory().toString();
                }
                final JFileChooser saver = new JFileChooser();
                saver.setCurrentDirectory(new File(current_save_folder));
                saver.setDialogTitle("Save two frames and state as...");
                saver.setFileSelectionMode(JFileChooser.FILES_ONLY);
                saver.setAcceptAllFileFilterUsed(true);
                int approval = saver.showSaveDialog(getParent());
                File file = saver.getSelectedFile();
                if (approval == saver.APPROVE_OPTION && file != null) {
                    file = new File(file.getAbsolutePath());
                    current_save_folder = file.getParent();
                    File file_in_png = new File(file.getAbsolutePath());
                    File file_out_png = new File(file.getAbsolutePath());
                    File file_state = new File(file.getAbsolutePath());
                    String file_lowercase = file.getAbsolutePath().toLowerCase();
                    if (file_lowercase.endsWith(".in.png")) {
                        file = new File(file.getAbsolutePath().replace(".in.png", ""));
                    } else {
                        file_in_png = new File(file.getAbsolutePath() + ".in.png");
                    }
                    if (file_lowercase.endsWith(".out.png")) {
                        file = new File(file.getAbsolutePath().replace(".out.png", ""));
                    } else {
                        file_out_png = new File(file.getAbsolutePath() + ".out.png");
                    }
                    if (file_lowercase.endsWith(".state")) {
                        file = new File(file.getAbsolutePath().replace(".state", ""));
                    } else {
                        file_state = new File(file.getAbsolutePath() + ".state");
                    }
                    if (file.exists() || file_in_png.exists() || file_out_png.exists() || file_state.exists()) {
                        shortName name = new shortName(saver.getSelectedFile().getName());
                        int answer = JOptionPane.showConfirmDialog(getParent(), "File " + name.getShortName(30) + " already exists!\nOverwrite??", "Yes or No?", JOptionPane.YES_NO_CANCEL_OPTION);
                        switch (answer) {
                            case JOptionPane.YES_OPTION:
                                saver.approveSelection();
                                try {
                                    System.out.println("saving...");
                                    ImageIO.write(double_buffer.output.image, "png", new File(file.getAbsolutePath() + ".out.png"));   // save frame
                                    ImageIO.write(double_buffer.buffer.image, "png", new File(file.getAbsolutePath() + ".in.png"));
                                    // ImageIO.write(data_buffer.display.image, "png", new File(file.getAbsolutePath() + ".display.png"));   // cursor movements
                                    // ImageIO.write(data_buffer.image.image, "png", new File(file.getAbsolutePath() + ".sketch.png"));      // preloaded image
                                    Preset.write(Main.percept, new File(file.getAbsolutePath() + ".state")); // save with extension *.state
                                    System.out.println("...saved.");
                                    current_save_folder = file.getParent();
                                } catch (IOException e) { e.printStackTrace(); }
                                return;
                            case JOptionPane.NO_OPTION:
                                System.out.println("save file not accepted");
                                return;
                            case JOptionPane.CLOSED_OPTION:
                                System.out.println("save menu closed");
                                return;
                            case JOptionPane.CANCEL_OPTION:
                                saver.cancelSelection();
                                System.out.println("save menu cancelled");
                                return;
                        }
                    } else {
                        try {
                            System.out.println("saving...");
                            ImageIO.write(double_buffer.output.image, "png", new File(file.getAbsolutePath() + ".out.png"));   // save frame
                            ImageIO.write(double_buffer.buffer.image, "png", new File(file.getAbsolutePath() + ".in.png"));
                            // ImageIO.write(data_buffer.display.image, "png", new File(file.getAbsolutePath() + ".display.png"));   // cursor movements
                            // ImageIO.write(data_buffer.image.image, "png", new File(file.getAbsolutePath() + ".sketch.png"));      // preloaded image
                            Preset.write(Main.percept, new File(file.getAbsolutePath() + ".state")); // save with extension *.state
                            System.out.println("...saved.");
                            current_save_folder = file.getParent();
                        } catch (IOException e) { e.printStackTrace(); }
                    }
                }

            } catch (Exception E) {
                System.err.println("File write error while saving screenshot and state.");
                JOptionPane.showMessageDialog(getParent(), "Could not write files.", "Error", JOptionPane.ERROR_MESSAGE);
                E.printStackTrace();
            }
        }
    }); } catch (InterruptedException | InvocationTargetException e) { e.printStackTrace(); }

    if (windowed_mode) {
        setVisible(true);
    } else
        screen_mode();
    hide_cursor();
    save_as_running = false;
    running = true;

}

screenshot 1 of the save as menu

screenshot 2 of the file overwrite confirmation dialogue

1 个答案:

答案 0 :(得分:0)

经过大量的实验后,我发现了一种有趣的方法来纠正我的Windows 8中确认对话的外观,其中包含来自Oracle的最新Java。

主类现在扩展了JFrame ,似乎没有其他原因让菜单按照应有的方式运行。

由于代码方式的原因,主类还实现了Runnable,因此运行的第一个代码就像这样......

    Main m = new Main();
    m.run();

名为Perceptron的基本程序类是用作主程序窗口的JFrame。 Perceptron在方法m.run()中使用invokeAndWait(perceptron_runnable)在其自己的Runnable中实例化。

使用执行程序服务从Perceptron构造函数调用活动呈现循环,并在EDT之外运行。