有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

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

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

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

应在第四帧中输入相同的用户名和密码 单击按钮时,每个帧都会重定向到其他帧


共 (4) 个答案

  1. # 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. # 2 楼答案

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

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

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

    JFrame2。setVisible(真); JFrame2。txt2。setText(Me.txt1.getText())

  3. # 3 楼答案

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

    你的主框架

    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
     }
    }
    
  4. # 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.