我使用 java fx 作为 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);
}
}