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

Java 传递在一个JFrame'中输入的值;s文本字段作为其他JFrame中的输入参数,java,swing,jframe,jtextfield,Java,Swing,Jframe,Jtextfield,如何将在一个JFrame的文本字段中输入的值作为输入参数传递给另一个JFrame 通过JTextFields在第一个JFrame中输入用户名和密码 String usr = jTextField2.getText(); String pass = jTextField3.getText(); 应在第四帧中输入相同的用户名和密码 单击按钮时,每个帧都会重定向到其他帧。如果有许多帧,则必须为此创建实例变量。 如果您不知道什么是实例变量,请参见此。 让我们看一个例子: 这将是发送变量的帧: publ

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

通过
JTextFields
在第一个
JFrame
中输入用户名和密码

String usr = jTextField2.getText();
String pass = jTextField3.getText();
应在第四帧中输入相同的用户名和密码
单击按钮时,每个帧都会重定向到其他帧。如果有许多帧,则必须为此创建实例变量。 如果您不知道什么是实例变量,请参见此。 让我们看一个例子:

这将是发送变量的帧:

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

注意:我没有编译代码,这只是为了演示您的问题。

假设您有许多帧,您必须为此创建实例变量。 如果您不知道什么是实例变量,请参见此。 让我们看一个例子:

这将是发送变量的帧:

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

注意:我没有编译代码,这只是为了演示您的问题。

您也可以像这样将值传递给构造函数

你的主框架

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);
     } 
} 
你的下一帧

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

您还可以像这样将值传递给构造函数

你的主框架

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);
     } 
} 
你的下一帧

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

首先创建一个静态类型变量

公共静态JTextField txt2; 公共JTextField txt1,按钮1

//第一帧中的动作按钮1

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

首先创建公共静态类型变量

公共静态JTextField txt2; 公共JTextField txt1,按钮1

//第一帧中的动作按钮1

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

您的意思是将用户名和密码传递给其他jtextfield的帧吗?嗨,Azad,谢谢您的回复:)我不希望在其他框架文本字段中填充数据。。我希望数据作为代码后端的输入..我想,我终于理解了你,你的意思是每个帧有两个字符串(用户,通过),对吗?是的Azad…我在第二种形式中使用的代码是getLogger().info(“ConnectToDevice using:”+networkElement+:“+getUsername()+”:“+getPassword());为此,我需要在Form1的文本字段中输入值。请参见您的意思是将用户名和密码传递给其他jtextfield的帧?嗨,Azad,谢谢您的回答:)我不希望在其他框架文本字段中填充数据。。我希望数据作为代码后端的输入..我想,我终于理解了你,你的意思是每个帧有两个字符串(用户,通过),对吗?是的Azad…我在第二种形式中使用的代码是getLogger().info(“ConnectToDevice using:”+networkElement+:“+getUsername()+”:“+getPassword());为此,我需要在Form1的文本字段中输入值,请参见非常感谢AZAD。。你使我的一天:“RaghuNandiraju:不客气,这是你的第一个问题,所以,请考虑答案是否被接受。”请参阅关于SO的此问题:。看,这很有用。非常感谢你,阿扎德。。你使我的一天:“RaghuNandiraju:不客气,这是你的第一个问题,所以,请考虑答案是否被接受。”请参阅关于SO的此问题:。看,这是非常有用的。