如何用流调用列表的内容



我正在使用javafx作为gui。当用户选择一个文件并点击进程时,它会读入并处理一个jar文件,然后使用EmbeddedStorageManager以字符串格式打印并保存一些度量。当我按下一个按钮时,我正试图调用另一个类中列表的内容,但它不断返回,说列表是空的。

当我在填充后直接运行该方法(不按按钮(时,它似乎会打印出内容,但当我在按下显示所有按钮后调用它时,它会打印出空列表。

我已经在谷歌上搜索了几个小时,但似乎找不到任何有用的东西。

请参阅下面的代码,感谢您提前提供的帮助。

数据库类该类是从中调用列表的地方,它包含显示列表内容的方法。

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);

}
}

问题似乎是AppWindow类中缺少对Database :: addElement的调用。

向数据库添加内容的方法只在ProcessJar类内部调用两次,并且它使用自己的Database实例。

您应该在AppWindow中添加另一个按钮,并将其操作设置为db.addElement("something");

最新更新