简体   繁体   English

是否可以在 JavaFX 中为子窗格设置规则只为父窗格制作圆角?

[英]Is it possible to make round corners for child Panes settings rules only to parent Pane in JavaFX?

I have the following fxml:我有以下 fxml:

<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<StackPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <AnchorPane maxHeight="200.0" maxWidth="200.0" prefHeight="200.0" prefWidth="200.0">
         <children>
            <VBox style="-fx-border-radius: 10; -fx-border-color: black; -fx-border-width: 1;" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
               <children>
                  <Pane maxWidth="200.0" prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: red;" />
                  <Pane prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: cyan;" />
                  <Pane prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: green;" />
               </children>
            </VBox>
         </children>
      </AnchorPane>
   </children>
</StackPane>

And this is the result:这是结果: 在此处输入图片说明

VBox has the following css rule: VBox 具有以下 css 规则:

-fx-border-radius: 10; 
-fx-border-color: black; 
-fx-border-width: 1;

The problem is that child corners (total 4 corners) are outside parent - I mean that 4 child corners are not round according to parent radius.问题是子角(总共 4 个角)在父角之外 - 我的意思是根据父半径,4 个子角不是圆形的。 Is it possible to fix it without setting rules to child panes?是否可以在不为子窗格设置规则的情况下修复它? If yes, then how?如果是,那么如何? I am asking it because in real application I have in parent many different children and order of these children can change and I don't want to control child radius.我问它是因为在实际应用中,我在父母中有许多不同的孩子,这些孩子的顺序可以改变,我不想控制孩子的半径。

You might use shape clipping with a round rectangle.您可以对圆角矩形使用形状剪裁。 Bind its coordinates and dimensions to the source pane and it should work flawlessly.将其坐标和尺寸绑定到源窗格,它应该可以正常工作。

import javafx.scene.shape.*;

VBox box = ...;
Rectangle r = new Rectangle();
r.widthProperty().bind(box.widthProperty());
r.heightProperty().bind(box.heightProperty());
r.setArcWidth(10);
r.setArcHeight(10);

box.setClip(r);

a bit late as I can see, but you could achieve this without having to do any extra work.据我所知,有点晚了,但是您无需做任何额外的工作就可以实现这一目标。 You only have to use css : In your css file, simply select the AnchorPanes using css.您只需要使用 css :在您的 css 文件中,只需使用 css 选择 AnchorPanes。 For an instance, let's think that your root's id is #rootPane, you can use #rootPane to style the rest of the Anchor Panes using the following way,举个例子,假设你的 root 的 id 是 #rootPane,你可以使用 #rootPane 使用以下方式设置其余的 Anchor Panes 的样式,

#rootPane AnchorPane{
    -fx-background-radius:200; /* a value of your preference*/
}

will get exactly what you want.会得到你想要的。 The output is here : The Output输出在这里:输出

I've added colours to the Anchor Panes externally, just for visual purposes我在外部为锚窗格添加了颜色,仅用于视觉目的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM