日期:2014-05-20  浏览次数:20782 次

新学JAVA,老师给了两道题,但每个问题都只解决了一半,请大家地点一下
编写Applet,接收用户输入的网页地址,并与程序中事先保存的地址相比较,若存在则打开网页,并在新打开的浏览器窗口显示。

找一幅图像,显示在Applet中,要求:(1)按照原图大小显示;(2)将其放大一倍显示;(3)将其缩小一倍显示;(4)将图像的右下部的1/4块放大一倍显示。

------解决方案--------------------
下面的过程是开浏览器打开指定的页面, 支持多平台

public boolean showInBrowser(String url){
String os = System.getProperty( "os.name ").toLowerCase();
Runtime rt = Runtime.getRuntime();
try{
if (os.indexOf( "win " ) > = 0) {
// this doesn 't support showing urls in the form of "page.html#nameLink "
rt.exec( "rundll32 url.dll,FileProtocolHandler " + url);
} else if (os.indexOf( "mac " ) > = 0) {
rt.exec( "open " + url);
} else if (os.indexOf( "nix ") > =0 || os.indexOf( "nux ") > =0) {
// Do a best guess on unix until we get a platform independent way
// Build a list of browsers to try, in this order.
String[] browsers = { "epiphany ", "firefox ", "mozilla ", "konqueror ",
"netscape ", "opera ", "links ", "lynx "};

// Build a command string which looks like "browser1 "url " || browser2 "url " ||... "
StringBuffer cmd = new StringBuffer();
for (int i=0; i <browsers.length; i++)
cmd.append( (i==0 ? " " : " || " ) + browsers[i] + " \ " " + url + "\ " ");

rt.exec(new String[] { "sh ", "-c ", cmd.toString() });
} else {
return false;
}
}catch (IOException e){
return false;
}
return true;
}

那个提示找不到符号,因为第4,5个参数是double, 而api中定义是int
------解决方案--------------------
Runtime.getRuntime().exec( "start 网址 ");

这样应该可以的吧