gpt4 book ai didi

java - 如何解决JavaFx自定义时间线问题?

转载 作者:行者123 更新时间:2023-12-02 01:56:41 27 4
gpt4 key购买 nike

我已经看到了这个时间线构建 here然后我尝试使用 hbox 和 sperators 和标签来制作时间线,但我陷入了表示错误

下面的 Controller 允许我在上面的节点上构建这个时间线

public class GuiMainController implements Initializable {

@FXML
private BorderPane borderPane;

public void getUnit1(){
//This will fill the Time Line Unit in your Gui
VBox vbox = new VBox();
HBox hbox = new HBox();
hbox.setManaged(false);
for (int i =0; i < 24; i++) {
//For each unit create a new instance
AnchorPane anchorPane = new AnchorPane();
anchorPane.setPrefHeight(30);
anchorPane.setPrefWidth(66);

Separator separatorR = new Separator();
separatorR.setLayoutX(66);
separatorR.setLayoutY(14);
separatorR.setOrientation(Orientation.VERTICAL);
separatorR.setPrefHeight(15);

Separator separatorM = new Separator();
separatorM.setLayoutY(14);
separatorM.setOrientation(Orientation.VERTICAL);
separatorM.setPrefHeight(15);

Separator separatorL = new Separator();
separatorL.setLayoutX(33);
separatorL.setLayoutY(20);
separatorL.setOrientation(Orientation.VERTICAL);
separatorL.setPrefHeight(10);
separatorL.setPrefWidth(6);

Label lblT = new Label();
lblT.setText(i+"h");

Label lblTT = new Label();
lblTT.setLayoutX(66);
lblTT.setText((i+1)+"h");

anchorPane.getChildren().addAll(separatorL,lblT,separatorM,separatorR,lblTT);
hbox.getChildren().add(anchorPane);
}
borderPane.getChildren().add(hbox);
}
public void initialize(URL arg0, ResourceBundle arg1) {
getUnit1();
}
}

enter image description here

我怎样才能制作这个时间表,感谢任何帮助来获得这样的时间线 enter image description here

最佳答案

恕我直言,您不应该在这里涉及任何布局,因为这样您就依赖于这些布局的确切实现。此外,我不确定这里是否应该涉及 Seperator 。不过,您可以使用 Path 并计算轴上所有线的位置。

private static Label createLabel(String text, double layoutY, double majorTickDistance, int index) {
Label label = new Label(text);

// label width should be exactly the distance between ticks
label.setMaxWidth(Region.USE_PREF_SIZE);
label.setMinWidth(Region.USE_PREF_SIZE);
label.setPrefWidth(majorTickDistance);

// center text
label.setAlignment(Pos.BASELINE_CENTER);
label.setLayoutY(layoutY);

// align center of the label with major tick
label.setLayoutX((index + 0.5) * majorTickDistance);
return label;
}

/**
* @param majorTickDistance the width between major ticks
* @param minorTicks the number of minor ticks between major ticks
* @param majorTickHeight the height of major tick markers
* @param minorTickHeight the height of minor tick markers
* @param firstText the first text for the first major tick
* @param text the text for other ticks
*/
private static Pane createAxis(double majorTickDistance, int minorTicks,
double majorTickHeight, double minorTickHeight, String firstText, String... text) {
final double labelY = majorTickHeight + 3;
final double minorTickDistance = majorTickDistance / minorTicks;

// initialize path with first major tick and horizontal line
Path path = new Path(
new MoveTo(0, 0), new HLineTo(majorTickDistance * text.length),
new MoveTo(0, 0), new VLineTo(majorTickHeight)
);

// add path and first label
Pane pane = new Pane(path, createLabel(firstText, labelY, majorTickDistance, -1));

for (int i = 0; i < text.length; i++) {
double offset = i * majorTickDistance;

// add minor ticks
for (int j = 1; j < minorTicks; j++) {
double x = offset + j * minorTickDistance;
path.getElements().addAll(new MoveTo(x, 0), new VLineTo(minorTickHeight));
}
offset += majorTickDistance;

// add major tick at the end of the current interval
path.getElements().addAll(new MoveTo(offset, 0), new VLineTo(majorTickHeight));

// add label below major tick
pane.getChildren().add(createLabel(text[i], labelY, majorTickDistance, i));
}

pane.setMaxSize(Region.USE_PREF_SIZE, Region.USE_PREF_SIZE);

return pane;
}

@Override
public void start(Stage primaryStage) throws Exception {

Scene scene = new Scene(new StackPane(createAxis(30, 5, 10, 5, "1", "2", "3", "4", "5", "6")), 400, 400);

primaryStage.setScene(scene);
primaryStage.show();
}

关于java - 如何解决JavaFx自定义时间线问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52188838/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com