如何使用Applet在新窗口中打开链接?(How to open a link in a new window using Applet?)

优质
小牛编辑
120浏览
2023-12-01

问题描述 (Problem Description)

如何使用Applet在新窗口中打开链接?

解决方案 (Solution)

下面的示例演示如何使用带有第二个参数为“_blank”的showDocument()从新窗口中的applet打开特定网页。

import java.applet.*;
import java.awt.*;
import java.net.*;
import java.awt.event.*;
public class testURL_NewWindow extends Applet implements ActionListener {
   public void init() { 
      String link_Text = "google";
      Button b = new Button(link_Text);
      b.addActionListener(this);
      add(b);
   }
   public void actionPerformed(ActionEvent ae) { 
      Button source = (Button)ae.getSource();
      String link = "http://www."+source.getLabel()+".com";
      try {
         AppletContext a = getAppletContext();
         URL url = new URL(link);
         a.showDocument(url,"_blank");
      } catch (MalformedURLException e) {
         System.out.println(e.getMessage());
      }
   }
}

结果 (Result)

上面的代码示例将在启用Java的Web浏览器中生成以下结果。

View in Browser.