swing gui 打开url 连接的一个方法
2009-05-27 11:34
281 查看
// coding
StringBuffer html = new StringBuffer();
private static final String LEGAL_INFORMATION = "Legal_Information";
html.append("<html><head></head>");
html.append("<body style='font-family: Pain; color:#383838; font-size: 8px; font-weight: normal;'>");
html.append(" <a href="+LEGAL_INFORMATION+"> Legal<BR>");
html.append("Information</a>. By use of this system, the user consents to the terms of this Notice.");
html.append("</body></html>");
JEditorPane last = new JEditorPane("text/html", html.toString());
last.setBackground(this.getBackground());
last.setEditable(false);
last.setSelectionColor(this.getBackground());
last.addHyperlinkListener(new HyperlinkListener() {
public void hyperlinkUpdate(HyperlinkEvent e) {
if(e.getEventType().equals(EventType.ENTERED)){
setCursor(new Cursor(Cursor.HAND_CURSOR));
}else{
if(e.getEventType().equals(EventType.EXITED)){
setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
}
}
if(e.getEventType().equals(EventType.ACTIVATED)){
if (e.getDescription() != null) {
if (LEGAL_INFORMATION.equals(e.getDescription())) {
openURL("http://www.yourCompany.com/legal.html");
}
}
}
}
});
//open url
public static void openURL(String url) {
String errMsg = "Error attempting to launch web browser";
String osName = System.getProperty("os.name");
try {
if(osName.startsWith("Mac OS")) {
Class fileMgr = Class.forName("com.apple.eio.FileManager");
Method openURL = fileMgr.getDeclaredMethod("openURL", new Class[] {String.class});
openURL.invoke(null, new Object[] { url });
} else if(osName.startsWith("Windows")){
Runtime.getRuntime().exec((new StringBuilder()).append("rundll32 url.dll,FileProtocolHandler ").append(url).toString());
} else {
String browsers[] = {"firefox", "opera", "konqueror", "epiphany", "mozilla", "netscape"};
String browser = null;
for(int count = 0; count < browsers.length && browser == null; count++)
if( Runtime.getRuntime().exec( new String[] { "which", browsers[count] }).waitFor() == 0)
browser = browsers[count];
if(browser == null)
throw new Exception("Could not find web browser");
Runtime.getRuntime().exec(new String[] { browser, url});
}
}catch(Exception e){
JOptionPane.showMessageDialog(null, (new StringBuilder()).append(errMsg).append(":/n").append(e.getLocalizedMessage()).toString());
}
}
StringBuffer html = new StringBuffer();
private static final String LEGAL_INFORMATION = "Legal_Information";
html.append("<html><head></head>");
html.append("<body style='font-family: Pain; color:#383838; font-size: 8px; font-weight: normal;'>");
html.append(" <a href="+LEGAL_INFORMATION+"> Legal<BR>");
html.append("Information</a>. By use of this system, the user consents to the terms of this Notice.");
html.append("</body></html>");
JEditorPane last = new JEditorPane("text/html", html.toString());
last.setBackground(this.getBackground());
last.setEditable(false);
last.setSelectionColor(this.getBackground());
last.addHyperlinkListener(new HyperlinkListener() {
public void hyperlinkUpdate(HyperlinkEvent e) {
if(e.getEventType().equals(EventType.ENTERED)){
setCursor(new Cursor(Cursor.HAND_CURSOR));
}else{
if(e.getEventType().equals(EventType.EXITED)){
setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
}
}
if(e.getEventType().equals(EventType.ACTIVATED)){
if (e.getDescription() != null) {
if (LEGAL_INFORMATION.equals(e.getDescription())) {
openURL("http://www.yourCompany.com/legal.html");
}
}
}
}
});
//open url
public static void openURL(String url) {
String errMsg = "Error attempting to launch web browser";
String osName = System.getProperty("os.name");
try {
if(osName.startsWith("Mac OS")) {
Class fileMgr = Class.forName("com.apple.eio.FileManager");
Method openURL = fileMgr.getDeclaredMethod("openURL", new Class[] {String.class});
openURL.invoke(null, new Object[] { url });
} else if(osName.startsWith("Windows")){
Runtime.getRuntime().exec((new StringBuilder()).append("rundll32 url.dll,FileProtocolHandler ").append(url).toString());
} else {
String browsers[] = {"firefox", "opera", "konqueror", "epiphany", "mozilla", "netscape"};
String browser = null;
for(int count = 0; count < browsers.length && browser == null; count++)
if( Runtime.getRuntime().exec( new String[] { "which", browsers[count] }).waitFor() == 0)
browser = browsers[count];
if(browser == null)
throw new Exception("Could not find web browser");
Runtime.getRuntime().exec(new String[] { browser, url});
}
}catch(Exception e){
JOptionPane.showMessageDialog(null, (new StringBuilder()).append(errMsg).append(":/n").append(e.getLocalizedMessage()).toString());
}
}
相关文章推荐
- swing点击按钮 打开一个浏览器窗口的方法
- Mac开发 浏览器中打开一个URL连接地址
- Excel 超连接图片文件默认用IE打开解决方法
- vs2005打开form设计器出错的一个解决方法
- 在vc中用MFC按纽打开一个txt文件,在旁边的文本框里显示文件的路径的方法
- Visual Studio 连接 Mysql 实现一个选课管理系统--->提取数据的几种方法
- 一个链接按两种方式打开两个网址的方法
- Android网络连接的两种方法:apache client和httpurlconnection的比较 分类: H1_ANDROID 2013-10-13 23:23 640人阅读 评论(0) 收藏
- Linux+Mono+Apache2下Asp.net连接MySql的一个错误及解决方法
- SpringMVC,3种不同的URL路由配置方法(这根本不是一个小问题)
- "Adb connection Error:远程主机强迫关闭了一个现有的连接"的解决方法
- 火狐SSL收到一个弱临时Diffie-Hellman连接失败解决方法
- 基于AWT、Swing的GUI编程 - 多个命令共享同一个监听器类
- JavaScript写一个方法把URL解析成对象.html
- 打开虚拟机时因出现一个VMware Autheffication选择框而打不开的解决方法
- Adb connection Error:远程主机强迫关闭了一个现有的连接——解决方法k
- 字符串的原型扩展一个方法来解析url(String.prototype.pro)-自己写的一个方法
- fsockopen — 打开一个网络连接或者一个Unix套接字连接
- redis连接过多的一个解决方法
- MYSQL不能从远程连接的一个解决方法