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

匿名 (未验证) 提交于 2019-12-03 01:10:02

问题:

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

回答1:

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.



回答2:

You can also pass values to the constructor like this

Your main frame

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

Your next frame

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


回答3:

first create publicly static type variable

public static JTextField txt2; public JTextField txt1,button1;

//action button1 in 1st JFrame

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



回答4:

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;        
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!