可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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;