【问题标题】:JavaFX: Error when adding multiple Hboxes to content in tabJavaFX:将多个 Hbox 添加到选项卡中的内容时出错
【发布时间】:2022-07-08 16:05:57
【问题描述】:

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

出现的错误如下:
线程“JavaFX 应用程序线程”java.lang.IllegalArgumentException 中的异常:子项:检测到循环:父项 = TabPane@6f5ca7e2[styleClass=tab-pane],节点 = TabPaneSkin$TabContentRegion@2d7c1f31[styleClass=tab-content-area]

基本上红线在下面的屏幕截图中我希望标签是“选择文件”,它包含在与文本字段和其下方的按钮不同的 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);
    }
}

样式.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;
    }

}

【问题讨论】:

    标签: java javafx styling hbox


    【解决方案1】:

    所以我设法解决了它。我有一个小错误:我应该像这样将它们封装到Vbox 中:

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

    【讨论】:

      猜你喜欢
      • 2017-01-28
      • 1970-01-01
      • 1970-01-01
      • 2019-02-15
      • 1970-01-01
      • 2017-06-13
      • 2016-10-09
      • 1970-01-01
      相关资源
      最近更新 更多