在java的桌面程序,一个界面里如何实现点击几个字即打开IE到一个网址;或者点菜单即可打开IE到一个网址? 如题。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.IOException;import javax.swing.*;public class OpenIE extends JFrame { public OpenIE() { JButton button = new JButton("Open"); button.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub try { String[] strs = {"C:\\Program Files\\Internet Explorer\\iexplore.exe", "http://dzh.mop.com/topic/readSub_5661107_0_0.html"}; Runtime.getRuntime().exec(strs); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } }); this.getContentPane().add(button); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setBounds(200, 200, 200, 200); } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub new OpenIE().setVisible(true); }} C:\\Program Files\\Internet Explorer\\用这这样的地址很有问题。给个比较健壮的:/* ============================================================== * $Id URLOpener.java,v 1.0 xio Exp $ * Created on [2005-5-23 8:51:41] by xio * ============================================================== * * 项目名称:$ XioLog $ */package com.wodar.log.util;import java.lang.reflect.Method;import javax.swing.JOptionPane;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;/** * <p>Title: </p> * <p>Description: </p> * <p>Copyright: Copyright (c) 2005</p> * <p>Company: wodar.com</p> * <p>URLOpener.java</p> * @author xio * @version $Revision:1.0 $ $Date 2006-1-16,19:34:21$ */public class URLOpener { public static void openURL(String url) { 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( "rundll32 url.dll,FileProtocolHandler " + url); } else { //assume Unix or Linux 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"); } else { Runtime.getRuntime().exec(new String[] { browser, url }); } } } catch (Exception ex) { log.warn("打开浏览器时出错:" + ex.getMessage()); JOptionPane.showMessageDialog(null, "打开浏览器时出错:" + ex.getLocalizedMessage()); } } private static Log log = LogFactory.getLog(URLOpener.class);} Runtime.getRuntime().exec("C:\\Program Files\\Internet Explorer\\iexplore.exe http://doc.c06.net/tea/node/N39885L1PnullLnull.html"); 这样写就可以了 Myeclipse可以编辑,为什么还需整合进Eclipse? 请高人指点,java收邮件,急!急!急! Ireport+JasperReport的问题 在线求解 马上送分 哪位高手能帮我改改程序啊!!很简单的,关于C/S通信的 为什么在借口的方法定义中不能使用setLable() ? 改CSDN的名字怎么改 初学JAVA,一个小问题 怎么样用JAVA取得系统的路径? 学习JAVA,装上JBUILDER还用不用装JDK了? jbuilder4.0 企业版能否把代码编译成在特定平台上运行的代码 java 窗口与主线程的关系 请教:在Ecplise能启动TOMCAT,而在LOMBOZ中。。。
import java.awt.event.ActionListener;
import java.io.IOException;import javax.swing.*;public class OpenIE extends JFrame { public OpenIE() {
JButton button = new JButton("Open");
button.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
try {
String[] strs = {"C:\\Program Files\\Internet Explorer\\iexplore.exe",
"http://dzh.mop.com/topic/readSub_5661107_0_0.html"};
Runtime.getRuntime().exec(strs);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
this.getContentPane().add(button);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setBounds(200, 200, 200, 200);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new OpenIE().setVisible(true);
}}
用这这样的地址很有问题。给个比较健壮的:/* ==============================================================
* $Id URLOpener.java,v 1.0 xio Exp $
* Created on [2005-5-23 8:51:41] by xio
* ==============================================================
*
* 项目名称:$ XioLog $
*/
package com.wodar.log.util;import java.lang.reflect.Method;import javax.swing.JOptionPane;import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2005</p>
* <p>Company: wodar.com</p>
* <p>URLOpener.java</p>
* @author xio
* @version $Revision:1.0 $ $Date 2006-1-16,19:34:21$
*/
public class URLOpener {
public static void openURL(String url) {
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(
"rundll32 url.dll,FileProtocolHandler " + url);
} else { //assume Unix or Linux
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");
} else {
Runtime.getRuntime().exec(new String[] { browser, url });
}
}
} catch (Exception ex) {
log.warn("打开浏览器时出错:" + ex.getMessage());
JOptionPane.showMessageDialog(null, "打开浏览器时出错:"
+ ex.getLocalizedMessage());
}
} private static Log log = LogFactory.getLog(URLOpener.class);
}