有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

JavaFXHBOX边框

我不明白为什么我的HBox周围没有边界?现在并没有什么事情会因为这件事而引发非法辩论。我猜是设置中心(hbox)的部分。(忽略这一点,我只是在写,因为StackOverflow不允许我上传这么多代码)

package view;

import javafx.scene.paint.Color;
import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Border;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.BorderStroke;
import javafx.scene.layout.BorderStrokeStyle;
import javafx.scene.layout.BorderWidths;
import javafx.scene.layout.CornerRadii;
import javafx.scene.layout.HBox;
import javafx.scene.text.Font;

public class MyPane extends BorderPane{

private int score=0;

public MyPane() {
    this.score=0;
    init();
    // TODO Auto-generated constructor stub
}

public MyPane(int score) {
    this.score=score;
    init();
}

public void init() {

    Image img=new Image("Ball.png");
    ImageView imv= new ImageView(img);
    imv.setFitHeight(100);
    imv.setFitWidth(100);
    Label label= new Label(Integer.toString(score));
    label.setPrefSize(100, 100);
    label.setFont(new Font(50));
    label.setPadding(new Insets(18));



    HBox hbox= new HBox();
    hbox.setBorder(new Border(new BorderStroke(Color.GREEN, BorderStrokeStyle.SOLID, null , null)));
    hbox.getChildren().add(imv);
    hbox.getChildren().add(label);
    hbox.setSpacing(50);
    hbox.setPadding(new Insets(20));






    this.getChildren().add(hbox);
    this.setCenter(hbox);
}

}


共 (1) 个答案

  1. # 1 楼答案

    您正试图将HBox添加为BorderPane的“非托管”子级。使用BorderPane,必须指定要将Node放置在哪个区域

    因此,问题不是你的边界没有出现在HBox上,而是你的HBox从来没有被添加到BorderPane

    将最后一行更改为:

    this.setCenter(hbox);

    这将把HBox设置在BorderPane的中心

    请查看^{}的文档以了解更多信息