2016-09-02 37 views
-1

我們在下面有一個小程序代碼。我們已經指定了一個按鈕,當點擊按鈕時,它應該打開一個新的網站。雅虎網站在這種情況下。該小程序的代碼是Java小程序按鈕點擊打開鏈接

public class GotoLinkApplet extends Applet implements ActionListener{ 
public void init() 
{ 
    String link="yahoo"; 
    Button b=new Button(link); 
    b.addActionListener(this); 
    add(b); 

} 
@Override 
public void actionPerformed(ActionEvent e) { 
    // TODO Auto-generated method stub 
    System.out.println("1"); 
Button src= (Button)e.getSource(); 
String link="http://www."+src.getLabel()+".com"; 
try{ 
    AppletContext a=getAppletContext(); 
    URL url =new URL(link); 
    //a.showDocument(url, "_self"); 
     a.showDocument(url, "_blank"); 
     System.out.println("a"); 
     } 
catch(MalformedURLException ae) 
{ 
    System.out.println(ae.getMessage()); 
} 
} 

} 

我們在eclipse中執行上述代碼,但是當我們點擊按鈕時,雅虎鏈接不會出現。請求你幫忙。但是指定showdocument的代碼運行良好。

對上述任何幫助非常感謝。

+0

1)爲什麼要編寫一個applet?如果是由於老師指定它,請將它們轉介給[爲什麼CS教師應該**停止**教Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should -stop教學-java的小應用程序/)。 2)爲什麼使用AWT?看到[這個答案](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978)有很多很好的理由放棄AWT組件而轉向Swing。 .. –

+0

.. 3)請參閱[Java插件支持棄用](http://www.gizmodo.com.au/2016/01/rest-in-hell-java-plug-in/)和[移至插件免費網頁](https://blogs.oracle.com/java-platform-group/entry/moving_to_a_plugin_free)。 4)使用合乎邏輯的一致形式縮進代碼行和塊。縮進旨在使代碼的流程更易於遵循! 5)AFAIR,小程序查看器不實現'showDocument(..)' –

+0

可能的重複[如何使用java打開默認webbrowser](http://stackoverflow.com/questions/5226212/how-to-open-在-默認網頁瀏覽器,使用的Java) – progyammer

回答