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

标签 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
        }
}

最佳答案

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

code="Reverser.class"

code="Applets.Reverser.class"

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

您还需要发布浏览器向您提供的错误消息。据我们所知,您可能会遇到版本不兼容的情况。

关于java - JFrame 到 JApplet 在浏览器中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17075393/

相关文章:

java - 如何从 JavaScript(AJAX 请求)向 Servlet 发送和捕获参数

java - Scrollview 中的 TabHost 在内容更改时滚动

java - 关闭 JOptionPane 后全屏卡住

java - 为什么我的 showMessageDialog 显示两次?

JavaME,实现点对点通信

java - 写入文本文件 - 写入下一行

java - JPanel 在 Paint() 方法后面渲染

java applet 将 jar 文件导出为独立的可执行文件

java - 唯一地将 Android 应用程序连接到 PC 上的 Java 小程序

java - 获取 Velocity 模板内 web 应用程序的绝对上下文路径