2013-06-12 77 views
0

我有一個帶有Swing GUI的Java程序,當我在Eclipse上測試它時,它試圖使它在HTML文件上作爲JApplet工作,將它作爲Applet運行,但是當它編譯它時使用javac。我得到所有這些文件Reverser.class,Reverser $ 1.class,Reverser $ 2.class,Reverser $ 3.class和Reverser $ 4.class。它不工作的幫助JApplet到JApplet在瀏覽器中不起作用

<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 
     } 
} 
+0

*「我有一個帶有Swing GUI的Java程序,我試圖使它在HTML文件上作爲JApplet工作。」*對於部署Java桌面應用程序,最好的選擇通常是安裝該應用程序。使用[Java Web Start](http://stackoverflow.com/tags/java-web-start/info)。 JWS適用於Windows,OS X和* nix。 I.E.不要將幀轉換爲applet,只需使用JWS啓動幀即可。 –

回答

1

你的小程序中沒有包名稱。考慮更改

code="Reverser.class" 

code="Applets.Reverser.class" 

,並確保類文件是相對於HTML文件的小程序子目錄英寸或者甚至更好,創建一個jar文件。

此外,您還需要發佈瀏覽器給您的錯誤消息。對於我們所知的所有情況,您可能會遇到版本不兼容問題。

+0

你是說我應該創建一個名爲Applets的文件夾,將所有類文件放在一邊,包括HTML文件並運行它?而關於jar文件我讀了它,但我不明白的清單文件的東西 – user1898552

+0

@ user1898552:是做一個文件夾稱爲小程序,因爲這是包的名稱。該HTML文件不應位於Applets文件夾中。你也需要發佈瀏覽器給你的錯誤信息。對於我們所知的所有情況,您可能會遇到版本不兼容問題。 –

+0

好吧,我得到它工作一個製作一個jar文件,就像你說的,在HTML中我放下代碼=「Applets.Reverser.class」archive =「Reverser.jar」。然而,可以說我有3個其他程序和一個主要的java java類文件,並且我編譯了所有這些文件,然後我創建了一個jar文件。在我的主GUI中,java源代碼可以如何從按鈕 – user1898552