简体   繁体   中英

Why Java Applet isn't displaying?

I am trying to build a very simple program with java applet.

The program should navigate to corresponding website as an user click on their links which are appearing on the applet window.

But in my case no applet window is popping up! Well there is a window popping up (below is the image) 弹出窗口 (as soon as I run the code) which is not the relevant with my program.

.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 main:

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>

Your init() method is written as inIt() . As everything is case sensitive, your init method gets never called.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM