工具栏类是JavaFX的一部分。工具栏是垂直或水平显示项目的控件。按钮、切换按钮和分隔符通常放置在工具栏中。也可以将任何节点插入其中。如果工具栏中的项目太多,无法选择当前在工具栏中不可见的项目,则会出现溢出按钮。工具栏将focusTraversable设置为false。
null
类的构造函数:
- 工具栏() :创建一个空工具栏。
- 工具栏(节点…n) :创建用指定节点填充的工具栏。
常用方法:
方法 | 解释 |
---|---|
getItems() | 返回工具栏上的项目。 |
getOrientation() | 返回工具栏的方向。 |
设置方向(方向v) | 设置对象方向的值。 |
下面的程序演示了ToolBar类的使用:
- 创建工具栏并将其添加到场景的Java程序: 在这个程序中,我们将创建一个名为 工具栏 .我们还将创建一个名为 标签 还有两个按钮名为 按钮1 和 按钮2 并将它们添加到工具栏。将工具栏添加到名为 vbox 并添加 VBox 去现场。然后将场景添加到舞台上,并调用 show() 函数来显示结果。
// Java program to create a toolbar
// and add it to the scene
import
javafx.application.Application;
import
javafx.scene.Scene;
import
javafx.scene.control.*;
import
javafx.scene.layout.*;
import
javafx.stage.Stage;
import
javafx.event.ActionEvent;
import
javafx.event.EventHandler;
import
javafx.scene.Group;
public
class
Toolbar
extends
Application {
// launch the application
public
void
start(Stage stage)
{
try
{
// set title for the stage
stage.setTitle(
"creating toolbar"
);
// create a label
Label label =
new
Label(
"Toolbar"
);
// creating buttons
Button button1 =
new
Button(
"Button1"
);
Button button2 =
new
Button(
"Button2"
);
// creating toolbar
ToolBar toolbar =
new
ToolBar(label, button1, button2);
// create a VBox
VBox vbox =
new
VBox(toolbar);
// create a scene
Scene scene =
new
Scene(vbox,
300
,
300
);
// set the scene
stage.setScene(scene);
stage.show();
}
catch
(Exception e) {
System.out.println(e.getMessage());
}
}
// Main Method
public
static
void
main(String args[])
{
// launch the application
launch(args);
}
}
输出:
- Java程序创建工具栏并将其添加到场景中,并设置工具栏的方向: 在这个程序中,我们将创建一个名为 工具栏 .我们还将创建一个名为 标签 还有两个按钮名为 按钮1 和 按钮2 并使用 getItems()。添加() 作用使用 setOrientation() 作用现在将工具栏添加到名为 hbox 并将HBox添加到 场景 .最后将场景添加到舞台上,并调用 show() 函数来显示结果。
// Java program to create a toolbar and
// add it to the scene and set orientation
// of the toolbar
import
javafx.application.Application;
import
javafx.scene.Scene;
import
javafx.scene.control.*;
import
javafx.scene.layout.*;
import
javafx.stage.Stage;
import
javafx.event.ActionEvent;
import
javafx.event.EventHandler;
import
javafx.scene.Group;
import
javafx.geometry.*;
public
class
Toolbar_1
extends
Application {
// launch the application
public
void
start(Stage stage)
{
try
{
// set title for the stage
stage.setTitle(
"creating toolbar"
);
// create a label
Label label =
new
Label(
"Toolbar"
);
// creating buttons
Button button1 =
new
Button(
"Button1"
);
Button button2 =
new
Button(
"Button2"
);
// creating toolbar
ToolBar toolbar =
new
ToolBar();
// add items
toolbar.getItems().add(label);
toolbar.getItems().add(button1);
toolbar.getItems().add(button2);
// set orientation of the toolbar
toolbar.setOrientation(Orientation.VERTICAL);
// create a HBox
HBox hbox =
new
HBox(toolbar);
// create a scene
Scene scene =
new
Scene(hbox,
300
,
300
);
// set the scene
stage.setScene(scene);
stage.show();
}
catch
(Exception e) {
System.out.println(e.getMessage());
}
}
// Main Method
public
static
void
main(String args[])
{
// launch the application
launch(args);
}
}
输出:
注: 上述程序可能无法在联机IDE中运行。请使用脱机编译器。
参考: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/ToolBar.html
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END