首页 > 解决方案 > 如果 Java 中不存在外部应用程序的路径,则需要抛出错误消息

问题描述

我有 4 个执行每个特定程序的 jButton(暂时是浏览器),我需要创建一个条件来告诉用户,如果该特定程序的路径不存在,它将打开一个带有该特定浏览器的网页.

这段代码会重复自己,我需要对其进行优化。我尝试使用另一种方法来使用 if 语句,但它只是在执行每个按钮时给我带来了很多问题。

警告_MSG(); 只是 jOptionPane 的另一种方法,用于确认用户是否要打开外部应用程序等。

private void chromeMouseClicked(java.awt.event.MouseEvent evt) {                                    
  
    File file = new File("C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe");
    if(!file.exists()){
       try {
         URI url = new URI("https://google.com/chrome"); 
         java.awt.Desktop.getDesktop().browse(url);
         System.err.println("File Error, Google Chrome is not found!\n");
            } catch (IOException | URISyntaxException e) {
              System.err.println("Unknown Error, Exception found\n" + e);
             }
    }else {
        Warning_MSG(file);
    }
    
}                                   

private void firefoxMouseClicked(java.awt.event.MouseEvent evt) {                                     
    File file = new File("C:\\Program Files\\Mozilla Firefox\\firefox.exe");
    if(!file.exists()){
       try {
         System.err.println("File Error, Mozilla Firefox is not found!\n");
         URI url = new URI("https://www.mozilla.org/en-GB/firefox/download/thanks/"); 
         java.awt.Desktop.getDesktop().browse(url);
            } catch (IOException | URISyntaxException e) {
             System.err.println("Unknown Error, Exception found\n" + e);
             }
    }else {
        Warning_MSG(file);
    }
    
}                                    

private void ieMouseClicked(java.awt.event.MouseEvent evt) {                                
    File file = new File("C:\\Program Files (x86)\\Internet Explorer\\iexplore.exe");
   if(!file.exists()){
      System.err.println("File Error, iExplorer is not found! \n");
    }else {
        Warning_MSG(file);
    }
    
}                               

private void edgeMouseClicked(java.awt.event.MouseEvent evt) {                                  
    File file = new File("C:\\Windows\\SystemApps\\Microsoft.MicrosoftEdge_8wekyb3d8bbwe\\MicrosoftEdge.exe");
 if(!file.exists()){
       System.err.println("File Error, Microsoft Edge is not Found! \n");
    }else {
        Warning_MSG(file);
    }
    
}      

问题是,我怎样才能优化这些代码片段,这样我就不必在其他程序中重复同样的事情,而是只在满足条件时才运行某些事情?

标签: javaswingif-statement

解决方案


您可以创建一个带有 2 个参数的枚举(exe 路径、下载页面)

enum Browser
{
    Chrome("C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe", "https://google.com/chrome"),
    Firefox("C:\\Program Files\\Mozilla Firefox\\firefox.exe", "https://www.mozilla.org/en-GB/firefox/download/thanks/"),
    IExplorer("C:\\Program Files (x86)\\Internet Explorer\\iexplore.exe", "https://google.com/internet-explorer"),
    Edge("C:\\Windows\\SystemApps\\Microsoft.MicrosoftEdge_8wekyb3d8bbwe\\MicrosoftEdge.exe", "https://google.com/download-edge");
    
    private final String _filePath, _downloadPageURL;
    
    Browser(String filePath, String downloadPage)
    {
        _filePath = filePath;
        _downloadPageURL = downloadPage;
    }
    
    private String getDownloadPageURL()
    {
        return _downloadPageURL;
    }
    
    private String getFilePath()
    {
        return _filePath;
    }
    
    public boolean exists()
    {
        return new File(getFilePath()).exists();
    }
    
    public void openDownloadPage()
    {
        try
        {
            Desktop.getDesktop().browse(new URI(getDownloadPageURL()));
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}

然后,您所要做的就是简单检查您的方法。

private void chromeMouseClicked(java.awt.event.MouseEvent evt) 
{
    final Browser chrome = Browser.Chrome;
    if (!chrome.exists())
    {
        chrome.openDownloadPage();
        System.err.println("File Error, Google Chrome is not found!\n");
        return;
    }
    
    // The browser exists at this line. So create your code
    // ...
}

推荐阅读