简体   繁体   English

如何跟踪 Java 应用程序中的所有组件?

[英]How does one keep track of all the components in a Java Application?

I'm more interested in understanding how it's done in the real world.我更感兴趣的是了解它在现实世界中是如何完成的。

I am building an application in Javafx where it is listening to INPUT within a webpage using webview.我正在 Javafx 中构建一个应用程序,它使用 webview 在网页中侦听INPUT When the user clicks an input a new Button("Input Clicked") is created and added to a GridPane, at the bottom.当用户单击输入时,会创建一个新的 Button("Input Clicked") 并将其添加到底部的 GridPane 中。 The user can then click the button and a dialog will appear for information, like the inputs ID, Class, Value, etc.然后用户可以单击该按钮,将出现一个对话框以获取信息,例如输入 ID、类、值等。

If a webpage has 10+ inputs, how does one keep track of all the new Button()s created?如果一个网页有 10 多个输入,那么如何跟踪所有新创建的 Button()? Do I write to a file?我要写入文件吗? Do I create an arraylist?我要创建一个数组列表吗? If so, what if the user closes the application?如果是这样,如果用户关闭应用程序怎么办? How do I recreate the GridPane, almost like a File->Save feature?如何重新创建 GridPane,就像文件->保存功能一样?

I've attached an image of the application.我附上了应用程序的图像。 As you can see, I have clicked the google search INPUT 4 times, 4 buttons were created.如您所见,我已单击 google 搜索 INPUT 4 次,创建了 4 个按钮。 Each one needs to be unique.每一个都必须是独一无二的。 If I close the application, it needs to recreate it.如果我关闭应用程序,它需要重新创建它。

the code is simply代码很简单

mainGrid.add(new Button("Input Clicked",IncrementCol,IncrementRow);

Example例子

One way of keeping track of your input controls is through a pattern called " Model-View-Controller ".跟踪输入控件的一种方法是通过称为“ 模型-视图-控制器”的模式。 The state of the input controls is captured in the Model, which you can persist and then restore later.输入控件的状态在模型中捕获,您可以保留它,然后在以后恢复。

Something like this.像这样的东西。 The UI class combines the View (the JTextField and JLabel , etc.) and the Controller ( ActionListener ). UI类结合了ViewJTextFieldJLabel等)和ControllerActionListener )。 You might want to separate these into different classes in a large enough UI.您可能希望在足够大的 UI 中将它们分成不同的类。 The Model class holds all the state, that the Controller shifts between the View and the Model . Model类保存所有状态,即ControllerViewModel之间转换。

This allows you to keep track of the things in the UI.这允许您跟踪 UI 中的内容。

public UI extends JFrame {
    private Model model;
    private JTextField fn;
    private JTextField ln;
    private JLabel n;

    public JFrame(Model model) {
        this.model = model;
        fn = JTextField(model.getFirstName());
        ln = JTextField(model.getLastName());
        n = new JLabel(model.getFullName());
        JButton b = new JButton("Submit");
        JButton s = new JButton("Save");
        JButton r = new JButton("Restore");
        b.setActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ev) {
                model.setFirstName(fn.getText());
                model.setLastName(ln.getText());
                n.setText(model.getFullName());
            }
        });
        s.setActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ev) {
                model.save();  
            }
        });
        r.setActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ev) {
                model.load();  
                fn.setText(model.getFirstName());
                ln.setText(model.getLastName());
                n.setText(model.getFullName());                  
            }
        });
        add(fn);
        add(ln);
        add(l);
        add(b);
        add(s);
        add(r);
    }
}

public class Model {
    private firstName;
    private lastName;
    private fullName;

    public String getFirstName() { return firstName; }
    public void setFirstName(String firstName) { this.firstName; update(); }
    public String getLastName() { return firstName; }
    public void setLastName(String firstName) { this.firstName; update(); }
    public String getFullName() { return fullName; }
    public void update() { this.fullName = firstName + " " + lastName }

    public void save() {
        try(PrintWriter w = new PrintWriter(new FileWriter("c:/temp/test.txt"))) {
           w.println(firstName);
           w.println(lastName);
        }
    }

    public void load() {
       try(Scanner s = new Scanner(new FileReader("c:/temp/test.txt"))) {
          firstName = s.next();
          lastName = s.next();
          update();
       }
    }
}

If your Buttons/GridPane is Serializable, you could choose an also serializable list implementation (like ArrayList) and then serialize that into a file whenever the user adds a button or closes the application.如果您的 Buttons/GridPane 是可序列化的,您可以选择一个也可序列化的列表实现(如 ArrayList),然后在用户添加按钮或关闭应用程序时将其序列化到文件中。 Deserialization then allows for easy reconstruction of the saved state.然后反序列化允许轻松重建保存的状态。

Create model objects, like simple JavaBean, to contain the state and data.创建模型对象,如简单的 JavaBean,以包含状态和数据。 These objects should not be JavaFC classes, just simple classes you create.这些对象不应该是 JavaFC 类,而应该是您创建的简单类。 You would serialize these to a file as you see fit.您可以将这些序列化为您认为合适的文件。 I wrote a lot of code for doing this for JavaFX applications and I can attest that it is possible to write to files on a web deployed JavaFX, though there were some hoops to jump.我为 JavaFX 应用程序编写了大量代码来执行此操作,我可以证明可以写入 Web 部署的 JavaFX 上的文件,尽管有一些问题要跳。

You can store the saved objects in memory in custom domain specific Context or Manager singletons (eg AccountManager.getInstance() or AccountnContext.getInstance() ).您可以将保存的对象存储在自定义域特定上下文或管理器单例(例如AccountManager.getInstance()AccountnContext.getInstance() )中的内存中。 These singleton objects would query for the serialized objects upon startup or on request.这些单例对象将在启动或请求时查询序列化对象。 You can have a serialization class that manages all the sterilization of objects to disk.您可以有一个序列化类来管理对象到磁盘的所有灭菌。 Maybe it takes the name of the file and the object type and would either save it or retrieve it.也许它需要文件名和对象类型,然后保存或检索它。 The singletons would hold and provide access to the JavaBean objects.单例将保存并提供对 JavaBean 对象的访问。

EDIT: Just to add to @sh0rug0ru's answer, my answer is centered on the Model aspect of the MVC.编辑:只是为了添加到@sh0rug0ru 的答案中,我的答案集中在 MVC 的模型方面。 The Model is responsible for containing the state, values, and sterilization of the domain specific objects.模型负责包含域特定对象的状态、值和灭菌。

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

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