我应该如何将值从JFrame传递给另一个?

时间:2022-09-10 23:27:11

I have this logical problem regarding on how to pass value from one JFrame to another.

关于如何将值从一个JFrame传递到另一个JFrame,我遇到了这个逻辑问题。

What I want to do:

我想做的事:

Since all value inputted by the user were automatically saved into a database, I want to capture first the ID and then pass it on to another JFrame were it will show the recently stored data.

由于用户输入的所有值都自动保存到数据库中,我想首先捕获ID,然后将其传递给另一个JFrame,它将显示最近存储的数据。

So, basically I have a JFrame were the user will fill up all the required information, after hitting the submit button, I want to show another JFrame showing that the addition of an Item were successful while showing the previous data inputted.

所以,基本上我有一个JFrame是用户将填写所有必需的信息,在点击提交按钮后,我想显示另一个JFrame显示添加一个项目成功,同时显示输入的先前数据。

Problem:

I don't know how to pass the value I have got(using a database query) from the first JFrame to another JFrame(where the user will see the complete information recently stored).

我不知道如何将我从第一个JFrame获得的值(使用数据库查询)传递给另一个JFrame(用户将看到最近存储的完整信息)。

here's a sample code:

这是一个示例代码:

        Statement stat = con.createStatement();
        ResultSet rs = stat.executeQuery("SELECT cId, LastName, FirstName, "
                + "Address, TelNo, Email from pcontacts WHERE FirstName ='"
                +firstname.getText()+"' AND LastName ='"+lastname.getText()+"'");

        int cId;
        String LastName, FirstName, Address, TelNo, Email;
        if(counter == 0){
            if(rs.next()) {
                //cId = rs.getInt("cId");
                LastName = rs.getString("LastName");
                FirstName = rs.getString("FirstName");
                Address = rs.getString("Address");
                TelNo = rs.getString("TelNo");
                Email = rs.getString("Email");

                fname.setText(FirstName);
                lname.setText(LastName);
                address.setText(Address);
                contact.setText(TelNo);
                email.setText(Email);
                counter++;

How can i pass the cId to another JFrame?

如何将cId传递给另一个JFrame?

Please Help. Thanks in Advance :)

请帮忙。提前致谢 :)

PS

hope you understand my explanation.. if you have any other questions regarding my explanation.. please do tell me, I will try my best to explain it further. Thanks again

希望您理解我的解释..如果您对我的解释有任何其他疑问......请告诉我,我会尽力进一步解释。再次感谢

2 个解决方案

#1


0  

The prefered way in many cases is to use PropertyChangeListeners, look at tutorial at http://download.oracle.com/javase/tutorial/uiswing/events/propertychangelistener.html

在许多情况下,首选的方法是使用PropertyChangeListeners,查看http://download.oracle.com/javase/tutorial/uiswing/events/propertychangelistener.html上的教程。

#2


0  

You can create a custom class extending JFrame, with a private member in it, and initialize it in the constructor.

您可以创建一个扩展JFrame的自定义类,其中包含一个私有成员,并在构造函数中初始化它。

#1


0  

The prefered way in many cases is to use PropertyChangeListeners, look at tutorial at http://download.oracle.com/javase/tutorial/uiswing/events/propertychangelistener.html

在许多情况下,首选的方法是使用PropertyChangeListeners,查看http://download.oracle.com/javase/tutorial/uiswing/events/propertychangelistener.html上的教程。

#2


0  

You can create a custom class extending JFrame, with a private member in it, and initialize it in the constructor.

您可以创建一个扩展JFrame的自定义类,其中包含一个私有成员,并在构造函数中初始化它。