Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
362 views
in Technique[技术] by (71.8m points)

java - Pass values entered in one JFrame's text field as an input parameter in other JFrame

How to pass values entered in one JFrame's text field as an input parameter in other JFrame?

Entered user name and password in first JFrame through JTextFields..

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

Same username and password should be given as input in forth frame each frame is redirected to other on button click

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Suppose you have many frames, you have to create instance variables for that purpose. If you don't know what an instance variable see this tutorial. Lets see an example:

This will be your frame that sends the variables :

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);
 }

And this will be your first frame:

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

NOTE : I didn't compile the code, this is only for demonstration on your problem.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...