javafx:在选项卡中添加多个Hbox时的错误

发布于 2025-02-13 22:44:05 字数 3274 浏览 0 评论 0原文

我正在尝试创建一个用于文件加密的Javafx应用程序,我对Javafx是相当新的,因此我仍在学习绳索。目前,我的问题是,我需要将Hbox1和Hbox2添加到名为Tabenc的选项卡中的内容中。目前,我遇到了“孩子:周期检测到的”错误,据我了解,这是正在创建循环依赖性。我已经尝试过无数次修复它,但是也许我忽略了一些东西,任何帮助都将不胜感激。

出现的错误如下:
线程“ Javafx应用程序线程” Java.lang.IllegalArgumentException:儿童:循环检测到:parent = tabpane@6f5ca7e2 [styleclass = tab-pane = tab-pane],node = tabpaneskin

$红线是在下面的屏幕截图中,我想在那里标签要成为“选择文件”,该文件包含在其他Hbox中的文本字段和下面的按钮中,因为它们应包含在另一个Hbox中。

如果我的问题缺少任何东西,请告诉我,我将相应地修改。

main.java

import javafx.application.Application;
import javafx.scene.image.Image;
import javafx.stage.Stage;
import java.security.Security;

public class Main extends Application {

    private Style clientStyle = new Style();
    @Override
    public void start(Stage primaryStage) {

        primaryStage.setScene(clientStyle.getScene());
        primaryStage.setTitle("NTH Secure");
        primaryStage.getIcons().add(new Image(("styles/lock.png")));
        primaryStage.setResizable(false);
        primaryStage.show();
    }

    public static void main(String[] args) {
        Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
        launch(args);
    }
}

style.java

import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;


// A class containing the UI elements of the program
public class Style {
    private Scene scene;
    private TabPane tabPane;
    private String dir = System.getProperty("user.dir")+"/testFiles";

    public Style(){

        BorderPane root = new BorderPane();
        scene = new Scene(root, 500, 300);
        scene.getStylesheets().add(getClass().getResource("styles/application.css").toExternalForm());
        tabPane = new TabPane();
        root.setCenter(tabPane);


        //Tab for encryption
        Tab tabEnc = new Tab("Encrypt");
        tabEnc.setClosable(false);
        //PasswordField passwordTxt = new PasswordField();
        Label selectLabel = new Label("Select File");
        HBox hbox1 = new HBox(selectLabel);
        hbox1.setPadding(new Insets(20, 20, 20, 20));
        hbox1.setSpacing(10);

        TextField fileLabel = new TextField("");
        fileLabel.setEditable(false);
        Button buttonFile = new Button("Select");
        Button buttonClear = new Button("Clear");
        buttonClear.setPrefWidth(70);
        buttonFile.setPrefWidth(80);
        fileLabel.setPrefWidth(350);
        HBox hbox2 = new HBox(fileLabel, buttonFile, buttonClear);
        hbox2.setPadding(new Insets(20, 20, 20, 20));
        hbox2.setSpacing(10);
        root.getChildren().addAll(hbox1, hbox2);
        tabEnc.setContent(root);

        //Tab for decryption
        Tab tabDec = new Tab("Decrypt");
        tabDec.setClosable(false);

        //Tab for information
        Tab tabInf = new Tab("About");
        tabInf.setClosable(false);

        tabPane.getTabs().addAll(tabEnc, tabDec, tabInf);

    }

    public Scene getScene(){
        return this.scene;
    }

}

I am trying to create a JavaFX application for file encryption I am fairly new to JavaFX so I am still learning the ropes. My issue at the moment is that I need to add Hbox1 and HBox2 to the content in the tab called tabEnc. At the moment I am getting an error "Children: cycle detected" which from what I understand is that a circular dependency is being created. I have tried numerous times to fix it but maybe I am overlooking something, any help would be greatly appreciated.

The error that comes up is as follows:

Exception in thread "JavaFX Application Thread" java.lang.IllegalArgumentException: Children: cycle detected: parent = TabPane@6f5ca7e2[styleClass=tab-pane], node = TabPaneSkin$TabContentRegion@2d7c1f31[styleClass=tab-content-area]

Essentially where the red line is in the below screenshot I would like the label there to be "Select File" which is contained in a different Hbox to that of the text field and buttons below it as they should be contained in another Hbox.

If my question is missing anything please let me know and I will amend it accordingly.

enter image description here

Main.java

import javafx.application.Application;
import javafx.scene.image.Image;
import javafx.stage.Stage;
import java.security.Security;

public class Main extends Application {

    private Style clientStyle = new Style();
    @Override
    public void start(Stage primaryStage) {

        primaryStage.setScene(clientStyle.getScene());
        primaryStage.setTitle("NTH Secure");
        primaryStage.getIcons().add(new Image(("styles/lock.png")));
        primaryStage.setResizable(false);
        primaryStage.show();
    }

    public static void main(String[] args) {
        Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
        launch(args);
    }
}

Style.java

import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;


// A class containing the UI elements of the program
public class Style {
    private Scene scene;
    private TabPane tabPane;
    private String dir = System.getProperty("user.dir")+"/testFiles";

    public Style(){

        BorderPane root = new BorderPane();
        scene = new Scene(root, 500, 300);
        scene.getStylesheets().add(getClass().getResource("styles/application.css").toExternalForm());
        tabPane = new TabPane();
        root.setCenter(tabPane);


        //Tab for encryption
        Tab tabEnc = new Tab("Encrypt");
        tabEnc.setClosable(false);
        //PasswordField passwordTxt = new PasswordField();
        Label selectLabel = new Label("Select File");
        HBox hbox1 = new HBox(selectLabel);
        hbox1.setPadding(new Insets(20, 20, 20, 20));
        hbox1.setSpacing(10);

        TextField fileLabel = new TextField("");
        fileLabel.setEditable(false);
        Button buttonFile = new Button("Select");
        Button buttonClear = new Button("Clear");
        buttonClear.setPrefWidth(70);
        buttonFile.setPrefWidth(80);
        fileLabel.setPrefWidth(350);
        HBox hbox2 = new HBox(fileLabel, buttonFile, buttonClear);
        hbox2.setPadding(new Insets(20, 20, 20, 20));
        hbox2.setSpacing(10);
        root.getChildren().addAll(hbox1, hbox2);
        tabEnc.setContent(root);

        //Tab for decryption
        Tab tabDec = new Tab("Decrypt");
        tabDec.setClosable(false);

        //Tab for information
        Tab tabInf = new Tab("About");
        tabInf.setClosable(false);

        tabPane.getTabs().addAll(tabEnc, tabDec, tabInf);

    }

    public Scene getScene(){
        return this.scene;
    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

Oo萌小芽oO 2025-02-20 22:44:07

所以我设法解决了它。我的错误存在很小的错误:我应该将它们封装到vbox之类的范围内:

VBox vbox = new VBox(); 
vbox.getChildren().addAll(hbox1, hbox2); 
tabEnc.setContent(vbox); 

So I managed to solve it. There was a small error on my part: I should have encapsulated them into a Vbox like so:

VBox vbox = new VBox(); 
vbox.getChildren().addAll(hbox1, hbox2); 
tabEnc.setContent(vbox); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文