myfilechooser.java

来自「java画图程序」· Java 代码 · 共 59 行

JAVA
59
字号
package mypackage;
import javax.swing.*;
import java.io.File;
import javax.swing.filechooser.FileSystemView;

//保存对话框
public class MyFileChooser extends JFileChooser
{

    public MyFileChooser() 
    {
        super();
    }

    public MyFileChooser(File currentDirectory) 
    {
        super(currentDirectory);
    }

    public MyFileChooser(File currentDirectory, FileSystemView fsv) 
    {
        super(currentDirectory, fsv);
    }

    public MyFileChooser(FileSystemView fsv) 
    {
        super(fsv);
    }

    public MyFileChooser(String currentDirectoryPath) 
    {
        super(currentDirectoryPath);
    }

    public MyFileChooser(String currentDirectoryPath, FileSystemView fsv) 
    {
        super(currentDirectoryPath, fsv);
    }

    public void approveSelection()
    {
        if (this.getDialogType() == JFileChooser.SAVE_DIALOG)
        {
            File temp = this.getSelectedFile();
            
            if (temp.exists()) 
            {
                if (JOptionPane.showConfirmDialog(this,
                        temp.getPath() + " 已存在。\n要替换它吗?",
                        	"另存为",
                        		JOptionPane.YES_NO_OPTION,
                        			JOptionPane.WARNING_MESSAGE)
                        				== JOptionPane.NO_OPTION)
                    return;
            }
        }
        super.approveSelection();
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?