Pass values entered in one JFrame's text field as an input parameter in other JFrame

前端 未结 4 594
执念已碎
执念已碎 2020-12-17 07:42

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

相关标签:
4条回答
  • 2020-12-17 07:54
    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.
    
    0 讨论(0)
  • 2020-12-17 08:13

    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
     }
    }
    
    0 讨论(0)
  • 2020-12-17 08:14

    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.

    0 讨论(0)
  • 2020-12-17 08:17

    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());

    0 讨论(0)
提交回复
热议问题