import java.net.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Applet按钮打开网页 extends JApplet implements ActionListener { WebButton[] choices = new WebButton[3]; /* * 初始化Applet * (non-Javadoc) * @see java.applet.Applet#init() */ public void init() { choices[0] = new WebButton("Obscure Store", "http://www.obscurestore.com/"); choices[1] = new WebButton("Need to Know", "http://www.ntk.net/"); choices[2] = new WebButton("Bleat", "http://www.lileks.com/bleats"); FlowLayout flo = new FlowLayout(); Container pane = getContentPane(); pane.setLayout(flo); for (int i = 0; i < choices.length; i++) { choices[i].addActionListener(this); pane.add(choices[i]); } setContentPane(pane); } /* * 网页点击按钮调用 * (non-Javadoc) * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent) */ public void actionPerformed(ActionEvent evt) { WebButton clicked = (WebButton)evt.getSource(); try { URL load = new URL(clicked.address); getAppletContext().showDocument(load);//显示网页 } catch (MalformedURLException e) { showStatus("Bad URL:" + clicked.address); } } public void dssdf(){ return; } } class WebButton extends JButton { String address; WebButton(String iLabel, String iAddress) { super(iLabel); address = iAddress; } }