提问者:小点点

JavaFX:将多个Hbox添加到选项卡中的内容时出错


我正在尝试创建一个用于文件加密的JavaFX应用程序。我对JavaFX还很陌生,所以我仍在学习技巧。目前我的问题是,我需要将Hbox1和HBox2添加到名为tabEnc的选项卡中的内容中。目前,我收到一个错误“Children:cycle detected”,据我所知,这是一个循环依赖项正在创建。我已经试过很多次了,但也许我忽略了什么,任何帮助都将不胜感激。

出现的错误如下:< br >线程“JavaFX Application Thread”中的异常Java . lang . illegalargumentexception:Children:cycle detected:parent = tab pane @ 6 F5 ca 7 e 2[style class = ta B- pane],node = TabPaneSkin$TabContentRegion @ 2d 7 c1f 31[style class = ta B- content-area]

基本上,在下面屏幕截图中的红线所在的地方,我希望标签是“选择文件”,它包含在与它下面的文本字段和按钮不同的Hbox中,因为它们应该包含在另一个Hbox中。

如果我的问题遗漏了什么,请告诉我,我会相应地修改。

主.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;
    }

}

共1个答案

匿名用户

所以我设法解决了这个问题。我有一个小错误:我应该把它们封装到一个Vbox中,如下所示:

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