gpt4 book ai didi

java - 是否可以在 StackPane 中添加 Hbox?

转载 作者:行者123 更新时间:2023-11-30 02:11:21 24 4
gpt4 key购买 nike

如您所见,我是一名初级开发人员,也是 stackOverflow 社区的新人。所以请不要告诉我我很蠢(或者我至少不知道为什么:))

我正在用 javaFx 编写一个小应用程序,但我遇到了一个小问题,我花了整个下午的时间来解决这个问题。

所以我有这个类(class)的程序:

package glintSG.view.mainPane;

import java.awt.Dimension;

import javafx.geometry.Pos;
import javafx.scene.layout.HBox;
import javafx.scene.text.Text;

public class CountDown extends HBox {

private Text countDownLabel;

public CountDown() {
super();
}

public HBox buildCountDown() {
HBox hb = new HBox();
hb.setId("countDownBox");
countDownLabel = new Text();
countDownLabel.setId("countdownlabel-SG");
//position of the count down adapted to the screen size
relocateCountDown();
countDownLabel.getStyleClass().add("countdownlabel-SG");
hb.getChildren().add(countDownLabel);
hb.setAlignment(Pos.BASELINE_LEFT);
return hb;
}

public Text getCountDownLabel() {
return countDownLabel;
}

public void relocateCountDown() {
countDownLabel.setTranslateX(Main.getWindowWidth()/2);
}
}

现在,如果我在我看来它正在工作:

package glintSG.view;

public class SGView {

private EnterHeadingPane enterHeadingPane;
private CountDown countDown;

@Override
public void preInit() {
countDown = new CountDown();
enterHeadingPane = new EnterHeadingPane();
}

@Override
public void init() {
getRootNode().getChildren().add(countDown.buildCountDown());
getRootNode().getChildren().add(enterHeadingPane.buildEnterCap());
}
}

(如您所见,我确实减少了类(class),以便您更具可读性,如果您需要更多有关类(class)的信息,请不要犹豫)

但是如果我这样做它就会起作用。我不明白的是,当我不想将倒计时放在主图像中但在子堆栈 Pane 中时,什么也没有显示。如果我把倒计时放在这个类中:

package glintSG.view.mainPane;

public class EnterHeadingPane extends StackPane {

private Group group;

private Button tryButtonCAP;
private TextField inputTextCAP;
private Text textLabel;
private CountDown countDown;
private DifficultyLvlBox diffLvl;

private double textFieldPaddingTop = 7.0;
private double textFieldPaddingBot = 09.0;
private double textFieldPaddingLeft = 10.0;
private double textFieldPaddingRight = 10.0;

private double ButtonPaddingTop = 10.0;
private double ButtonPaddingBot = 10.0;
private double ButtonPaddingLeft = 10.0;
private double ButtonPaddingRight = 10.0;

private CustomTextField capTextField;
private boolean isWarningHere = false;

private Dimension dimension = java.awt.Toolkit.getDefaultToolkit().getScreenSize();


public EnterHeadingPane() {
super();
this.getStyleClass().add("glint-title-window");
}

public StackPane buildEnterCap() {
final StackPane enterCap = new StackPane();
enterCap.setId("enterCapLayer");
textLabel = buildTextLabel();
inputTextCAP = buildTextFieldCap();
tryButtonCAP = buildCapButton();
countDown =buildCountDown();
group = new Group();
group.getChildren().add(tryButtonCAP);
group.getChildren().add(inputTextCAP);
group.getChildren().add(textLabel);
group.getChildren().add(countDown);
group.getChildren().add(diffLvl);
group.setTranslateX(getScreenWidth() -
textLabel.getLayoutBounds().getWidth() - 50);
group.setTranslateY(900);
enterCap.getChildren().add(group);
return enterCap;
}
//well i write the methods buildTextLabel etc.

private CountDown buildCountDown() {
countDown = new CountDown();
countDown.setId("countDown-SG");
countDown.buildCountDown();
countDown.setAlignment(Pos.CENTER);
return countDown;
}
}

如果我这样做了,我的倒计时就不会被打印出来。为什么?

所以我认为主要问题是:是否可以将 hbox 放入堆栈 Pane 中,但我认为可以。所以我需要你帮助我:)如果有人知道为什么,请帮助我:)

最佳答案

在您的第一个示例中,它可以工作,因为您使用了

getRootNode().getChildren().add(countDown.buildCountDown());

当您调用 countDown.buildCountDown() 时,您将创建一个新的 HBox 并返回它。

在第二个示例中

private CountDown buildCountDown() {
countDown = new CountDown();
countDown.setId("countDown-SG");
countDown.buildCountDown();
countDown.setAlignment(Pos.CENTER);
return countDown;
}

它不起作用,因为您返回了扩展 HBox 但为空的 CountDown 对象。

您实际上在 CountDown 类中犯了一个错误。

  • 您应该使用倒计时类作为 HBox,如下所示:

    public class CountDown extends HBox {

    private Text countDownLabel;

    public CountDown() {
    super();
    // Initializes your CountDown.
    buildCountDown();
    }

    public void buildCountDown() {
    // Not needed to instantiate a new HBox, your countdown object is extending this kind of Node
    this.setId("countDownBox");
    countDownLabel = new Text();
    countDownLabel.setId("countdownlabel-SG");

    //position of the count down adapted to the screen size
    relocateCountDown();

    countDownLabel.getStyleClass().add("countdownlabel-SG");
    // Add the label to your CountDown object.
    this.getChildren().add(countDownLabel);
    this.setAlignment(Pos.BASELINE_LEFT);
    }


    public Text getCountDownLabel() {
    return countDownLabel;
    }


    public void relocateCountDown() {
    countDownLabel.setTranslateX(Main.getWindowWidth()/2);
    }
    }

    当您实例化 CountDown 对象时,您可以将其添加到您的 GroupStackPane 中。

  • 或者您可以使用对象初始化 HBox 属性并通过 getter 检索它:

    public class CountDown {

    private Text countDownLabel;

    private HBox container;

    public CountDown() {
    super();
    // Initializes your CountDown.
    buildCountDown();
    }

    // Your method
    public void buildCountDown() {
    container = new HBox();
    container.setId("countDownBox");
    countDownLabel = new Text();
    countDownLabel.setId("countdownlabel-SG");

    //position of the count down adapted to the screen size
    relocateCountDown();

    countDownLabel.getStyleClass().add("countdownlabel-SG");
    container.getChildren().add(countDownLabel);
    container.setAlignment(Pos.BASELINE_LEFT);
    }

    // You can access to your HBox here
    public HBox getContainer() {
    return container;
    }

    public Text getCountDownLabel() {
    return countDownLabel;
    }

    public void relocateCountDown() {
    countDownLabel.setTranslateX(Main.getWindowWidth()/2);
    }
    }

    Hbox“容器”将在对象 CountDown 的同时初始化,因为在 CountDown 构造函数中调用 buildCountDown 方法。您可以通过 getter CountDown#getContainer() 访问它。

  • 关于java - 是否可以在 StackPane 中添加 Hbox?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49985314/

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