将在一个JFrame文本字段中输入的值作为其他JFrame中的输入参数传递 [英] Pass values entered in one JFrame's text field as an input parameter in other JFrame
问题描述
如何将在一个JFrame文本字段中输入的值作为其他JFrame中的输入参数传递?
How to pass values entered in one JFrame's text field as an input parameter in other JFrame?
在第一个 JFrame中输入用户名和密码
通过 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
推荐答案
假设您有许多帧,则必须为此目的创建实例变量。
如果您不知道实例变量是什么,请参阅教程。
让我们看一个例子:
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);
}
这将是你的第一帧:
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.
这篇关于将在一个JFrame文本字段中输入的值作为其他JFrame中的输入参数传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!