简体   繁体   中英

How to get data from JFrame 1 to another JFrame?

Upon reading up on lots of question being posted and answered by people, I still don't get how the passing of data works in Java. I have a simple code right here that require the user to type in their age, height, weight and a button that will open up the 2nd Frame.

First Frame

public Collectdata()
{
    JPanel text = new JPanel();
    text.add(jage);
    text.add(age);
    text.add(jheight);
    text.add(height);
    text.add(jweight);
    text.add(weight);
    text.setLayout(new GridLayout(3,2));

    JPanel jbutt = new JPanel();
    jbutt.add(next);

    setLayout(new BorderLayout());
    add(text,BorderLayout.CENTER);
    add(jbutt,BorderLayout.SOUTH);

    next.addActionListener(this);
}

public static void main(String[] args)
{
    Collectdata GUI = new Collectdata();
    GUI.setTitle("DataCollection");     //Set title
    GUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //close program
    GUI.setSize(250,150);
    GUI.setVisible(true);
}

public void actionPerformed(ActionEvent a)
{
    if(a.getSource() == next)
    {
        Calculate secondwind = new Calculate(this);
        secondwind.setTitle("Calculate");       //Set title
        secondwind.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  //close program
        secondwind.setSize(350,150);
        secondwind.setVisible(true);
        this.setVisible(false); 
    }
}

}

And this is the 2nd Frame that will be shown after clicking the button.

2nd Frame

public Calculate(Collectdata collectdata)
{
    this.collectdata = collectdata;

    JPanel text = new JPanel();
    text.add(jage);
    text.add(age);
    text.add(jheight);
    text.add(height);
    text.add(jweight);
    text.add(weight);
    text.setLayout(new GridLayout(3,2));

    age.setEditable(false);
    height.setEditable(false);
    weight.setEditable(false);

    setLayout(new BorderLayout());
    add(text,BorderLayout.CENTER);
}

I will like to be able to get the data keyed from the JFrame to be able to pass over to the 2nd JFrame and using the data for some calculation.

How do I do that?

Thanks.

Typically, when requesting/prompting information from the user, you would use a dialog of some kind, this is a short-lived, single purpose window, whose job should be to either collect a small amount of information from the user, ask a question or provide some kind of informative notification.

You need to maintain isolation of responsibility and try and de-couple your code as much as possible, for example, your Calculate class shouldn't care how the information is generated and your collection class shouldn't care what you're going to do with the information.

To this end, start by creating a custom class, extending from JPanel which contains all the fields you need to collect the data from the user...

public class UserInfoEditorPane extends JPanel {
    private JSpinner jage;
    private JSpinner jheight;
    private JSpinner jweight;

    public UserInfoEditorPane() {  
        // Set the UI as you want...
    }

    public Collectdata getUserInfo() {
        // Create a new instance of Collectdata
        // and populate it with the values from
        // the fields...
        Collectdata data = ...;
        //...
        return data;
    }
}

(nb I'm using Collectdata as a POJO, not a UI class)

Now, using something like a JOptionPane , display the panel to the user...

UserInfoEditorPane userInfoPane = new UserInfoEditorPane();
switch (JOptionPane.showMessageDialog(null, userInfoPane, "User Info", JOptionPane.PLAIN_MESSAGE)) {
    case JOptionPane.OK_OPTION:
        Calculate secondwind = new Calculate(userInfoPane.getUserInfo());
        secondwind.setTitle("Calculate");       //Set title
        secondwind.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  //close program
        secondwind.pack();
        secondwind.setLocationRelativeTo(null);
        secondwind.setVisible(true);
        break;
}

See How to Make Dialogs for more details

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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