2014-10-27 78 views
0

我想用java applet構建一個非常簡單的程序。爲什麼Java Applet不顯示?

程序應該導航到相應的網站,因爲用戶點擊它們出現在applet窗口上的鏈接。

但在我的情況下,沒有applet窗口彈出!那麼有一個窗口彈出(下圖是圖像)window popping up(只要我運行代碼)這是不是我的程序相關。

的.java:

package stringpractice; 

import java.awt.*; 
import java.applet.*; 
import javax.swing.*; 
import javax.swing.event.*; 
import java.util.*; 
import java.net.*; 
import javax.swing.plaf.basic.BasicListUI; 

public class Applet extends JApplet { 
    private HashMap<String,URL>websiteInfo; 
    private ArrayList<String>titles; 
    private JList mainList; 

    private void inIt(){ 
     websiteInfo =new HashMap<String, URL>(); 
     titles =new ArrayList<String>(); 
     grabHTMLInfo(); 

     add(new JLabel("What website u wanna visit?"),BorderLayout.NORTH); 
     mainList=new JList(titles.toArray()); 

     mainList.addListSelectionListener(


     new ListSelectionListener() { 

      //@Override 
      public void valueChanged(ListSelectionEvent event) { 

       Object object = mainList.getSelectedValue(); 
       URL newDocument = websiteInfo.get(object); 
       AppletContext browser = getAppletContext(); 
       browser.showDocument(newDocument); 

      } 
     } 
); 
     add(new JScrollPane(mainList),BorderLayout.CENTER); 

    } 
    private void grabHTMLInfo(){ 
     String title; 
     String address; 
     int counter=0; 
     URL url; 
     title=getParameter("title"+counter); 
     while(title!=null){ 
      address=getParameter("address"+counter); 
      try { 
       url =new URL(address); 
       websiteInfo.put(title, url); 
       titles.add(title); 
      } catch (MalformedURLException uRLException) { 

       uRLException.printStackTrace(); 
      } 
      ++counter; 
      title=getParameter("title"+counter); 
     } 



    } 


} 

的.java主:

package stringpractice; 

public class appletMain { 

    public static void main(String[] args) { 

    } 

} 

.HTML:

<html> 
<title>testing applet</title> 

<body> 

<applet code="Applet.class" width="500" height="200"> 

<param name="title0" value="Google"> 
<param name="address0" value="https://www.google.com/?gfe_rd=cr&ei=M8ovVKu9AujJ8gfH8oH4Cw"> 


<param name="title1" value="Yahoo"> 
<param name="address1" value="https://se.yahoo.com/"> 



</applet> 

</body> 



</html> 
+2

將init方法聲明更改爲:'public void init()' – user432 2014-10-27 22:56:31

+1

Applets不應該有'main'方法 – MadProgrammer 2014-10-27 22:56:54

+0

user432,這更糟糕。沒有幫助。它所做的只是停止顯示小程序窗口(在我添加公共之後)。 – Lolita 2014-10-27 23:00:38

回答

0

init()方法寫成inIt()。由於一切都區分大小寫,您的init方法從未被調用過。