gpt4 book ai didi

JavaFX - 打开文件时选项卡不会显示也不显示文件内容

转载 作者:行者123 更新时间:2023-11-30 06:08:52 28 4
gpt4 key购买 nike

我正在尝试使用 JavaFX 创建一个文本编辑器。我希望能够从窗口将文件作为选项卡打开到主窗口中。到目前为止,我已经创建了必要的菜单项和一个选项窗口,其中包含打开文件资源管理器以选择文件的功能。

当用户按下“选择文件”按钮时,文件资源管理器将打开。当用户选择文件时,“打开文件”窗口将关闭。然后离开主窗口(第三张图像),但不包含包含文件内容的选项卡。

执行“openFile()”函数时,不会返回任何错误,但不会打开任何选项卡。我相信尝试在“chooseFileButton.SetOnAction()”函数中打开选项卡可能存在问题,但无法确认。

任何建议/解释将不胜感激。

<小时/>

打开文件

enter image description here

打开文件(FileChooser)

enter image description here

输出:

enter image description here

<小时/>
public class Main extends Application {

public static void main(String[] args) {
launch(args);
}

@Override
public void start(Stage primary) throws Exception {
primary.setTitle("Chris' Text Editor");

MenuBar menuBar = new MenuBar();
VBox vbox = new VBox(menuBar);

/* FILE MENU */
MenuItem openFile = new MenuItem("Open...");

fileMenu.getItems().add(openFile);

Pane rootPane = new Pane();

TextArea editorTextArea = new TextArea();
editorTextArea.setMinHeight(1000);
editorTextArea.setMinWidth(1000);
editorTextArea.setVisible(false);
rootPane.getChildren().add(editorTextArea);

TabPane tabPane = new TabPane();
tabPane.setSide(Side.TOP);

openFile.setOnAction(new EventHandler<ActionEvent>() {

@Override
public void handle(ActionEvent event) {

Label fileLabel = new Label();
fileLabel.setText("No File selected...");

GridPane grid = new GridPane();
Scene contextScene = new Scene(grid, 450, 300);

/* NEW WINDOW */
Stage openFileWindow = new Stage();
openFileWindow.setTitle("Open File");
openFileWindow.setScene(contextScene);

/* SET WINDOW MODAL */
openFileWindow.initModality(Modality.WINDOW_MODAL);

/* SET PARENT WINDOW */
openFileWindow.initOwner(primary);

/* CHOOSE FILE DIRECTORY BUTTON */
openFileWindow.setX(primary.getX() + (primary.getX() / 2));
openFileWindow.setY(primary.getX() + (primary.getX() / 2));

openFileWindow.show();

/* CHOOSE FILE BUTTON */
Button chooseFileButton = new Button();
chooseFileButton.setText("Choose File");

chooseFileButton.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
FileChooser chooseFile = new FileChooser();
File selectedFile = chooseFile.showOpenDialog(openFileWindow);

if(selectedFile != null) {
String filePath = selectedFile.getPath();
fileLabel.setText(filePath);
String fileContent = openFile2(filePath);

/* CREATE NEW TAB */
Tab newTab = new Tab();
newTab.setContent(editorTextArea);
newTab.setText(filePath);
tabPane.getTabs().add(newTab);

editorTextArea.setVisible(true);

/* POPULATE TEXT AREA WITH FILE CONTENTS */
editorTextArea.appendText(fileContent);

/* FOCUS ON TAB */
SingleSelectionModel<Tab> selection = tabPane.getSelectionModel();
selection.select(newTab);

openFileWindow.close();
}
}
});

grid.setAlignment(Pos.CENTER);
grid.setHgap(10);
grid.setVgap(10);

grid.add(chooseFileButton, 0, 0);
grid.add(fileLabel, 0, 1);

}


});

menuBar.getMenus().add(fileMenu);

Scene scene = new Scene(vbox, 1000, 750);
primary.setScene(scene);
primary.show();
}

 public String openFile2(String filePath) {
StringBuilder content = new StringBuilder();

try (Stream<String> stream = Files.lines(Paths.get(filePath), StandardCharsets.UTF_8)){
stream.forEach(s -> content.append(s).append("\n"));
} catch (IOException e) {
e.printStackTrace();
}

return content.toString();

}

最佳答案

您从未将 TabPane 添加到场景中:

vbox.getChildren().add(tabPane);

关于JavaFX - 打开文件时选项卡不会显示也不显示文件内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50670670/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com