Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
打开文件时,JavaFX-Tab将不显示文件内容_Java_File_Javafx_Tabs - Fatal编程技术网

打开文件时,JavaFX-Tab将不显示文件内容

打开文件时,JavaFX-Tab将不显示文件内容,java,file,javafx,tabs,Java,File,Javafx,Tabs,我正在尝试使用JavaFX创建一个文本编辑器。我想能够打开一个文件从一个窗口到主窗口作为一个标签。到目前为止,我已经创建了必要的菜单项和选项窗口,其中包含打开文件资源管理器以选择文件的功能 当用户按下“选择文件”按钮时,文件浏览器打开。当用户选择文件时,“打开文件”窗口关闭。然后,主窗口(第三幅图像)将保留,但不包含包含文件内容的选项卡 执行“openFile()”函数时,不会返回任何错误,但不会打开任何选项卡。我认为,尝试打开“chooseFileButton.SetOnAction()”函数

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

当用户按下“选择文件”按钮时,文件浏览器打开。当用户选择文件时,“打开文件”窗口关闭。然后,主窗口(第三幅图像)将保留,但不包含包含文件内容的选项卡

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

如有任何建议/解释,将不胜感激


打开文件

打开文件(文件选择器)

输出:


public类主扩展应用程序{
公共静态void main(字符串[]args){
发射(args);
}
@凌驾
public void start(Stage primary)引发异常{
primary.setTitle(“克里斯的文本编辑器”);
菜单栏菜单栏=新建菜单栏();
VBox VBox=新的VBox(菜单栏);
/*文件菜单*/
MenuItem openFile=新的MenuItem(“打开…”);
fileMenu.getItems().add(openFile);
窗格根窗格=新窗格();
TextArea editorTextArea=新建TextArea();
EditorExtArea.setMinHeight(1000);
EditorExtArea.setMinWidth(1000);
editorTextArea.setVisible(false);
rootPane.getChildren().add(editorTextArea);
TabPane TabPane=新建TabPane();
tabPane.setSide(侧面、顶部);
setOnAction(新的EventHandler(){
@凌驾
公共无效句柄(ActionEvent事件){
Label fileLabel=新标签();
fileLabel.setText(“未选择文件…”);
GridPane grid=新建GridPane();
场景contextScene=新场景(网格,450300);
/*新窗口*/
Stage openFileWindow=newstage();
setTitle(“打开文件”);
openFileWindow.setScene(contextScene);
/*设置窗口模式*/
openFileWindow.initModal(MODAL.WINDOW\u MODAL);
/*设置父窗口*/
openFileWindow.initOwner(主);
/*选择文件目录按钮*/
openFileWindow.setX(primary.getX()+(primary.getX()/2));
setY(primary.getX()+(primary.getX()/2));
openFileWindow.show();
/*选择文件按钮*/
Button chooseFileButton=新建按钮();
chooseFileButton.setText(“选择文件”);
chooseFileButton.setOnAction(新的EventHandler(){
公共无效句柄(ActionEvent事件){
FileChooser chooseFile=新建FileChooser();
File selectedFile=chooseFile.showOpenDialog(openFileWindow);
如果(selectedFile!=null){
String filePath=selectedFile.getPath();
fileLabel.setText(filePath);
字符串fileContent=openFile2(filePath);
/*创建新选项卡*/
Tab newTab=新选项卡();
newTab.setContent(editorTextArea);
newTab.setText(文件路径);
tabPane.getTabs().add(newTab);
editorTextArea.setVisible(true);
/*用文件内容填充文本区域*/
editorTextArea.appendText(文件内容);
/*关注标签*/
SingleSelectionModel selection=tabPane.getSelectionModel();
选择。选择(newTab);
openFileWindow.close();
}
}
});
网格设置对齐(位置中心);
网格。setHgap(10);
网格设置间隙(10);
添加(选择文件按钮,0,0);
添加(fileLabel,0,1);
}
});
menuBar.getMenus().add(fileMenu);
场景=新场景(vbox,1000750);
主场景(场景);
primary.show();
}

公共字符串openFile2(字符串文件路径){
StringBuilder内容=新建StringBuilder();
try(Stream=Files.line(path.get(filePath),StandardCharsets.UTF_8)){
stream.forEach->content.append.append(“\n”);
}捕获(IOE异常){
e、 printStackTrace();
}
返回content.toString();
}

您从未将
选项卡窗格添加到场景中:

vbox.getChildren().add(tabPane);

我觉得很傻。我在VBox中添加了“rootPane”,然后在“rootPane”中添加了“tabPane”。现在程序按我的预期运行。非常感谢!
 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();

 }
vbox.getChildren().add(tabPane);