如何修复自定义时间线问题JavaFx?
我看过这个时间线构建,然后我尝试使用hbox和sperators以及标签来创建时间线,但我遇到了一个表示错误 以下控制器允许我在上述节点上构建此时间线如何修复自定义时间线问题JavaFx?,java,javafx,Java,Javafx,我看过这个时间线构建,然后我尝试使用hbox和sperators以及标签来创建时间线,但我遇到了一个表示错误 以下控制器允许我在上述节点上构建此时间线 public class GuiMainController implements Initializable { @FXML private BorderPane borderPane; public void getUnit1(){ //This will fill the Time Line
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();
}
}
public类GuiMainController实现可初始化{
@FXML
私有边框窗格边框窗格;
public void getUnit1(){
//这将填充Gui中的时间线单位
VBox VBox=新的VBox();
HBox HBox=新的HBox();
hbox.setManaged(false);
对于(int i=0;i<24;i++){
//为每个单元创建一个新实例
锚烷锚烷=新锚烷();
锚烷高度(30);
锚泊烷。宽度(66);
分离器分离器R=新分离器();
分离器设置布局X(66);
分离器设置布局(14);
分离器设置方向(方向垂直);
分离器设置高度(15);
分离器分离器=新分离器();
分离性毛状体(14);
分离。设置方向(方向。垂直);
分隔高度(15);
分离器分离器L=新分离器();
分隔符setLayoutX(33);
分离器设置布局(20);
分离器设置方向(方向垂直);
分离器设置高度(10);
分隔符设置预宽(6);
标签lblT=新标签();
lblT.setText(i+“h”);
标签lblTT=新标签();
lblTT.setLayoutX(66);
lblTT.setText((i+1)+“h”);
getChildren().addAll(separatorL,lblT,separatorM,separatorR,lblTT);
hbox.getChildren().add(anchorPane);
}
borderPane.getChildren().add(hbox);
}
公共void初始化(URL arg0,ResourceBundle arg1){
getUnit1();
}
}
我怎样才能制定这个时间表,如果有任何帮助,我们将不胜感激
我想你不应该在这里涉及任何布局,因为这样你就依赖于这些布局的精确实现。此外,我不确定这里是否应该包括
分隔符。您可以使用路径
计算轴上所有直线的位置
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();
}
private静态标签createLabel(字符串文本、双布局、双majorTickDistance、int索引){
标签=新标签(文本);
//标签宽度应该正好是刻度之间的距离
label.setMaxWidth(Region.USE_PREF_SIZE);
label.setMinWidth(区域。使用前缀大小);
label.setPrefWidth(majorTickDistance);
//中心文本
标签。设置对齐(位置基线\中心);
label.setLayoutY(layoutY);
//将标签的中心与主刻度对齐
label.setLayoutX((索引+0.5)*主路径距离);
退货标签;
}
/**
*@param majorTickDistance主刻度之间的宽度
*@param minorTicks主刻度之间的次刻度数
*@param majorTickHeight主要记号标记的高度
*@param minorTickHeight次要记号标记的高度
*@param firstText第一个主刻度的第一个文本
*@param text其他记号的文本
*/
专用静态窗格createAxis(双主轨迹距离,int minorTicks,
double majorTickHeight、double minorTickHeight、String firstText、String…text){
最终双标签=主标签高度+3;
最终双minorTickDistance=主minorTickDistance/minorTicks;
//使用第一个主记号和水平线初始化路径
路径=新路径(
新的MoveTo(0,0),新的HLineTo(majorTickDistance*text.length),
新MoveTo(0,0),新VLineTo(majorTickHeight)
);
//添加路径和第一个标签
Pane Pane=新窗格(路径,createLabel(firstText,labelY,majorTickDistance,-1));
for(int i=0;i
我想您不应该在这里涉及任何布局,因为这样您就依赖于这些布局的精确实现。此外,我不确定这里是否应该包括分隔符。您可以使用路径
计算轴上所有直线的位置
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();
}
private静态标签createLabel(字符串文本、双布局、双majorTickDistance、int索引){
标签=新标签(文本);
//标签宽度应该正好是刻度之间的距离
label.setMaxWidth(Region.USE_PREF_SIZE);
label.setMinWidth(区域。使用前缀大小);
label.setPrefWidth(majorTickDistance);
//中心文本
标签。设置对齐(位置基线\中心);
label.setLayoutY(layoutY);
//将标签的中心与主刻度对齐
label.setLayoutX((索引+0.5)*主路径距离);
退货标签;
}
/**
*@param majorTi