简体   繁体   English

如何将值从一类传递到另一类?

[英]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. 我有这个gui弹出面板,它填写了一些信息,例如数据包号,距离等。一旦用户填写了信息,他将单击ok,该gui将关闭,而我的其他具有计算方法的gui类应接收所有数据在早期的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. 填写完GUI并单击Submit按钮后,解析数据并填写班级中的字段。

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) { ... } 公共无效的Calculate(MyData数据){...}

For more advanced handling, look into "interfaces" in Java - that's the standard way this is done. 要进行更高级的处理,请查看Java中的“接口”,这是完成此操作的标准方法。

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

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