我有4個jButtons來執行每個特定的程式(Browsers暫時),我需要做一個條件,告訴用戶如果該特定程式的路徑不存在,它將打開一個以該特定瀏覽器為中心的網頁。
這段代碼會重復出現,我需要對其進行優化。我試著做了另一種方法,在那里我將使用if陳述句,但在執行每個按鈕時,它反而給我帶來了很多問題。
Warning_MSG(); 只是另一個使用jOptionPane的方法,以確認用戶是否要打開一個外部應用程式,等等等等。
private void chromeMouseClicked(java. awt.event.MouseEvent evt) {
File file = new File( "C: 程式檔(x86)GoogleChromeApplicationchrome. exe")。)
if(!file.exists()){
try {
URI url = new URI( "https://google. com/chrome")。)
java.awt.Desktop.getDesktop().browse(url)。
System.err.println("檔案錯誤,沒有找到谷歌瀏覽器!
")。)
} catch (IOException | URISyntaxException e) {
System.err.println("Unknown Error, Exception found
" e)。)
}
}else {
Warning_MSG(file)。
}
}
private void firefoxMouseClicked(java.awt.event.MouseEvent evt) {
File file = new File( "C: 程式檔案(Program Files):Mozilla Firefoxfirefox. exe")。)
if(!file.exists()){
try {
System.err.println("File Error, Mozilla Firefox is not found!
")。)
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
" e)。)
}
}else {
Warning_MSG(file)。
}
}
private void ieMouseClicked(java.awt.event.MouseEvent evt) {
File file = new File( "C: 程式檔(x86)/Internet Explorer/iexplore。 exe")。)
if(!file.exists()){
System.err.println("File Error, iExplorer is not found!
")。)
}else {
Warning_MSG(file)。
}
}
private void edgeMouseClicked(java.awt.event.MouseEvent evt) {
File file = new File( "C: C: /WindowsSystemAppsMicrosoft. MicrosoftEdge_8wekyb3d8bbweMicrosoftEdge.exe")。)
if(!file.exists()){
System.err.println("File Error, Microsoft Edge is not Found!
")。)
}else {
Warning_MSG(file)。
}
問題是,我怎樣才能優化這些代碼,使我不必在其他程式中重復同樣的事情,而是在滿足條件時才運行某些東西?
uj5u.com熱心網友回復:
你可以用2個引數(exe路徑、下載頁面)創建一個列舉
enum Browser
{
Chrome("C:Program Files (x86)GoogleChromeApplicationchrome.exe", "https://google.com/chrome") 。
Firefox("C:Program FilesMozilla Firefoxfirefox.exe", "https://www.mozilla.org/en-GB/firefox/download/thanks/"),
IExplorer("C:Program Files (x86)Internet Exploreriexplore.exe", "https://google.com/internet-explorer"),
Edge("C:WindowsSystemAppsMicrosoft.MicrosoftEdge_8wekyb3d8bbweMicrosoftEdge.exe", "https://google.com/download-edge") 。
private final String _filePath, _downloadPageURL;
瀏覽器(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("檔案錯誤,沒有找到谷歌瀏覽器!
")。)
return。
}
// 瀏覽器在這一行存在。因此,創建你的代碼
//...。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/333339.html
標籤:
