通过java中的按钮启动网站

时间:2015-10-01 03:14:13

标签: java

我正在开发一个软件,用户可以通过点击java按钮访问任何网站。

JButton button1 = new JButton("Click Me");
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent myEvent) {
    // Here clicking this method will open a website
   open("www.myrequiredWesite.com");
}

});

如何从上面的java源代码调用默认浏览器,以便默认浏览器将从java图形用户界面中单击按钮打开特定站点。

1 个答案:

答案 0 :(得分:-1)

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

public static void openPage(URL url) {
    try {
        openPage(url.toURI());
    } catch (URISyntaxException e) {
        e.printStackTrace();
    }
}

注意使用java.net来使用桌面对象