import javafx.application.Application;
import javafx.geometry.*;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.*;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
public class LoweredEtchedBorderDemo extends Application {
@Override
public void start(Stage stage) {
Label label = new Label("Testing");
label.setPadding(new Insets(10));
// uncomment to see the area that the content node is taking up within the border.
//label.setStyle("-fx-background-color: palegreen;");
BorderPane borderPane = new BorderPane(new StackPane(label));
// uncomment these two lines if you would like the border to resize to fit available space.
borderPane.setMinSize(Region.USE_COMPUTED_SIZE, Region.USE_COMPUTED_SIZE);
borderPane.setMaxSize(Region.USE_COMPUTED_SIZE, Region.USE_COMPUTED_SIZE);
VBox layout = new VBox(borderPane);
layout.setPadding(new Insets( 10));
layout.setStyle("-fx-base: lightgrey;");
VBox.setVgrow(borderPane, Priority.ALWAYS);
Scene scene = new Scene(layout);
stage.setScene(scene);
stage.show();
}
private class BorderPane extends Region {
// clip the bordered content within the bordered area.
Rectangle clipRect = new Rectangle(getWidth(), getHeight());
public BorderPane(Node content) {
super();
getChildren().add(content);
getStylesheets().add(getClass().getResource(
"border-pane.css"
).toExternalForm());
getStyleClass().add("border-pane");
// by default size the border to the preferred size of the content.
setMinSize(Region.USE_PREF_SIZE, Region.USE_PREF_SIZE);
setMaxSize(Region.USE_PREF_SIZE, Region.USE_PREF_SIZE);
content.setClip(clipRect);
}
@Override protected void layoutChildren() {
final double width = getWidth();
double height = getHeight();
double top = getInsets().getTop();
double right = getInsets().getRight();
double left = getInsets().getLeft();
double bottom = getInsets().getBottom();
double contentWidth = width - left - right;
double contentHeight = height - top - bottom;
Node child = getManagedChildren().get(0);
layoutInArea(child, left, top,
contentWidth, contentHeight,
0, null,
HPos.LEFT,
VPos.TOP);
clipRect.setWidth(contentWidth);
clipRect.setHeight(contentHeight);
}
}
public static void main(String[] args) {
launch(args);
}
}
# 1 楼答案
基于Region扩展名和外部CSS文件的示例
如果需要,此实现允许边框包含任意大小的内容(包括父布局窗格)。例如,您可以将包含内容的StackPane放在BorderPane内,并使用StackPane的“可选布局约束”来对齐内容,并为BorderPane内的内容定义边距(有关如何实现这一点的示例,请参阅链接的StackPane javadoc)
此外,边框本身的样式可以从外部CSS文件定制,因此应该可以相对轻松地复制任何standard Swing borders(和其他边框样式)
边框窗格。css
LoweredEtchedBorderLabelBackgroundDemo。java
相关问题
# 2 楼答案
我研究了JavaFX的
Lowered-Etched-Border
,但没有找到任何有效的文档。我还测试了InnerShadow
和其他效果,但这些效果并不适合。所以我创建了一个LEBLabel
(属于Label
的子类)具有这种边框样式