2013-07-01 12 views
1

如何將在一個JFrame的文本字段中輸入的值作爲輸入參數傳遞給其他JFrame?在一個JFrame的文本字段中輸入的值作爲其他JFrame中的輸入參數

輸入的用戶名和在第一JFrame通過JTextFields密碼..

String usr = jTextField2.getText(); 
String pass = jTextField3.getText(); 

相同的用戶名和密碼應在第四幀 每個幀被重定向到其它按鈕被給定爲輸入點擊

+0

您的意思是將用戶名和密碼傳遞給其他jtextfield的框架? – Azad

+0

嗨Azad,感謝您的迴應.. :)我不希望數據在其他框架文本字段中填充..我想在代碼後端輸入數據.. –

+0

我想,我終於理解了你,你的意思是每個框架都有兩個字符串(用戶,通行證),我說得對嗎? – Azad

回答

6

假設你有很多幀,你必須爲此創建實例變量。 如果您不知道什麼是實例變量,請參閱tutorial。 讓我們看一個例子:

這將是你的幀發送變量:

public class MainFrame { 
    public void actionPerformed(ActionEvent ev) { 
    String user = userField.getText(); 
    String pass = passField.getText(); 
    FrameOne frameOne = new FrameOne(); 
    frameOne.setUser(user); 
    frameOne.setPass(pass); 

    /* 
    * You've passed the user and pass to other frame, 
    * now you can make it visible. 
    */ 
    frameOne.setVisible(true); 
} 

,這將是您的第一幀:

public class FrameOne extends JFrame { 
    private JTextField userField; 
    private JTextField passField; 

    // then create setters and getter 
    public void setUser(String user) {this.userField.setText(user);} 
    public String getUser() {return this.userField.getText();} 

    public void setPass(String pass) {this.passField.setText(pass);} 
    public String getPass() {return this.passField.getText();} 

    public FrameOne() { 
     //define the components here 
    } 
} 

注:我沒有編譯代碼,這只是爲了演示你的問題。

+0

非常感謝你AZAD ..你做了我的一天:) –

+0

@RaghuNandiraju:不客氣,這是你的第一個問題,所以請考慮答案是否被接受。在SO上看到這個問題:[接受答案如何工作?](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work)。 並參見[StackOverFlow/about](http://stackoverflow.com/about)它非常有用。 – Azad

3

您也可以值傳遞給構造這樣

你的主框架

public class MainFrame{ 
     // 
     public void actionPerformed(ActionEvent ev){ 

     FrameOne frameOne = new FrameOne(userField.getText(), passField.getText()); 

     //you've passed the user and pass to other frame. 
     // then you can make it visible. 
     frameOne.setVisible(true); 
    } 
} 

你的下一個幀

public class FrameOne extends JFrame{ 
    private String user; 
    private String pass; 

    public FrameOne(String usr, String pas){ 
    this.user=usr; 
    this.pass=pas; 
    //define the components here 
} 
} 
0

首先創建公開靜態類型的變量

公共靜態JTextField txt2; public JTextField txt1,button1;在第一的JFrame

//動作button1的

JFrame2.setVisible(真); JFrame2.txt2.setText(Me.txt1.getText());

+1

正確縮進您的代碼。 – surajsn

0
Suppose u have two class like this: 

for login.java 
---------------- 
suppose u r calling welcome.java: 
Welcome wc= new Welcome(new JFrame(), true); 
after this line call a method of welcome.java which u have to create like: 
wc.setUser(username); 

for welcome.java 
------------------ 

create a method:void setUser(String username) { 
     user1 = user; 
     cname.setText(user1); 
    } 

user1 is global variable and available for all which u have to define lke: 
String user1; 
after it is assigning the username value to user1 
here cname is a label which name is cname; 
so, we are seeting the text of cname to the user. 
相關問題