我正在尝试使用JavaFX创建文本编辑器。我希望能够将窗口中的文件作为选项卡打开到主窗口中。到目前为止,我已经创建了必要的菜单项和一个选项窗口,其中包含打开文件资源管理器以选择文件的功能。
当用户按下“选择文件”按钮时,将打开文件资源管理器。当用户选择文件时,“打开文件”窗口关闭。然后保留主窗口(第3张图像),但不包含带有文件内容的标签。
执行“openFile()”函数后,不会返回任何错误,但不会打开任何选项卡。我相信尝试在“chooseFileButton.SetOnAction()”函数中打开选项卡可能会出现问题,但无法确认。
非常感谢任何建议/解释。
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();
}
答案 0 :(得分:1)
您从未将statusColumn.setCellFactory(column -> new JFXTreeTableCell<MyLog, Image>() {
private final ImageView imageView;
{
imageView = new ImageView();
imageView.setFitWidth(20);
imageView.setFitHeight(20);
setGraphic(imageView);
}
@Override
protected void updateItem(Image item, boolean empty) {
super.updateItem(item, empty);
imageView.setImage(item);
}
});
添加到场景中:
const subarrays = [
[1,0,0],
[0,1,0],
[0,0,1],
[1,0,0],
[0,1,0]
];
const N = 100;
result = Array.from({length: N}).map((n,idx) => subarrays[idx%subarrays.length])