问题描述
上面的图片是我的代码的结果。但我想要如下。
This above picture is result of my code.But I want like the following.
如何解决?以下是我的代码。我读了太多的来源,但他们太复杂。例如,,我认为这方式非常复杂。也许有一个简单的方法来解决这个问题。
How can I fix it? The following is my code.I read too many sources but they was too complicated. For example,a source that I read, I think this way is very complicated.Maybe there is a easy way to solve this problem.
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class Test extends Application {
public BorderPane border = new BorderPane();
public Label name = new Label("Name");
public Label surname = new Label("Surname");
public TextField name1 = new TextField();
public TextField surname1 = new TextField();
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage arg0) throws Exception {
Scene scene = new Scene(new VBox(), 300, 200);
arg0.setTitle("Header");
arg0.setResizable(false);
scene.setFill(Color.OLDLACE);
StackPane grid = addStackPane();
border.setMargin(grid, new Insets(12,12,12,12));
border.setCenter(grid);
((VBox) scene.getRoot()).getChildren().add(border);
arg0.setScene(scene);
arg0.show();
}
@SuppressWarnings("static-access")
public StackPane addStackPane() {
StackPane pane = new StackPane();
GridPane grid = new GridPane();
Label title = new Label("Border Title");
title.setStyle("-fx-translate-y: -7");
pane.setAlignment(title, Pos.TOP_LEFT);
grid.setStyle("-fx-content-display: top");
grid.setStyle("-fx-border-insets: 20 15 15 15");
grid.setStyle("-fx-background-color: white");
grid.setStyle("-fx-border-color: black");
grid.setHgap(10);
grid.setVgap(10);
grid.setPadding(new Insets(25, 10, 25, 10));
grid.add(name, 1, 0);
grid.add(name1, 2, 0);
grid.add(surname, 1, 1);
grid.add(surname1, 2, 1);
pane.getChildren().addAll(grid, title);
return pane;
}
}
谢谢大家阅读这个主题。
Thank you all that reads this topic.
推荐答案
尝试设置 -fx-background-color
标题标签的颜色与borderPane的背景颜色相同。并且确保你在css文件中设置,因为不可能通过 setStyle()
设置多个样式,除非你连接它们:
Try to set -fx-background-color
of your title label to the same color as the borderPane's background. And make sure you set in a css file, because it's not possible to set multiple styles via setStyle()
unless you concatenate them:
myComponent.setStyle("-fx-text-fill: white;"+
"-fx-background-color: black;");
此外,使用InlineStyleSheets是一种不好的做法,因为它总是具有比规定的规则更高的优先级
Furthermore it is bad practice to use an InlineStyleSheets as it always has a higher priority than a rule specified in a CSS StyleSheet.
(如果将 pane.setAlignment(title,Pos.TOP_LEFT)
更改为 StackPane.setAlignment(title,Pos.TOP_LEFT)
您可以删除static-acces警告。)
(If you change pane.setAlignment(title, Pos.TOP_LEFT)
to StackPane.setAlignment(title, Pos.TOP_LEFT)
you can remove the "static-acces" warning.)
这篇关于JavaFX边框标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!