JavaFx 8-一个比您有边界的类';重新着色,并能在中间有文字 有没有人知道我可以使用一个非常重要的矩形,但是它在矩形的中间有文本,矩形有一个填充颜色和一个边框颜色(边框可以被改变成红色或者沿着这些线的东西)

JavaFx 8-一个比您有边界的类';重新着色,并能在中间有文字 有没有人知道我可以使用一个非常重要的矩形,但是它在矩形的中间有文本,矩形有一个填充颜色和一个边框颜色(边框可以被改变成红色或者沿着这些线的东西),java,netbeans,javafx-8,Java,Netbeans,Javafx 8,基本上现在我有一个窗格,我想制作一个2D网格(10x10),其中网格中的每个单独对象都是一个矩形类型的对象,具有数字文本中心对齐、填充颜色和边框颜色 注意:我曾尝试使用gridpane,但由于缺少相关文档,我认为我只能设置填充颜色,网格窗格中的每个单元格看起来不像我希望的那样是一个单独的对象。我也尝试过实现矩形,但矩形没有文本或边框,我无法操作 谢谢你的帮助 我认为最好的办法是创建一个自定义控件,公开所需的属性。 有关信息和示例,请参见。只需使用标签即可。以下是概念证明: import java

基本上现在我有一个窗格,我想制作一个2D网格(10x10),其中网格中的每个单独对象都是一个矩形类型的对象,具有数字文本中心对齐、填充颜色和边框颜色

注意:我曾尝试使用gridpane,但由于缺少相关文档,我认为我只能设置填充颜色,网格窗格中的每个单元格看起来不像我希望的那样是一个单独的对象。我也尝试过实现矩形,但矩形没有文本或边框,我无法操作


谢谢你的帮助

我认为最好的办法是创建一个自定义控件,公开所需的属性。
有关信息和示例,请参见。

只需使用
标签即可。以下是概念证明:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class CustomLabelDemo extends Application {

    @Override
    public void start(Stage primaryStage) {
        Label label = new Label("Hello World");
        label.setStyle(
            "-fx-alignment: center;"
            +"-fx-padding: 6px;"
            +"-fx-background-color: red, -fx-background;"
            +"-fx-background-insets: 0, 4px;"
        );
        StackPane root = new StackPane(label);
        primaryStage.setScene(new Scene(root, 350, 75));
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
css在这里的工作方式是它定义了两个背景:第一个(因此后面的一个)是红色的;前面的设置为
-fx background
,这是大多数控件的默认样式表中为背景定义的颜色。与之对应的是两个背景的两个插图:第一个设置为零,第二个设置为4像素。这意味着红色边框的4个像素将可见。设置填充只是为了确保文本不会与外部背景(边框)重叠

在实际应用程序中,应将样式放入外部文件中。您还可以为边框颜色定义颜色;这将使在运行时更容易更改颜色:

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class CustomLabelDemo extends Application {

    @Override
    public void start(Stage primaryStage) {
        Label label = new Label("Hello World");
        label.getStyleClass().add("custom-label");

        Button changeColorButton = new Button("Change to green");
        changeColorButton.setOnAction(event -> label.setStyle("custom-label-border-color: green;"));

        VBox root = new VBox(10, label, changeColorButton);
        root.setAlignment(Pos.CENTER);
        Scene scene = new Scene(root, 350, 75);
        scene.getStylesheets().add("custom-label.css");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
使用custom-label.css:

.custom-label {
    custom-label-border-color: red ;
    -fx-alignment: center;
    -fx-padding: 6px;
    -fx-background-color: custom-label-border-color, -fx-background;
    -fx-background-insets: 0, 4px;
}

.button {
    -fx-alignment: center ;
}
.custom-label {
    custom-label-border-color: green ;
    -fx-alignment: center;
    -fx-padding: 6px;
    -fx-background-color: custom-label-border-color, -fx-background;
    -fx-background-insets: 0, 4px;
}

.custom-label:error {
    custom-label-border-color: red ;
}

.check-box {
    -fx-alignment: center ;
}
如果颜色表示一组固定的状态,则可能需要使用伪类来表示该状态:

import javafx.application.Application;
import javafx.css.PseudoClass;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class CustomLabelDemo extends Application {

    @Override
    public void start(Stage primaryStage) {
        Label label = new Label("Hello World");
        label.getStyleClass().add("custom-label");

        CheckBox errorCheckBox = new CheckBox("Error");
        PseudoClass errorState = PseudoClass.getPseudoClass("error");
        errorCheckBox.selectedProperty().addListener((obs, wasSelected, isNowSelected) -> 
            label.pseudoClassStateChanged(errorState, isNowSelected));

        VBox root = new VBox(10, label, errorCheckBox);
        root.setAlignment(Pos.CENTER);
        Scene scene = new Scene(root, 350, 75);
        scene.getStylesheets().add("custom-label.css");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
custom-label.css:

.custom-label {
    custom-label-border-color: red ;
    -fx-alignment: center;
    -fx-padding: 6px;
    -fx-background-color: custom-label-border-color, -fx-background;
    -fx-background-insets: 0, 4px;
}

.button {
    -fx-alignment: center ;
}
.custom-label {
    custom-label-border-color: green ;
    -fx-alignment: center;
    -fx-padding: 6px;
    -fx-background-color: custom-label-border-color, -fx-background;
    -fx-background-insets: 0, 4px;
}

.custom-label:error {
    custom-label-border-color: red ;
}

.check-box {
    -fx-alignment: center ;
}

您应该能够使用标签,只需使用CSST对其进行配置,这是一种过度使用的方式<代码>标签
已经为您提供了执行此操作所需的一切。非常感谢!但是,如何访问边框颜色?假设物体[6]这个[]应该是一个正方形,你如何将标签的黑色轮廓改成其他形状?在这个例子中,边框是红色的。更改边框颜色的一种快速而肮脏的方法就是再次调用setStyle(…),用您想要的颜色替换“red”。但是有更好的方法,我会在回到电脑后更新。