Java 示例 - 使用 Applet 在新窗口中打开链接 问题描述 如何使用 Applet 在新窗口中打开链接? 解决方案 以下示例演示了如何使用 showDocument() 在新窗口中从小程序打开特定网页,第二个参数为“_blank”。 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()); } } } 复制 结果 上面的代码示例将在启用 java 的 Web 浏览器中产生以下结果。 View in Browser. 复制