如何使用 Java 中的小应用程序访问链接



问题说明

如何使用小应用程序访问链接?

解决方案

以下示例演示如何使用 AppletContext 类的 showDocument() 方法从一个小应用程序访问某个特定网页。

import java.applet.*;
import java.awt.*;
import java.net.*;
import java.awt.event.*;

public class tesURL extends Applet implements ActionListener {
   public void init() {
      String link = "yahoo";
      Button b = new Button(link);
      b.addActionListener(this);
      add(b);
   }
   public void actionPerformed(ActionEvent ae) {
      Button src = (Button)ae.getSource();
      String link = "http://www."+src.getLabel()+".com";
      
      try {
         AppletContext a = getAppletContext();
         URL u = new URL(link);
         a.showDocument(u,"_self");
      } catch (MalformedURLException e){
         System.out.println(e.getMessage());
      }
   }
}

结果

上述代码示例将在支持 Java 的 Web 浏览器中产生以下结果。

View in Browser. 
java_applets.htm
广告
© . All rights reserved.