Programing

자바 버튼으로 브라우저에서 링크를여시겠습니까?

lottogame 2020. 8. 18. 08:10
반응형

자바 버튼으로 브라우저에서 링크를여시겠습니까?


버튼 클릭으로 기본 브라우저에서 링크를 열려면 어떻게해야합니까?

button.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        open("www.google.com"); // just what is the 'open' method?
    }
});

?


사용 바탕 화면 # 찾아보기 (URI) 방법을. 사용자의 기본 브라우저에서 URI를 엽니 다.

public static boolean openWebpage(URI uri) {
    Desktop desktop = Desktop.isDesktopSupported() ? Desktop.getDesktop() : null;
    if (desktop != null && desktop.isSupported(Desktop.Action.BROWSE)) {
        try {
            desktop.browse(uri);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return false;
}

public static boolean openWebpage(URL url) {
    try {
        return openWebpage(url.toURI());
    } catch (URISyntaxException e) {
        e.printStackTrace();
    }
    return false;
}

public static void openWebpage(String urlString) {
    try {
        Desktop.getDesktop().browse(new URL(urlString).toURI());
    } catch (Exception e) {
        e.printStackTrace();
    }
}

try {
    Desktop.getDesktop().browse(new URL("http://www.google.com").toURI());
} catch (Exception e) {}

참고 : 필요한 가져 오기를 포함해야합니다. java.net


데스크탑 환경이없는 솔루션은 BrowserLauncher2 입니다. 이 솔루션은 Linux에서와 같이 더 일반적이며 Desktop이 항상 사용 가능한 것은 아닙니다.

길이 답변은 https://stackoverflow.com/a/21676290/873282에 게시됩니다.


private void ButtonOpenWebActionPerformed(java.awt.event.ActionEvent evt) {                                         
        try { 
         String url = "https://www.google.com";
         java.awt.Desktop.getDesktop().browse(java.net.URI.create(url));
       }
       catch (java.io.IOException e) {
           System.out.println(e.getMessage());
       }
    }    

public static void openWebPage(String url) {
        try {
            Desktop desktop = Desktop.isDesktopSupported() ? Desktop.getDesktop() : null;
            if (desktop != null && desktop.isSupported(Desktop.Action.BROWSE)) {
                desktop.browse(new URI(url));
            }
            throw new NullPointerException();
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, url, "", JOptionPane.PLAIN_MESSAGE);
        }
    }

나는 이것이 오래된 질문이라는 것을 알고 있지만 때로는 Desktop.getDesktop()Ubuntu 18.04에서와 같이 예기치 않은 충돌이 발생합니다. 따라서 다음과 같이 코드를 다시 작성해야합니다.

public static void openURL(String domain)
{
    String url = "https://" + domain;
    Runtime rt = Runtime.getRuntime();
    try {
        if (MUtils.isWindows()) {
            rt.exec("rundll32 url.dll,FileProtocolHandler " + url).waitFor();
            Debug.log("Browser: " + url);
        } else if (MUtils.isMac()) {
            String[] cmd = {"open", url};
            rt.exec(cmd).waitFor();
            Debug.log("Browser: " + url);
        } else if (MUtils.isUnix()) {
            String[] cmd = {"xdg-open", url};
            rt.exec(cmd).waitFor();
            Debug.log("Browser: " + url);
        } else {
            try {
                throw new IllegalStateException();
            } catch (IllegalStateException e1) {
                MUtils.alertMessage(Lang.get("desktop.not.supported"), MainPn.getMainPn());
                e1.printStackTrace();
            }
        }
    } catch (IOException | InterruptedException e) {
        e.printStackTrace();
    }
}

public static boolean isWindows()
{
    return OS.contains("win");
}

public static boolean isMac()
{
    return OS.contains("mac");
}

public static boolean isUnix()
{
    return OS.contains("nix") || OS.contains("nux") || OS.indexOf("aix") > 0;
}

그런 다음 인스턴스에서이 도우미를 호출 할 수 있습니다.

button.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        MUtils.openURL("www.google.com"); // just what is the 'open' method?
    }
});

참고URL : https://stackoverflow.com/questions/10967451/open-a-link-in-browser-with-java-button

반응형