我不明白为什么我的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);
}}
发布于 2019-08-16 19:15:42
您正在尝试将HBox添加为BorderPane的“非托管”子级。使用BorderPane时,必须指定希望将Node放置在哪个区域中。
因此,问题不在于您的边框没有出现在HBox上,而是您的HBox实际上从未被添加到BorderPane中。
将最后一行更改为:
this.setCenter(hbox);
这将将您的HBox设置在BorderPane的中心。
有关更多信息,请查阅BorderPane文档。
https://stackoverflow.com/questions/57529570
复制相似问题