简体   繁体   中英

How do I pass the values from one class to another?

I have this gui pop up panel and it got things to filled up like packets number, distance etc. Once users fill in the information, he will click ok, the gui will close and my other gui class which has calculation method should receives all data that are filled in earlier gui. How do I store that data? I know I can store in temp file but I don't want to do that. I hope you can enlighten me.

import java.awt.*;
import java.applet.Applet;

class Example extends Applet implements ActionListener
{
    TextField txt = new TextField(10);
    Button goButton = new Button("Go");
    String data = new String ();

    public void init ()
    {
        add(txt);
        add(goButton);
        goButton.addActionListener(this);
    }

    public void actionPerformed (ActionEvent e)
    {
        String cmd = e.getActionCommand();

        if (cmd.equals("Go"))
        {
            // preserve data
            data = txt.getText();

            repaint();
        }
    }
}

You should create an intermediate class that represents the data.

After the GUI has been filled in and the submit button clicked, parse the data and fill in the fields in your class.

For example:

public class MyData {
    public String Name;
    public String Address;
}

Then, fire a method in your calculation method that takes this class as a parameter:

public void Calculate(MyData data) { ... }

For more advanced handling, look into "interfaces" in Java - that's the standard way this is done.

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