如何在 JavaFX 中创建工具栏?
工具栏用于显示界面元素,例如按钮、切换按钮和分隔符等。不能向工具栏添加任何其他节点。工具栏的内容可以水平或垂直排列。可以通过实例化 javafx.scene.control.ToolBar 类来创建工具栏。
示例
以下示例演示了如何创建 ToolBar。
import javafx.application.Application;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Separator;
import javafx.scene.control.ToolBar;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class ToolBarExample extends Application {
public void start(Stage stage) {
Button bt1 = new Button("Java");
Button bt2 = new Button("JavaFX");
Separator sep1 = new Separator();
Button bt3 = new Button("Neo4J");
Button bt4 = new Button("MongoDB");
Separator sep2 = new Separator();
Button bt5 = new Button("Python");
Button bt6 = new Button("C++");
//Creating a tool bar
ToolBar toolBar = new ToolBar();
//Retrieving the items list of the toolbar
ObservableList list = toolBar.getItems();
list.addAll(bt1, bt2, sep1, bt3, bt4, sep2, bt5, bt6);
//Creating a vbox to hold the pagination
VBox vbox = new VBox();
vbox.setSpacing(15);
vbox.setPadding(new Insets(50, 50, 50, 100));
vbox.getChildren().addAll(toolBar);
//Setting the stage
Scene scene = new Scene(new Group(vbox), 595, 150, Color.BEIGE);
stage.setTitle("Tool Bar");
stage.setScene(scene);
stage.show();
}
public static void main(String args[]){
launch(args);
}
}输出

广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP