javafx vbox 居中_JavaFX在边界窗格中居中放置一个可调整大小的弧线

创建一个组。在组中放置一个矩形(整个圆的大小)(例如,矩形的高度和宽度设置为圆的直径),然后将圆弧添加到组中,并将圆弧布局位置设置为圆的半径。将组放在StackPane中,以便固定大小的组将在可调整大小的区域中居中。将StackPane放在BorderPane的中心。将StackPane的最小大小设置为零,以使其大小小于Group,使Group保持居中并在其可见边界内剪切。在BorderPane的底部添加一些控件,以便您可以动态控制弧的长度。

import javafx.application.Application;

import javafx.geometry.Insets;

import javafx.scene.*;

import javafx.scene.control.Slider;

import javafx.scene.layout.*;

import javafx.scene.paint.Color;

import javafx.scene.shape.*;

import javafx.stage.Stage;

public class ArcControl extends Application {

@Override

public void start(Stage stage) throws Exception {

CenteredArc centeredArc = new CenteredArc();

ArcControls arcControls = new ArcControls(centeredArc.getArc());

BorderPane layout = new BorderPane();

layout.setCenter(centeredArc.getArcPane());

layout.setBottom(arcControls.getControlPane());

stage.setScene(new Scene(layout));

stage.show();

}

public static void main(String[] args) {

launch(args);

}

}

class CenteredArc {

private static final double INSET = 10;

private static final double ARC_RADIUS = 100;

private static final double INITIAL_ARC_LENGTH = 60;

private static final double ARC_STROKE_WIDTH = 10;

private static final double ARC_REGION_SIZE =

ARC_RADIUS * 2 + ARC_STROKE_WIDTH + INSET * 2;

private final Arc arc;

private final Pane arcPane;

public CenteredArc() {

// Create the arc.

arc = new Arc(

ARC_REGION_SIZE / 2, ARC_REGION_SIZE / 2,

ARC_RADIUS, ARC_RADIUS,

0,

INITIAL_ARC_LENGTH

);

arc.setStrokeWidth(10);

arc.setStrokeLineCap(StrokeLineCap.ROUND);

arc.setStroke(Color.FORESTGREEN);

arc.setFill(Color.POWDERBLUE);

// Create a background fill on which the arc will be centered.

// The paint of the background fill can be set to Color.TRANSPARENT

// if you don't want the fill to be seen.

final double fillSize = ARC_RADIUS * 2 + arc.getStrokeWidth() + INSET * 2;

Rectangle fill = new Rectangle(fillSize, fillSize, Color.PINK);

// Place the fill and the arc in the group.

// The Group will be a fixed sized matching the fill size.

Group centeredArcGroup = new Group(fill, arc);

// place the arc group in a StackPane so it is centered in a resizable region.

arcPane = new StackPane(centeredArcGroup);

arcPane.setPadding(new Insets(INSET));

arcPane.setMinSize(0, 0);

arcPane.setStyle("-fx-background-color: papayawhip;");

}

public Arc getArc() {

return arc;

}

public Pane getArcPane() {

return arcPane;

}

}

// helper class which can use a slider to control an arc.

class ArcControls {

private static final double INSET = 10;

private final Slider slider;

private final VBox controlPane;

public ArcControls(Arc arc) {

slider = new Slider(0, 360, arc.getLength());

controlPane = new VBox(

slider

);

controlPane.setPadding(

new Insets(INSET)

);

controlPane.setStyle(

"-fx-background-color: palegreen;"

);

arc.lengthProperty().bind(slider.valueProperty());

}

public Slider getSlider() {

return slider;

}

public VBox getControlPane() {

return controlPane;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值