简体   繁体   English

如何在不同的Jframe中获取文本字段值?

[英]how to get a textfield value across different jframes?

I'm creating a program that uses several different interacting menus to allow the user to access different parts of the system. 我正在创建一个使用几个不同的交互菜单的程序,以允许用户访问系统的不同部分。 I'm using Netbeans to help with the coding. 我正在使用Netbeans来帮助编码。

At the moment I'm stuck on a task. 此刻我被困在一项任务上。

When a user logs into the system through a "login" form, the system validates the details, and that user is redirected to either a "user_details" or an "admin_menu" depending on the credentials. 当用户通过“登录”表单登录系统时,系统将验证详细信息,并根据凭据将该用户重定向到“ user_details”或“ admin_menu”。 That much works fine. 这样很好。 From there, the user is able to access a form that allows them to update their details which are already saved in the database. 从那里,用户可以访问一个表格,该表格允许他们更新其已保存在数据库中的详细信息。

The only way I've found to limit the simple user to update their details is to ask them to login again, and from there retrieve their details so that they can be updated. 我发现限制简单用户更新其详细信息的唯一方法是要求他们再次登录,然后从那里检索其详细信息以便进行更新。 This process is messy, but it works. 这个过程很麻烦,但是可以。

with that in mind how do i retrieve whatever was imputed in the textfield "Username" that is located in the jform Login from another jform (User_details), the User_details jform only opens once the login is successful (once that occurs login is discarded and user_details is opened). 考虑到这一点,我如何从另一个jform(User_details)中检索位于jform登录名中的“用户名”文本字段中的所有内容,因此User_details jform仅在登录成功后才打开(一旦发生登录便被丢弃,并且user_details打开)。

by the way i've done a lot of research but cant seem to find an answer to my problem. 顺便说一下,我已经做了很多研究,但似乎找不到我的问题的答案。

here is part of my log in code : 这是我的登录代码的一部分:

String sql = "select * from user where Username =? and password=?";
        try {
            pst = con.prepareStatement(sql);
            pst.setString(1, username.getText());
            pst.setString(2, password.getText());
            rs = pst.executeQuery();

            int count = 0;
            while (rs.next()) {
                count = count + 1;
            }
            if (count == 1) {
                JOptionPane.showMessageDialog(null, "Access Granted");
                if ("manager@manage.com".equals(t.getText())) {
                    rs.close();
                    pst.close();
                    close();
                    Admin_menu am = new Admin_menu();
                    am.setVisible(true);
                } else {
                    rs.close();
                    pst.close();
                    close();
                    User_details M = new User_details();
                    M.setVisible(true);
                }
            } else {
                JOptionPane.showMessageDialog(null, "Incorrect Username or Password");
            }
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, ex);

        } finally {
            try {
                rs.close();
                pst.close();
            } catch (Exception ex) {
            }
        }

how can i make the User_details Jform get what ever was imputed in the textfield username? 我如何才能使User_details Jform获得在文本字段用户名中插入的内容?

You will probably want to create some class which represents the state of everything a logged in user does in your application. 您可能需要创建一个类,该类表示登录用户在应用程序中所做的所有操作的状态。 Traditionally, you would call it a Session. 传统上,您将其称为会话。 The logged in user is a property of that session, so you keep it in a field there. 登录的用户是该会话的属性,因此您将其保留在该字段中。 You pass the session into every frame you make, so that they all have access to it. 您将会话传递到所创建的每个框架中,以便他们都可以访问它。

The session becomes invalid when the user logs out. 用户注销后,该会话无效。

Then from your frames, components, whatever, you can get the current user from the session. 然后,从您的框架,组件等中获取会话中的当前用户。

But please, try to separate front-end from back-end code here. 但是请尝试在此处将前端代码与后端代码分开。 Look into the MVC pattern . 查看MVC模式

try to declare the variables public and initiailize them with the other jframes where you want to use them for example 尝试将变量声明为public,并与要使用它们的其他jframe一起初始化它们

public your class{

    initComponents();
    String a;


}

hope this helps :) 希望这可以帮助 :)

You could create a class "BasicView" in wich a class variable exists where you store your information, so every instance of this class can use ist. 您可以在存储信息的类变量存在的地方创建一个“ BasicView”类,因此该类的每​​个实例都可以使用ist。 Then you just need to extend this basic class, or just use BasicView.value or BasicView.getValue(). 然后,您只需要扩展该基本类,或者仅使用BasicView.value或BasicView.getValue()。 Also you can use a synchronized method to change this var. 您也可以使用同步方法来更改此变量。

public class BasicView {
    private static Object value;

    public synchronized void setValue(Object value) {
        this.value = value;
    }

    public Oject getValue() {
        return value;
    }
    .
    .
    .
}

I use this to init an ServicesLocator to get servies in all my beans. 我用它来初始化一个ServicesLocator来获取我所有豆子中的servies。 :) :)

EDIT: Also take a look at the MVC pattern and use this in the controller (Basic Controller) or something like that. 编辑:还可以看一下MVC模式,并在控制器(基本控制器)或类似的东西中使用它。

1. Use Composition , and also make sure that your GUI class always returns the same Object when referenced from different JFrames. 1.使用Composition ,并确保从不同的JFrames引用时 ,您的GUI类始终返回相同的Object。

2. This can be achieved using Singleton Principle . 2.这可以使用Singleton Principle来实现。

Create a class called session like this: 创建一个名为session的类,如下所示:

public class Session {

    public static String userName = "";

}

When login happens set the value of Session.userName to the given userName and when the user logs out set Session.userName to "". 登录时,将Session.userName的值设置为给定的userName,当用户注销时,将Session.userName设置为“”。 You can access Session.userName simply by typing Session.userName. 您只需键入Session.userName即可访问Session.userName。 Other session variables should be defined in the Session class too. 其他会话变量也应在Session类中定义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM