JFrame到JApplet在浏览器中不起作用

时间:2013-06-12 21:08:32

标签: java swing applet japplet

我有一个带有Swing GUI的Java程序我试图让它在HTML文件上作为JApplet工作当我在Eclipse上测试它时它将它作为Applet运行它但是当我使用javac编译它时。我得到所有这些文件Reverser.class,Reverser $ 1.class,Reverser $ 2.class,Reverser $ 3.class和Reverser $ 4.class。它没有帮助

<HTML>
<BODY>
<applet code="Reverser.class", height="500" width="800">
</applet>
</BODY>
</HTML>


package Applets;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

public class Reverser extends JApplet
{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    //All Swing elements declared here
    private JTextArea userinput, useroutput;
    private JScrollPane sbr_userinput, sbr_useroutput;
    private JButton runButton, clearButton, homeButton;

    private String text; //User input stored here
    private String reversed_text; //reversed text stored here

    public void init() 
    {
        try {
            SwingUtilities.invokeAndWait(new Runnable() {
                public void run() {
                setContentPane(GUI());
                }
            });
        } catch (Exception e) { 
            System.err.println("createGUI didn't complete successfully");
        }
    }

    public Container GUI() //Main GUI container here
    {
        JPanel totalGUI = new JPanel(); //Main panel set here
        totalGUI.setLayout(new GridLayout(1, 2, 3, 3)); //Main panel layout set here.

        JPanel lPanel = new JPanel(); //Left panel made here
        lPanel.setLayout(new GridLayout(2, 1, 3 , 3)); //Left panel layout set here
        totalGUI.add(lPanel); // Left Panel added to main panel.

        JPanel rPanel = new JPanel(new GridLayout(3, 1, 3 , 3)); //Right panel made here and its layout set here aswell
        totalGUI.add(rPanel);//Right panel added to main panel.

        //Userinput TextArea made here 
        userinput = new JTextArea("Welcome to wicky waky text reverser!!!" + "\n" + "Enter your sentence HERE man!!!!");
        userinput.setEditable(true); //TextArea set to editable
        userinput.setLineWrap(true);
        userinput.setWrapStyleWord(true);
        lPanel.add(userinput);//TextArea added to left panel

        useroutput = new JTextArea(); //useroutput TextArea set here
        useroutput.setEditable(false); //TextArea set to not editable
        useroutput.setLineWrap(true);
        useroutput.setWrapStyleWord(true);
        lPanel.add(useroutput); //TextArea added to the left panel

        //Scroll bar made here
        sbr_userinput = new JScrollPane(userinput, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, 
                JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        sbr_userinput.setPreferredSize(new Dimension(300, 300));
        lPanel.add(sbr_userinput); //Scroll bar added to left panel

        //Scroll bar made here
        sbr_useroutput = new JScrollPane(useroutput, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, 
                JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        sbr_useroutput.setPreferredSize(new Dimension(300, 300));
        lPanel.add(sbr_useroutput); //Scroll bar added to the left panel

        runButton = new JButton("RUN"); //Button made here
        runButton.addActionListener(new ActionListener() //Action Listener made here
        {
            public void actionPerformed(ActionEvent e)
            {
                text = userinput.getText(); //Get userinput here
                reversed_text = reverser(text); //reverser method called here
                try {
                    userinput.setText("");
                    userinput.setText("Processed");
                    Thread.sleep(500); //sleep 0.5 sec
                    //Print out all output here
                    useroutput.setText("Your sentence is ==> " + text + "\n" + "\n"
                    + "Your reversed sentence is ==>  " + reversed_text);
                } catch (InterruptedException e1) 
                {

                    e1.printStackTrace();
                }

                System.out.println("working");
            }
        });
        rPanel.add(runButton); //Add button to right panel

        clearButton = new JButton("CLEAR"); //New button made here
        clearButton.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                userinput.setText(""); //Set userinput to empty
                useroutput.setText(""); //Set useroutput to empty
                System.out.println("cleared");
            }
        });
        rPanel.add(clearButton); //Add button to right panel

        homeButton = new JButton("HOME"); //New button made here
        homeButton.addActionListener(new ActionListener() //Action Listener added here
        {
            public void actionPerformed(ActionEvent e)
            {
            }
        });
        rPanel.add(homeButton); //add button to right panel

        totalGUI.setOpaque(true);
        return totalGUI; // return totalGUI
    }

    public static String reverser(String text)
        {
            //"text" is now put in the object "reverse_text", StringBuffer used so I can use the reverse method.
            StringBuffer reverse_text = new StringBuffer(text);

            String reversed = reverse_text.reverse().toString();   //reverse and toString methods used.

            return reversed; // return revered
        }
}

1 个答案:

答案 0 :(得分:1)

您的小程序中没有包名称。考虑改变

code="Reverser.class"

code="Applets.Reverser.class"

并确保类文件位于相对于HTML文件的Applets子目录中。或者甚至更好,创建一个jar文件。

此外,您还需要发布浏览器提供给您的错误消息。您可能知道我们所知道的版本不兼容。