将在一个JFrame文本字段中输入的值作为其他JFrame中的输入参数传递

1 java swing jframe jtextfield

如何将在一个JFrame的文本字段中输入的值作为输入参数传递给其他JFrame?

在第一次进入用户名和密码JFrame通过JTextFields..

String usr = jTextField2.getText();
String pass = jTextField3.getText();
Run Code Online (Sandbox Code Playgroud)

相同的用户名和密码应在第四帧中作为输入给出,每个帧在按钮点击时被重定向到其他帧

Aza*_*zad 7

假设您有许多帧,则必须为此目的创建实例变量.如果您不知道实例变量是什么,请参阅本教程.让我们看一个例子:

这将是您发送变量的框架:

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);
 }
Run Code Online (Sandbox Code Playgroud)

这将是你的第一帧:

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
    }
}
Run Code Online (Sandbox Code Playgroud)

注意:我没有编译代码,这仅用于演示您的问题.