gpt4 book ai didi

java - 如何在 JavaFX 中的 TabPane 中添加/删除 Tab?

转载 作者:行者123 更新时间:2023-12-01 18:13:37 25 4
gpt4 key购买 nike

如何在 JavaFX 中的 TabPane 中添加/删除 Tab?

我正在尝试在 JavaFX 中的 Pane 内使用 TabPane 在 TabPane 中添加和删除选项卡。我正在使用基于 FXML 的布局,如下所示:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.VBox?>


<Pane id="pane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller.Controller">
<children>
<TabPane id="tabPane" prefHeight="328.0" prefWidth="600.0" tabClosingPolicy="ALL_TABS">
<tabs>
<Tab text="Tab1">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="320.0" prefWidth="600.0" />
</content>
</Tab>
<Tab text="Tab2">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
</content>
</Tab>
</tabs>
</TabPane>
<Button id="tabButton" layoutX="274.0" layoutY="361.0" mnemonicParsing="false" onAction="#handleTabButton" text="Button" />
</children>
</Pane>

我有 MainApp 来启动我的应用程序和 Controller 来处理事件。我的主应用程序如下:

package src;

import controller.Controller;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class MainApp extends Application{

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

@Override
public void start(Stage primaryStage) throws Exception {
FXMLLoader loader = new FXMLLoader(getClass().getResource("LMainApp.fxml"));
Parent root = loader.load();

Controller controller = loader.getController();
controller.setStage(primaryStage);

primaryStage.setTitle("TestTabs");

primaryStage.setScene(new Scene(root));
primaryStage.show();
}

}

我的 Controller 如下:

package controller;

import java.net.URL;
import java.util.ResourceBundle;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.SingleSelectionModel;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.stage.Stage;

public class Controller implements Initializable{

Stage stage;

@FXML
private TabPane tabPane;

private Tab tab = new Tab();
private SingleSelectionModel<Tab> selectionModel;

@Override
public void initialize(URL location, ResourceBundle resources) {
selectionModel = tabPane.getSelectionModel();
}

public void setStage(Stage stage){
this.stage = stage;
}

@FXML
private void handleTabButton(ActionEvent event){
tab.setText("New Tab");
tab.setId("newTab");
tab.setClosable(true);
tabPane.getTabs().add(tab);
selectionModel.selectLast();
}

}

现在,当我启动应用程序时,我在 SelectionModel = tabPane.getSelectionModel(); 处收到 NullPointerException;在我的 Controller 中。看来 tabPane 为空。

堆栈跟踪:

Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(Unknown Source)
at com.sun.javafx.application.LauncherImpl.launchApplication(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(Unknown Source)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$152(Unknown Source)
at com.sun.javafx.application.LauncherImpl$$Lambda$50/1645995473.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: javafx.fxml.LoadException:
/E:/EclipseWorkspace/TestTabJFX/bin/src/LMainApp.fxml

at javafx.fxml.FXMLLoader.constructLoadException(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.load(Unknown Source)
at src.MainApp.start(MainApp.java:19)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$159(Unknown Source)
at com.sun.javafx.application.LauncherImpl$$Lambda$53/434272560.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$172(Unknown Source)
at com.sun.javafx.application.PlatformImpl$$Lambda$46/186276003.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$170(Unknown Source)
at com.sun.javafx.application.PlatformImpl$$Lambda$48/463228645.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$171(Unknown Source)
at com.sun.javafx.application.PlatformImpl$$Lambda$47/237061348.run(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$145(Unknown Source)
at com.sun.glass.ui.win.WinApplication$$Lambda$36/2117255219.run(Unknown Source)
... 1 more
Caused by: java.lang.NullPointerException
at controller.Controller.initialize(Controller.java:26)
... 18 more
Exception running application src.MainApp

有人可以建议如何解决这个问题吗?此外,如果有人可以帮助我 - 如何访问/使用 FXML 文件中存在的元素。提前致谢。

最佳答案

您在 fxml 中使用 id 而不是 fx:id。将 TabPane 的声明更改为

<TabPane fx:id="tabPane" prefHeight="328.0" prefWidth="600.0" tabClosingPolicy="ALL_TABS">

了解更多信息:

关于java - 如何在 JavaFX 中的 TabPane 中添加/删除 Tab?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31066543/

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