正如您所见,我是stackOverflow社区中的初级开发人员和新人。 所以,请不要告诉我我是愚蠢的(或者至少为什么:))
我在javaFx中写了一个小应用程序,我面临一个小问题,我整个下午都在工作。
所以我有这个课程的这个程序:
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放入堆栈窗口,但我认为是。 所以我需要你帮助我:) 如果有人知道原因,请帮助我:)。
答案 0 :(得分:2)
在你的第一个例子中它起作用,因为你使用
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课实际上犯了一个错误。
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对象时,可以将其添加到Group
或StackPane
。
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()
访问它。