如何用流调用列表的内容?

qvtsj1bj  于 2021-06-26  发布在  Java
关注(0)|答案(1)|浏览(406)

我使用javafx作为gui。当用户选择一个文件并点击process时,它读入并处理一个jar文件,然后使用embeddedstoragemanager打印并以字符串格式保存一些度量。当我按下一个按钮时,我试图在一个单独的类中调用我列表的内容,但它不断返回说列表是空的。
当我在填充后直接运行这个方法时(不按按钮),它似乎打印出内容,但是当我在按ShowAll按钮后调用它时,它打印的列表是空的。
我在谷歌上搜索了几个小时,但似乎找不到任何有用的东西。
请看下面我的代码,并提前感谢任何帮助。
这个类是调用列表的地方,它包含显示列表内容的方法。
appwindow这是我用来调用数据库类中show方法的按钮。当按下这个按钮时,表示数据库是空的

package ie.gmit.sw;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.control.TitledPane;
import javafx.scene.control.ToolBar;
import javafx.scene.layout.VBox;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

public class AppWindow extends Application {
    private TextField txtFile; // A control, part of the View and a leaf node.
    //ProcessJar process = new ProcessJar();

    Database db = new Database();
    //QueryDB qdb = new QueryDB();
    ProcessJar pj = new ProcessJar();

    @Override
    public void start(Stage stage) throws Exception {
        stage.setTitle("");
        stage.setWidth(800);
        stage.setHeight(400);

        stage.setOnCloseRequest((e) -> System.exit(0));

        VBox box = new VBox();
        box.setPadding(new Insets(10));
        box.setSpacing(8);

        //**Strategy Pattern**. Configure the Context with a Concrete Strategy
        Scene scene = new Scene(box);
        stage.setScene(scene);

        ToolBar toolBar = new ToolBar(); // A ToolBar is a composite node for Buttons (leaf nodes)

        Button btnQuit = new Button("Quit"); // A Leaf node
        btnQuit.setOnAction(e -> System.exit(0)); // Plant an observer on the button
        toolBar.getItems().add(btnQuit); // Add to the parent node and build the tree

        Button btnAdd = new Button("Show all"); // A Leaf node
        btnAdd.setOnAction(e -> {
            db.show(); <<<<<<-----When this button is pressed its supposed to show the contents of the database, but its coming back empty
        });
        toolBar.getItems().add(btnAdd); // Add to the parent node and build the tree

        Button btnDelete = new Button("Delete"); // A Leaf node
        btnDelete.setOnAction(e -> {
            db.emptyDb();

        });
        toolBar.getItems().add(btnDelete); // Add to the parent node and build the tree

        /*
         * Add all the sub trees of nodes to the parent node and build the tree
         */
        box.getChildren().add(getFileChooserPane(stage)); // Add the sub tree to the main tree
        box.getChildren().add(toolBar); // Add the sub tree to the main tree

        // Display the window
        stage.show();
        stage.centerOnScreen();
    }

    /*
     * This method builds a TitledPane containing the controls for the file chooser
     * part of the application. We could have created a specialised instance of the
     * class TitledPane using inheritance and moved all of the method into its own
     * class (OCP).
     */
    private TitledPane getFileChooserPane(Stage stage) {
        VBox panel = new VBox(); //**A concrete strategy***

        txtFile = new TextField(); // A leaf node

        FileChooser fc = new FileChooser(); // A leaf node
        fc.getExtensionFilters().addAll(new FileChooser.ExtensionFilter("JAR Files", "*.jar"));

        Button btnOpen = new Button("Select File"); // A leaf node
        btnOpen.setOnAction(e -> { // Plant an observer on the button
            File f = fc.showOpenDialog(stage);
            // convert f to string
            txtFile.setText(f.getAbsolutePath());
        });

        Button btnProcess = new Button("Process"); // A leaf node
        btnProcess.setOnAction(e -> { // Plant an observer on the button
            File f = new File(txtFile.getText());
            System.out.println("[INFO] Processing file " + f.getName());

            try {
                pj.process(f.toString());
            } catch (FileNotFoundException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (ClassNotFoundException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

        });

        ToolBar tb = new ToolBar(); // A composite node
        tb.getItems().add(btnOpen); // Add to the parent node and build a sub tree
        tb.getItems().add(btnProcess); // Add to the parent node and build a sub tree

        panel.getChildren().add(txtFile); // Add to the parent node and build a sub tree
        panel.getChildren().add(tb); // Add to the parent node and build a sub tree

        TitledPane tp = new TitledPane("Select File to Process", panel); // Add to the parent node and build a sub tree
        tp.setCollapsible(false);
        return tp;
    }

}

跑步者

import java.io.IOException;
import javafx.application.Application;

public class Runner {

    public static void main(String[] args) throws IOException {
        System.out.println("[INFO] Launching GUI...");
        Application.launch(AppWindow.class, args);

    }
}
zlwx9yxi

zlwx9yxi1#

问题似乎是找不到 Database :: addElementAppWindow 班级。
向数据库中添加内容的方法仅在内部调用 ProcessJar 类,并且它正在使用自己的 Database .
您应该在中添加另一个按钮 AppWindow 把它的行动 db.addElement("something");

相关问题