下面的例子的功能是在一个文本框中输入网址后单击“确定”按钮即可链接到指定的页面,但是请问为什么运行后确无法链接到指定的页面?
import java.net.*;
public class Boy extends Applet implements ActionListener
{  
   Button button;
   URL url;
   TextField text;
   public void init()
   {  
      text=new TextField(18);
      button=new Button("确定");
      add(new Label("输入网址:"));
      add(text); add(button);
      button.addActionListener(this);
   }
   public void actionPerformed(ActionEvent e)
   { 
      if(e.getSource()==button)
       {  try {  url=new URL(text.getText().trim());
                 getAppletContext().showDocument(url);
              }
          catch(MalformedURLException g)
             {  text.setText("不正确的URL:"+url);
             }
       }
   }
}

解决方案 »

  1.   

    http://168899.0033.cn
      

  2.   

    可能getAppletContext()不足以显示网页
    以下仅作参考import java.net.URL;
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTextPane;public class Test {
    public static void main(String args[]) {
    JTextPane tp = new JTextPane();
    JScrollPane js = new JScrollPane();
    js.getViewport().add(tp);
    JFrame jf = new JFrame();
    jf.getContentPane().add(js);
    jf.pack();
    jf.setSize(400, 500);
    jf.setVisible(true); try {
    URL url = new URL(
    "http://www.sina.com.cn");
    tp.setPage(url);
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }