是否可以在Stackpane中添加Hbox



,您可以看到我是Junior Dev,而在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());
}
}

(如您所见,我真的减少了班级以使您更可读,如果您需要更多的课程,请不要犹豫(

,但是如果我这样做,那就起作用了。我不明白的是,当我不想将倒计时放在主要图像中时,而是在sub stackpane中,什么也没有显示。如果我将倒计时放入此类:

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但空为空的倒计时对象。

您实际上在倒数课上犯了一个错误。

  • 您应该将倒计时类用作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);      
        }
    }
    

    ,当您实例化倒数对象时,可以在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"容器"将与您的对象倒计时同时初始化,因为buildCountDown方法在Countdown构造器中调用。您可以通过getter CountDown#getContainer()访问它。

  • 最新更新