在JB中如何做一个按钮事件转到一个新开窗口的网址上? try { Runtime.getRuntime().exec("explorer http://www.sohu.com"); } catch (IOException e1) {} 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我在以下事件中加入后还是出错。。void jButton1_actionPerformed(ActionEvent e) { try { Runtime.getRuntime().exec("explorer http://cn.yahoo.com"); } catch (IOException e1) {}} ·如果使用“Runtime的exec方法”调用浏览器程序的话,最好加上浏览器的判断:String vendor = System.getProperty("java.vendor");if(vendor.equals("Netscape Communications Corporation")){//使用Netscape...} else if(vendor.equals("Sun MicroSystem Inc.")) {//使用AppletViewer...} else {//使用IE...}·使用AppletContext。如下:try {getAppletContext().showDocument(new URL("http://www.abc.com"), "_blank");} catch (MalformedURLException e) {//error handle}建议使用第二种方法。 import java.awt.*;import java.awt.event.*;import java.applet.Applet;import java.net.*;import java.applet.AppletContext;import javax.swing.*;public class MyApp extends JApplet{ public void init(){ JButton b = new JButton("www.yeah.net"); b.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ try{ URL url = new URL("http://www.javasoft.com/index.html"); System.out.println("http://www.javasoft.com/index.html"); getAppletContext().showDocument(url); } catch(MalformedURLException murl){ System.out.println("bad url !"); } } }); JButton b1 = new JButton("1"); b1.setFocusPainted(true); JButton b2 = new JButton("2"); // b2.setDefaultCapable(true); // JRootPane jrp = this.createRootPane(); // jrp.setDefaultButton(b2); // setRootPane(jrp); getContentPane().setLayout(new FlowLayout()); getContentPane().add(b); getContentPane().add(b1); getContentPane().add(b2); b1.setSelected(true); } } 谢谢两位!hexiaofeng(java爱好者)、(怪侠一枝梅) 先谢谢了成功后一定给分。 还是不成功。。有没有例子呀。JB中import java.awt.*;import java.awt.event.*;import java.applet.*;//import java.io.IOException ;import java.net.*;import java.applet.AppletContext;import javax.swing.*;public class Applet3 extends Applet { boolean isStandalone = false; JButton b = new JButton(); JButton b1 = new JButton(); JButton b2 = new JButton(); //Get a parameter value public String getParameter(String key, String def) { return isStandalone ? System.getProperty(key, def) : (getParameter(key) != null ? getParameter(key) : def); } //Construct the applet public Applet3() { } //Initialize the applet public void init() { try { jbInit(); } catch(Exception e) { e.printStackTrace(); } } //Component initialization private void jbInit() throws Exception { b.setBounds(new Rectangle(94, 115, 73, 29)); b.setText("江阴"); b.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { b_actionPerformed(e); } }); this.setLayout(null); b1.setText("常州"); b1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { } }); b1.setBounds(new Rectangle(370, 112, 73, 29)); b2.setText("无锡"); b2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { } }); b2.setBounds(new Rectangle(618, 115, 73, 29)); this.setBackground(Color.lightGray); this.add(b, null); this.add(b1, null); this.add(b2, null); } //Get Applet information public String getAppletInfo() { return "Applet Information"; } //Get parameter info public String[][] getParameterInfo() { return null; } void b_actionPerformed(ActionEvent e) {try { getAppletContext().showDocument(new URL("http://cn.yahoo.com"), "_blank"); } catch (MalformedURLException e) { //error handle } }} void b_actionPerformed(ActionEvent e) {try { getAppletContext().showDocument(new URL("http://cn.yahoo.com"), "_blank"); } catch (MalformedURLException e) { //error handle } }有错! swingcoder(swingcoder)错什么地方? 原因:在void b_actionPerformed(ActionEvent e)定义了变量e,在catch MalformedURLException e)又定义了变量e,造成重复定义。办法:修改catch MalformedURLException e)为catch MalformedURLException ex) 关于JAVA串口通信,RXTXComm.jar的问题(AIX系统) textPane不能输入中文的问题 求助!!! 也做了一个内网聊天工具 用JBUILDER生成EXE文件后可以打开,可是要从旁边读取图片, 我有一个面试,哪位大侠能帮忙作一下笔试题目?在线等!!!!! ◆◆简单问题◆◆【请问如何同时给JTable中的不同的行使用不同的字体和颜色?】 很有挑战的java IO 问题,如何实现byte[]转化为FileInputStream,不经过存储为文件 关于Socket类的getKeepAlive()方法?? synchronized问题 关于停止线程的问题 help!
void jButton1_actionPerformed(ActionEvent e)
{
try {
Runtime.getRuntime().exec("explorer http://cn.yahoo.com");
}
catch (IOException e1) {}
}
String vendor = System.getProperty("java.vendor");
if(vendor.equals("Netscape Communications Corporation")){
//使用Netscape...
} else if(vendor.equals("Sun MicroSystem Inc.")) {
//使用AppletViewer...
} else {
//使用IE...
}·使用AppletContext。如下:
try {
getAppletContext().showDocument(new URL("http://www.abc.com"), "_blank");
} catch (MalformedURLException e) {
//error handle
}建议使用第二种方法。
import java.awt.event.*;
import java.applet.Applet;
import java.net.*;
import java.applet.AppletContext;
import javax.swing.*;public class MyApp extends JApplet
{
public void init(){
JButton b = new JButton("www.yeah.net");
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try{
URL url = new URL("http://www.javasoft.com/index.html");
System.out.println("http://www.javasoft.com/index.html");
getAppletContext().showDocument(url);
}
catch(MalformedURLException murl){
System.out.println("bad url !");
}
}
});
JButton b1 = new JButton("1");
b1.setFocusPainted(true);
JButton b2 = new JButton("2");
// b2.setDefaultCapable(true);
// JRootPane jrp = this.createRootPane();
// jrp.setDefaultButton(b2);
// setRootPane(jrp);
getContentPane().setLayout(new FlowLayout());
getContentPane().add(b);
getContentPane().add(b1);
getContentPane().add(b2);
b1.setSelected(true);
}
}
hexiaofeng(java爱好者)、(怪侠一枝梅) 先谢谢了成功后一定给分。
有没有例子呀。JB中
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
//import java.io.IOException ;
import java.net.*;
import java.applet.AppletContext;
import javax.swing.*;public class Applet3 extends Applet {
boolean isStandalone = false;
JButton b = new JButton();
JButton b1 = new JButton();
JButton b2 = new JButton();
//Get a parameter value
public String getParameter(String key, String def) {
return isStandalone ? System.getProperty(key, def) :
(getParameter(key) != null ? getParameter(key) : def);
} //Construct the applet
public Applet3() {
}
//Initialize the applet
public void init() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
b.setBounds(new Rectangle(94, 115, 73, 29));
b.setText("江阴");
b.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
b_actionPerformed(e);
}
});
this.setLayout(null);
b1.setText("常州");
b1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) { }
});
b1.setBounds(new Rectangle(370, 112, 73, 29));
b2.setText("无锡");
b2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) { }
});
b2.setBounds(new Rectangle(618, 115, 73, 29));
this.setBackground(Color.lightGray);
this.add(b, null);
this.add(b1, null);
this.add(b2, null);
}
//Get Applet information
public String getAppletInfo() {
return "Applet Information";
}
//Get parameter info
public String[][] getParameterInfo() {
return null;
} void b_actionPerformed(ActionEvent e) {
try {
getAppletContext().showDocument(new URL("http://cn.yahoo.com"), "_blank");
}
catch (MalformedURLException e)
{ //error handle
}
}
}
try {
getAppletContext().showDocument(new URL("http://cn.yahoo.com"), "_blank");
}
catch (MalformedURLException e)
{ //error handle
}
}有错!
在void b_actionPerformed(ActionEvent e)定义了变量e,在catch MalformedURLException e)又定义了变量e,造成重复定义。办法:
修改catch MalformedURLException e)为catch MalformedURLException ex)