gpt4 book ai didi

java - 如何在 javaFX 中创建带有左、中、右部分的工具栏?

转载 作者:搜寻专家 更新时间:2023-10-31 08:25:47 30 4
gpt4 key购买 nike

我正在尝试在 javafx 中创建自定义工具栏。此工具栏应该能够在其表面的中心、左侧和右侧(三个部分)显示控件。问题是我不知道要实现这个。我阅读了许多与此问题相关的提示,但它们对我不起作用,或者我做错了什么......

无论如何,我写了几个方法,它们代表了实现我的工具栏的不同方法,但没有一个能正常工作。这里有我的尝试:

  1. 使用 HBox Hgrow 属性作为 spring。没用。

    public ToolBar createToolBar()
    {
    ToolBar toolBar = new ToolBar();
    Pane emptyPane = new Pane();
    HBox spring = new HBox(emptyPane);
    spring.setHgrow(emptyPane, Priority.ALWAYS);
    toolBar.getItems().addAll(spring, new Label("LABEL"));
    return toolBar;
    }

2.左右两边都可以,中间那 block 怎么定义?

    public AnchorPane createToolBar2()
{
AnchorPane toolBar = new AnchorPane();
Label leftLabel = new Label("left");
Label rightLabel = new Label("right");
toolBar.getChildren().addAll(leftLabel, rightLabel);
toolBar.setLeftAnchor(leftLabel, 0.0);
toolBar.setRightAnchor(rightLabel, 0.0);
return toolBar;
}
  1. 此方法适用于布局,但我无法监听来自左侧和中心部分的事件,因为它们已被右侧部分覆盖(原因是 StackPane),因此此解决方案也无用。

    public StackPane createToolBar3()
    {
    StackPane toolBar = new StackPane();
    Button left = new Button("left button");
    Button right = new Button("right button");
    Button center = new Button("center button");
    HBox leftSection = new HBox(left);
    leftSection.setAlignment(Pos.CENTER_LEFT);
    HBox centerSection = new HBox(center);
    centerSection.setAlignment(Pos.CENTER);
    HBox rightSection = new HBox(right);
    rightSection.setAlignment(Pos.CENTER_RIGHT);
    toolBar.getChildren().addAll(leftSection, centerSection, rightSection);

    left.setOnAction(event -> System.out.println("left"));
    right.setOnAction(event -> System.out.println("right"));
    center.setOnAction(event -> System.out.println("center"));
    return toolBar;
    }

我在该代码块中调用的上述方法:

   @Override
public void start(Stage stage) {

BorderPane borderPane = new BorderPane();
borderPane.setPrefWidth(500);
borderPane.setPrefHeight(300);
borderPane.setTop(createToolBar4());
stage.setScene(new Scene(borderPane));
stage.show();
}

在这方面我将不胜感激。

最佳答案

用于工具栏部分对齐的 Spring 方法对我来说效果很好。

springtool

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;

public class SectionalToolbar extends Application {
@Override
public void start(Stage stage) throws Exception {
final Pane leftSpacer = new Pane();
HBox.setHgrow(
leftSpacer,
Priority.SOMETIMES
);

final Pane rightSpacer = new Pane();
HBox.setHgrow(
rightSpacer,
Priority.SOMETIMES
);

final ToolBar toolBar = new ToolBar(
new Button("Good"),
new Button("Boys"),
leftSpacer,
new Button("Deserve"),
new Button("Fruit"),
rightSpacer,
new Button("Always")
);
toolBar.setPrefWidth(400);

BorderPane layout = new BorderPane();
layout.setTop(toolBar);

stage.setScene(
new Scene(
layout
)
);
stage.show();
}
public static void main(String[] args) { launch(); }
}

A ToolBar (至少在 Java 8 的标准 modena.css 样式表中)已经在内部实现为 HBox 容器,因此您可以只使用 HBox.setHgrow调整放置在工具栏中的项目的内部布局约束的方法。

此示例中的左右间隔器被实现为空白 Pane ,它们会增长和收缩以适应可用空间。

如果你想要一个固定大小的间隔,而不是 HBox.setHgrow,你可以使用类似的东西:

leftSpacer.setPrefSize(20); 
leftSpacer.setMinSize(HBox.USE_PREF_SIZE);
leftSpacer.setMaxSize(HBox.USE_PREF_SIZE);

关于java - 如何在 javaFX 中创建带有左、中、右部分的工具栏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30832692/

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