2011-04-16 101 views
1
import java.awt.*; 
import java.applet.*; 

public class GuiExample extends Applet { 
    Button okButton; 
    TextField nameField; 

    public void init() { 
     setLayout(null); 
     okButton = new Button("A button"); 
     nameField = new TextField("A TextField",100); 
     okButton.setBounds(20,20,100,30); 
     nameField.setBounds(20,70,100,40); 

     add(okButton); 
     add(nameField); 
    } 

} 

如何將文本框中的值傳遞給'validate.jsp',點擊該按鈕時? 而且我還希望瀏覽器轉到那個jsp頁面。並從那裏繼續執行? 我該如何修改代碼?從applet傳遞值到jsp

+1

你需要自己'ActionListener'執行添加到該按鈕,其內容從文本字段的值,然後調用'this.getAppletContext()。showDocument(新URL(「驗證。 jsp?p =「+ textFieldValue))'(最終你需要對字段值進行URL編碼)。 – jCoder 2011-04-16 12:51:28

+0

@jCoder:你爲什麼不張貼這個答案?我認爲這是必要的。 – 2011-04-16 17:12:45

+0

@Paŭlo我實際上忽略了許多實現細節。我會很快發佈一個完整的答案。 – jCoder 2011-04-16 17:22:31

回答

0

這段代碼可能會有幫助。

import java.applet.*; 
import java.awt.*; 
import java.awt.event.*; 
import java.net.*; 

public class GuiExample extends Applet { 
    Button okButton; 
    TextField nameField; 
    GuiExample currentApplet; // needed for navigation 

    public void init() { 
     currentApplet = this; 
     setLayout(null); 
     okButton = new Button("A button"); 
     nameField = new TextField("A TextField",100); 
     okButton.setBounds(20,20,100,30); 
     nameField.setBounds(20,70,100,40); 

     add(okButton); 
     add(nameField); 

     okButton.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       String text = nameField.getText(); 
       if (text != null) { 
        try { 
         // modify this to point to your website 
         String address = "http://www.example.com/validate.jsp?p=" + URLEncoder.encode(text, "UTF-8"); 
         currentApplet.getAppletContext().showDocument(new URL(address)); 
        } catch (Exception ex) { 
        } 
       } 
      } 
     }); 

    } 

}