【问题标题】:JavaFX HBox BorderJavaFX HBox 边框
【发布时间】:2019-12-23 01:40:15
【问题描述】:

我不明白为什么我的 HBox 周围没有边框?现在什么都没有发生,除了我猜的 this.setCenter(hbox) 部分,eclipse 抛出了 IllegalArgumentException 。 (忽略这个,我只是写,因为 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);
}

}

【问题讨论】:

  • minimal reproducible example 在这里会有所帮助。边框对我来说似乎很好。
  • 话虽如此,使用 CSS 添加边框可能要容易得多:hbox.setStyle("-fx-border-color: green");
  • ` public class MyPane extends BorderPane{ public void init() { Label label= new Label(Integer.toString(score));标签.setPrefSize(100, 100); label.setFont(新字体(50)); label.setPadding(新插图(18)); HBox hbox= 新 HBox(); hbox.setBorder(new Border(new BorderStroke(Color.GREEN, BorderStrokeStyle.SOLID, null, null))); hbox.getChildren().add(标签); hbox.setSpacing(50); hbox.setPadding(新插图(20)); this.getChildren().add(hbox); } }`

标签: java eclipse javafx


【解决方案1】:

您正尝试将HBox 添加为BorderPane 的“非托管”子代。对于 BorderPane,您必须指定要将 Node 放置在哪个区域

因此,问题不在于您的边框没有显示在HBox 上,而是您的HBox 从未真正添加到您的BorderPane

将最后一行更改为:

this.setCenter(hbox);

这会将您的HBox 设置在BorderPane 的中心。

请查看BorderPane 的文档以了解更多信息。

【讨论】:

  • 我把最后一行改成了 this.setCenter(hbox);但它仍然不起作用:C 它现在抛出非法参数的异常
  • 那么请编辑您的问题并提供我在第一条评论中提到的minimal reproducible example。你的问题不止这一点。
  • 我该怎么做? ^^ sry 我是新人
  • 只是阅读我发布的链接?然后单击您的问题下的“编辑”链接。基本想法是让您提供足够的代码,以便我可以在我的系统上复制和测试,以便重现问题。
  • 现在够了吗?
猜你喜欢
  • 2015-06-24
  • 1970-01-01
  • 2015-01-03
  • 2012-08-25
  • 2016-09-08
  • 2014-03-15
  • 1970-01-01
  • 1970-01-01
  • 2017-08-16
相关资源
最近更新 更多