我不明白为什么我的 HBox 周围没有边框?现在什么也没有发生,除了 eclipse 抛出 IllegalArgumentException 之外,因为我猜是 this.setCenter(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);
}
}
最佳答案
您正在尝试将 HBox
添加为 BorderPane
的“非托管”子项。对于 BorderPane
,您必须指定要将 Node
放置在哪个区域。
因此,问题不在于您的边框没有显示在 HBox
上,而是您的 HBox
从未真正添加到您的 边框 Pane
。
将最后一行更改为:
this.setCenter(hbox);
这会将您的 HBox
设置在 BorderPane
的中心。
请查看 BorderPane
的文档了解更多信息。
关于JavaFX HBox 边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57529570/