【问题标题】:Pass values entered in one JFrame's text field as an input parameter in other JFrame将在一个 JFrame 的文本字段中输入的值作为输入参数传递到另一个 JFrame
【发布时间】:2013-07-01 19:48:11
【问题描述】:

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

在第一个 JFrameJTextFields.. 中输入用户名和密码。

String usr = jTextField2.getText();
String pass = jTextField3.getText();

应该在第四帧输入相同的用户名和密码 每一帧在按钮点击时被重定向到其他帧

【问题讨论】:

  • 您的意思是将用户名和密码传递给其他 jtextfield 的框架?
  • 嗨 Azad,感谢您的回复.. :) 我不希望将数据填充到其他框架文本字段中.. 我希望将数据作为代码后端的输入..跨度>
  • 我想,我终于明白你的意思了,你的意思是每一帧都有两个字符串(用户,通行证),对吗?
  • yes Azad...我在第二种形式中使用的代码是 getLogger().info("ConnectToDevice using: "+networkElement+" : "+getUsername()+" : "+getPassword() );我需要在 form1 的文本字段中输入值

标签: java swing jframe jtextfield


【解决方案1】:

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

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

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

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

【讨论】:

【解决方案2】:

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

你的主框架

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

【讨论】:

    【解决方案3】:

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

    公共静态 JTextField txt2; public JTextField txt1,button1;

    //第一个 JFrame 中的操作按钮 1

    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;
      after it is assigning the username value to user1
      here cname is a label which name is cname;
      so, we are seeting the text of cname to the user.
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-10-02
        • 2014-10-31
        • 2021-12-26
        • 2013-12-17
        • 1970-01-01
        • 1970-01-01
        • 2017-03-21
        • 1970-01-01
        相关资源
        最近更新 更多