Java FXML StackPane不';t正确对齐儿童
我希望在堆栈窗格中对齐一个Java FXML StackPane不';t正确对齐儿童,java,javafx,javafx-8,fxml,Java,Javafx,Javafx 8,Fxml,我希望在堆栈窗格中对齐一个矩形和一个标签,但是我的代码没有达到预期的结果: FXML: 更新 如果我以编程方式调整大小,它会很好地对齐: this.pane.getChildren().addAll(this.bubble, this.message); bubble.setFill(Color.ALICEBLUE); bubble.setArcWidth(15.0); bubble.setArcHeight(15.0); bubble.setStroke(Color.BLACK); me
矩形
和一个标签
,但是我的代码没有达到预期的结果:
FXML:
更新
如果我以编程方式调整大小,它会很好地对齐:
this.pane.getChildren().addAll(this.bubble, this.message);
bubble.setFill(Color.ALICEBLUE);
bubble.setArcWidth(15.0);
bubble.setArcHeight(15.0);
bubble.setStroke(Color.BLACK);
message.setStyle("-fx-border-color:black; -fx-border-width: 1; -fx-border-style: solid;");
message.setText("message");
message.setPrefWidth(60.0);
message.setPrefHeight(25.0);
bubble.setWidth(70.0);
bubble.setHeight(30.0);
但是这样,我需要自己计算大小。基本上,如果你需要一个带有彩色背景的
文本或标签,最简单的方法是对单个标签或文本使用CSS,而不需要任何矩形
对象
将标签与CSS一起使用的示例
Main.java
application.css
@jewelsea对此问题的回答非常详细:
但是如果您想继续使用标签+矩形解决方案
我担心这与这个bug有关:
作为解决办法您可以请求更新堆栈窗格的布局,就像您在侦听器中尝试的那样
代码的唯一问题是您应该替换:
pane.layout();
与
这将确保布局发生在GUI线程上
示例
LabelRectangleTest.fxml
Main.java
基本上,如果您需要一个带有彩色背景的文本
或标签
,最简单的方法是将CSS用于单个标签
或文本
,而不需要任何矩形
对象
将标签与CSS一起使用的示例
Main.java
application.css
@jewelsea对此问题的回答非常详细:
但是如果您想继续使用标签+矩形解决方案
我担心这与这个bug有关:
作为解决办法您可以请求更新堆栈窗格的布局,就像您在侦听器中尝试的那样
代码的唯一问题是您应该替换:
pane.layout();
与
这将确保布局发生在GUI线程上
示例
LabelRectangleTest.fxml
Main.java
谢谢你的详细回答。我认为CSS解决方案会很好。谢谢你提供了非常详细的答案。我认为CSS解决方案会很好。
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
HBox root = new HBox();
Scene scene = new Scene(root,400,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
Label message = new Label();
TextArea textArea = new TextArea();
textArea.setPrefWidth(200);
textArea.setText("message");
message.textProperty().bind(textArea.textProperty());
message.getStyleClass().add("rounded-background-label");
root.getChildren().addAll(message, textArea);
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
.rounded-background-label {
-fx-background-color: black, white;
-fx-background-insets: 0, 1;
-fx-padding: 10px;
-fx-background-radius: 12px;
}
pane.layout();
Platform.runLater(new Runnable() {
@Override
public void run() {
pane.requestLayout();
}
});
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.shape.*?>
<HBox prefHeight="100.0" prefWidth="200.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="application.LabelRectangleTestController">
<children>
<HBox prefHeight="100.0" prefWidth="200.0">
<children>
<StackPane fx:id="pane" minHeight="126.0" prefHeight="126.0" prefWidth="200.0" HBox.hgrow="ALWAYS">
<children>
<Rectangle fx:id="bubble" arcHeight="15.0" arcWidth="15.0" fill="WHITE" height="27.75" stroke="BLACK" strokeType="INSIDE" width="68.0" />
<Label fx:id="message" text="Label" />
</children>
</StackPane>
<TextArea fx:id="textArea" prefWidth="200.0" wrapText="true" />
</children>
</HBox>
</children>
</HBox>
public class LabelRectangleTestController implements Initializable {
@FXML
private Label message;
@FXML
private Rectangle bubble;
@FXML
private StackPane pane;
@FXML
private TextArea textArea;
@Override
public void initialize(URL location, ResourceBundle resources) {
textArea.setText("message");
message.textProperty().bind(textArea.textProperty());
message.widthProperty().addListener((observable, oldValue, newValue) -> {
bubble.setWidth(newValue.intValue() + 10);
Platform.runLater(new Runnable() {
@Override
public void run() {
pane.requestLayout();
}
});
});
message.heightProperty().addListener((observable, oldValue, newValue) -> {
bubble.setHeight(newValue.doubleValue() + 10);
Platform.runLater(new Runnable() {
@Override
public void run() {
pane.requestLayout();
}
});
});
}
}
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("LabelRectangleTest.fxml"));
HBox root = loader.load();
Scene scene = new Scene(root,400,400);
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}