Java 是否可以在StackPane中添加Hbox?
正如您所见,我是stackOverflow社区的初级开发人员和新手。 所以请不要告诉我我很蠢(或者我至少不知道为什么:) 我正在用javaFx编写一个小应用程序,我整个下午都在处理一个小问题 所以我有这个课程:Java 是否可以在StackPane中添加Hbox?,java,javafx,stackpanel,hbox,Java,Javafx,Stackpanel,Hbox,正如您所见,我是stackOverflow社区的初级开发人员和新手。 所以请不要告诉我我很蠢(或者我至少不知道为什么:) 我正在用javaFx编写一个小应用程序,我整个下午都在处理一个小问题 所以我有这个课程: package glintSG.view.mainPane; import java.awt.Dimension; import javafx.geometry.Pos; import javafx.scene.layout.HBox; import javafx.scene
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());
}
}
(正如你所看到的,为了让你更具可读性,我确实减少了课程,如果你需要更多关于课程的信息,请不要犹豫)
但如果我这样做了,它会起作用。
我不明白的是,当我不想在主图像中倒计时,但在子堆栈窗格中,没有显示任何内容。
如果我在这节课上倒计时:
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放在stackpane中,但我认为是的。
所以我需要你帮我:)
如果有人知道原因,请帮助我:)在您的第一个示例中,由于您使用
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,但为空
你在倒计时课上犯了个错误。
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);
}
}
当您实例化倒计时对象时,可以将其添加到组
或堆栈窗格
中
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
“容器”将在对象倒计时的同时初始化,因为在倒计时构造函数中调用了buildCountDown
方法。您可以通过getter倒计时#getContainer()访问它
嗨!谢谢你的回答,但我不明白,为什么你说我的HBox是空的,而我在我的方法buildCountDown()
?你不是,你正在创建一个新的HBox,而你可以使用你的CountDown类来扩展HBox。不要在你的buildCountDown
方法中创建一个新的,只需使用这个
。我想你是对的,现在它正在打印我想要的,我只需要把它放好就可以了!谢谢!如果您在buildCountDown()
方法中使用的上一个HBox位置正确,您也可以使用第二个解决方案,不再扩展HBox,并添加一个getter来检索您在buildCountDown()方法中构建的HBox。