0

我有一个带有 Swing GUI 的 Java 程序,当我在 Eclipse 上测试它时,我试图让它在 HTML 文件上作为 JApplet 工作,将它作为 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
        }
}
4

1 回答 1

1

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

code="Reverser.class"

code="Applets.Reverser.class"

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

您还需要发布浏览器给您的错误消息。据我们所知,您可能存在版本不兼容问题。

于 2013-06-12T21:15:38.463 回答