- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试像这样布置我的节点: 这是我当前的布局,称为 CircularPane
:
import javafx.geometry.HPos;
import javafx.geometry.VPos;
import javafx.scene.Node;
import javafx.scene.layout.Pane;
public class CircularPane extends Pane {
@Override
protected void layoutChildren() {
final int radius = 50;
final double increment = 360 / getChildren().size();
double degreese = 0;
for (Node node : getChildren()) {
double x = radius * Math.cos(Math.toRadians(degreese)) + getWidth() / 2;
double y = radius * Math.sin(Math.toRadians(degreese)) + getHeight() / 2;
layoutInArea(node, x - node.getBoundsInLocal().getWidth() / 2, y - node.getBoundsInLocal().getHeight() / 2, getWidth(), getHeight(), 0.0, HPos.LEFT, VPos.TOP);
degreese += increment;
}
}
}
这是我的主要类(class):
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) {
CircularPane pane = new CircularPane();
for(int i = 0; i < 6; i++) {
Button button = new Button("" + i);
pane.getChildren().add(button);
}
stage.setScene(new Scene(pane));
stage.show();
}
}
这是我当前的显示:
节点不在底部接触,它们均匀分布在圆周上。我想做到这一点,让他们走到底部,但不知道该怎么做。
最佳答案
您将按钮布局在一个圆圈上的方法是正确的,但在这一行中您定义了它们的布局方式:
final double increment = 360 / getChildren().size();
这给出了从圆心引用的任意两个按钮之间的相同角度!这就是您获得当前显示的原因。
如果你想像你图中那样布置节点,如果我做对了,这些是条件:
因此让我们为这些值定义一些字段,并调整 Pane 的大小:
class CircularPane extends Pane {
private final double radius;
private final double ext_gap;
private final double int_gap;
public CircularPane(double radius, double ext_gap, double int_gap){
this.radius=radius;
this.ext_gap=ext_gap;
this.int_gap=int_gap;
setMinSize(2*radius, 2d*radius);
setPrefSize(2*radius, 2d*radius);
setMaxSize(2*radius, 2d*radius);
}
}
现在,给定任何 n
按钮,上述条件可以转化为一个求解节点大小的方程式。如果总可用长度 (2*radius
) 减去两个外部间隙 (2*ext_gap
) 与大小为 buttonSize
的 n 个按钮相同,并且n-1
个内部间隙 (int_size
),则每个按钮的大小必须为:
@Override
protected void layoutChildren() {
int n=getChildren().size();
double buttonSize = (2*radius-2*ext_gap-(n-1)*int_gap)/n;
}
最后,现在您可以设置按钮的大小并布局每个节点,只需增加 x
坐标(按按钮的大小加上内部间隙),然后获取 y
圆方程的坐标:
@Override
protected void layoutChildren() {
int n=getChildren().size();
double buttonSize = (2*radius-2*ext_gap-(n-1)*int_gap)/n;
double x=ext_gap+buttonSize/2d, y;
for (Node node : getChildren()) {
((Button)node).setMinSize(buttonSize, buttonSize);
((Button)node).setPrefSize(buttonSize, buttonSize);
((Button)node).setMaxSize(buttonSize, buttonSize);
node.setStyle("-fx-font-size: "+Math.round(buttonSize/3));
node.setManaged(false);
y=getHeight()/2d+Math.sqrt(radius*radius-Math.pow(x-radius,2d));
layoutInArea(node, x-buttonSize/2d, y-buttonSize/2d, getWidth(), getHeight(), 0.0, HPos.LEFT, VPos.TOP);
x+=buttonSize+int_gap;
}
}
请注意,您还可以更改字体的大小,以获得任何大小的按钮的可见数字。
另请注意,node.setManaged(false);
避免了在您单击按钮时调用 layoutChildren()
(由于单击按钮的大小发生变化当被聚焦或点击时)。
最后这将创建圆形 Pane 并绘制一个圆圈:
@Override
public void start(Stage primaryStage) {
CircularPane pane = new CircularPane(200,20,10);
for(int i = 0; i < 6; i++) {
Button button = new Button("" + (i+1));
pane.getChildren().add(button);
}
Circle circle = new Circle(200);
circle.setFill(null);
circle.setStroke(Color.BLACK);
StackPane stack=new StackPane(circle,pane);
Scene scene = new Scene(stack, 500, 500);
primaryStage.setScene(scene);
primaryStage.show();
}
结果是:
关于java - 将节点布置成圆形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30598631/
首先,这或多或少是我的第一个 GUI,而且我学习 Java 的时间不超过一周,因此它可能包含一些严重的编程错误。 我现在拥有的是:按钮和标签是 OptionPanel 的一部分,位于左侧,Drawin
当窗口大小调整时,Windows 会发送几条消息: WM_GETMINMAXINFO WM_ENTERSIZEMOVE WM_EXITSIZEMOVE WM_NCCALCSIZE WM_SIZING
我的问题:我有一个使用 UINavigationController 的用户入职流程介绍入职流程中的每个步骤。使用 UISearchBar 推送 View Controller 时在其中,UISear
我是一名优秀的程序员,十分优秀!