Java 如何在 Netbeans 中将变量值从一个 JFrame 传递到另一个 JFrame

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9834915/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 08:22:29  来源:igfitidea点击:

How to pass a variable value from one JFrame to Another JFrame in Netbeans

javaswingnetbeansjframe

提问by user1225502

I have two JFrames login.javaand account.java
I need to get the usernamefrom the login.javapage and put it in a variable in the account.javaJFrame. How can I do this in the Java NetBeans using the Swing?

我有两个 JFrame login.javaaccount.java
我需要usernamelogin.java页面中获取并将其放入account.javaJFrame. 如何使用 Swing 在 Java NetBeans 中执行此操作?

回答by Vinay

You can use getter and setter methods...

您可以使用 getter 和 setter 方法...

Set the username in a setter. And using object of login.java use it in account.java through getter...

在 setter 中设置用户名。并使用 login.java 的对象通过 getter 在 account.java 中使用它...

public class login {
    private String username;
    private String password;

    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = this.usernameTextField.getText();
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = this.passwordTextField.getText();
    }
}

Using objects of login.java access getPassword(), getUsername()in account.java. you need to pass object of login.java to account.java first...

使用的login.java访问对象getPassword()getUsername()在account.java。您需要先将 login.java 的对象传递给 account.java...

回答by Neo

Instead of Using JFrames for passing values between different forms you can use CardLayout which will persist your data which you have entered in the previous form. All you have to do is Create a JFrameForm and add panels to it.

您可以使用 CardLayout,而不是使用 JFrames 在不同表单之间传递值,它将保留您在前一个表单中输入的数据。您所要做的就是创建一个 JFrameForm 并向其添加面板。

回答by Krishna

Well you have very nice way to do it.

那么你有很好的方法来做到这一点。

Define new Static Final Objects of that class.and Save that value into the Object.

定义该类的新静态最终对象。并将该值保存到对象中。

and in other Class u can easily use that objects and as well as that values. By using

在其他类中,您可以轻松使用该对象以及该值。通过使用

CLASSNAME.OBJECT VALUE.

类名.对象值。

use that.

用那个。

回答by Ankur pandey

Since you have asked how to pass a variable value from one JFrame to Another JFrame (using swing). So for this put one textbox(tx) and a button(jButton3) in login.java and one label(lx) in account.java where we will print the value of the textbox from login.java .

由于您询问了如何将变量值从一个 JFrame 传递到另一个 JFrame(使用 swing)。因此,为此在 login.java 中放置一个文本框(tx)和一个按钮(jButton3),在 account.java 中放置一个标签(lx),我们将从 login.java 打印文本框的值。

Type this in login.java :-

在 login.java 中输入:-

 private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
      String msg= tx.getText();
      new NewJFrame2(msg).setVisible(true);
    } 

Then overload constructor in account.java :-

然后在 account.java 中重载构造函数:-

public NewJFrame2(String abc ){
        initComponents();
        lx.setText(abc);
    }

回答by Ankur pandey

The 100% working solution. Suppose u r calling welcome.java

100% 有效的解决方案。假设你调用welcome.java

Account ac= new Account(new JFrame(), true);

After this line call a method of welcome.java which u have to create like:

在此行之后调用welcome.java 的方法,您必须创建该方法,例如:

wc.setUser(username);

For account.java

账号.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:

User1 是全局变量,可用于您必须定义的所有 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.

在将用户名值分配给 user1 之后。这里 cname 是一个名称为 cname 的标签;因此,我们正在向用户显示 cname 的文本。