Button类是JavaFX包的一部分,它可以包含文本或图形,也可以同时包含文本和图形。
JavaFX中的按钮可以有三种不同的类型:
- 正常按钮: 正常的按钮
- 默认按钮: 接收键盘VK_回车键的默认按钮
- 取消按钮: 接收键盘VK_回车键的取消按钮
按下按钮时,会发送一个动作事件。此操作事件可由EventHandler管理。按钮还可以通过实现EventHandler来处理MouseEvent来响应鼠标事件。
Button类的构造函数是 :
- 按钮() :创建一个标签为空字符串的按钮。
- 按钮(字符串t) :创建以指定文本作为标签的按钮。
- 按钮(字符串t,节点g) :创建带有指定文本和图标的按钮作为其标签。
常用方法 :
方法 | 解释 |
---|---|
setCancelButton(布尔值v) | 设置属性cancelButton的值。 |
setDefaultButton(布尔值v) | 设置属性defaultButton的值 |
isDefaultButton() | 获取属性defaultButton的值。 |
isCancelButton() | 获取属性cancelButton的值。 |
取消按钮属性() | 取消按钮是接收键盘VK_ESC按下的按钮 |
defaultButtonProperty() | 默认按钮是接收键盘VK_回车键的按钮 |
createDefaultSkin() | 为此控件创建默认外观的新实例。 |
下面的程序演示了JavaFX中按钮的使用。
- 创建按钮并将其添加到舞台的程序: 该程序创建一个由名称指示的按钮 B 。该按钮将在场景中创建,而场景又将在舞台中托管。函数setTitle()用于为舞台提供标题。然后创建一个平铺窗格,在该窗格上调用addChildren()方法将按钮附加到场景中。最后,调用show()方法来显示最终结果。
// Java Program to create a button and add it to the stage
import
javafx.application.Application;
import
javafx.scene.Scene;
import
javafx.scene.control.Button;
import
javafx.scene.layout.StackPane;
import
javafx.stage.Stage;
public
class
button
extends
Application {
// launch the application
public
void
start(Stage s)
{
// set title for the stage
s.setTitle(
"creating buttons"
);
// create a button
Button b =
new
Button(
"button"
);
// create a stack pane
StackPane r =
new
StackPane();
// add button
r.getChildren().add(b);
// create a scene
Scene sc =
new
Scene(r,
200
,
200
);
// set the scene
s.setScene(sc);
s.show();
}
public
static
void
main(String args[])
{
// launch the application
launch(args);
}
}
输出 :
- Java程序创建一个按钮并向其添加事件处理程序 :该程序创建一个名为b的按钮。该按钮将在场景内创建,而场景又将在舞台内托管。我们将创建一个标签来显示按钮是否按下。函数setTitle()用于为舞台提供标题。然后创建一个平铺窗格,在该窗格上调用addChildren()方法将按钮和标签附加到场景中。最后,调用show()方法来显示最终结果。我们将创建一个事件处理程序来处理按钮事件。事件处理程序将使用setOnAction()函数添加到按钮中。
// Java program to create a button and add event handler to it
import
javafx.application.Application;
import
javafx.scene.Scene;
import
javafx.scene.control.Button;
import
javafx.scene.layout.*;
import
javafx.event.ActionEvent;
import
javafx.event.EventHandler;
import
javafx.scene.control.Label;
import
javafx.stage.Stage;
public
class
button_1
extends
Application {
// launch the application
public
void
start(Stage s)
{
// set title for the stage
s.setTitle(
"creating buttons"
);
// create a button
Button b =
new
Button(
"button"
);
// create a stack pane
TilePane r =
new
TilePane();
// create a label
Label l =
new
Label(
"button not selected"
);
// action event
EventHandler<ActionEvent> event =
new
EventHandler<ActionEvent>() {
public
void
handle(ActionEvent e)
{
l.setText(
" button selected "
);
}
};
// when button is pressed
b.setOnAction(event);
// add button
r.getChildren().add(b);
r.getChildren().add(l);
// create a scene
Scene sc =
new
Scene(r,
200
,
200
);
// set the scene
s.setScene(sc);
s.show();
}
public
static
void
main(String args[])
{
// launch the application
launch(args);
}
}
输出 :
- Java程序创建一个带有图像的按钮,并向其添加事件处理程序 :此程序创建一个按钮,按钮上的图像由名称b指示。将使用导入图像的文件输入流包含该图像。然后,我们将使用文件输入流的对象创建一个图像,然后使用该图像文件创建一个图像视图。该按钮将在场景中创建,而场景又将在舞台中托管。我们将创建一个标签来显示按钮是否按下。函数setTitle()用于为舞台提供标题。然后创建一个平铺窗格,在该窗格上调用addChildren()方法将按钮和标签附加到场景中。最后,调用show()方法来显示最终结果。我们将创建一个事件处理程序来处理按钮事件。事件处理程序将使用setOnAction()函数添加到按钮中。
// Java Program to create a button with a image and
// add event handler to it
import
javafx.application.Application;
import
javafx.scene.Scene;
import
javafx.scene.control.Button;
import
javafx.scene.layout.*;
import
javafx.scene.image.*;
import
java.io.*;
import
javafx.event.ActionEvent;
import
javafx.event.EventHandler;
import
javafx.scene.control.Label;
import
javafx.stage.Stage;
import
java.net.*;
public
class
button_2
extends
Application {
// launch the application
public
void
start(Stage s)
throws
Exception
{
// set title for the stage
s.setTitle(
"creating buttons"
);
// create a input stream
FileInputStream input =
new
FileInputStream(
"f:\gfg.png"
);
// create a image
Image i =
new
Image(input);
// create a image View
ImageView iw =
new
ImageView(i);
// create a button
Button b =
new
Button(
""
, iw);
// create a stack pane
TilePane r =
new
TilePane();
// create a label
Label l =
new
Label(
"button not selected"
);
// action event
EventHandler<ActionEvent> event =
new
EventHandler<ActionEvent>() {
public
void
handle(ActionEvent e)
{
l.setText(
"button selected "
);
}
};
// when button is pressed
b.setOnAction(event);
// add button
r.getChildren().add(b);
r.getChildren().add(l);
// create a scene
Scene sc =
new
Scene(r,
200
,
200
);
// set the scene
s.setScene(sc);
s.show();
}
public
static
void
main(String args[])
{
// launch the application
launch(args);
}
}
输出
- Java程序创建带有图像和文本的按钮,并向其添加事件处理程序
该程序创建一个按钮,按钮上有一个图像和一个由名称b指示的文本。该图像将使用导入图像的文件输入流包含在内。然后,我们将使用文件输入流的对象创建一个图像,然后使用该图像文件创建一个图像视图。该按钮将在场景中创建,而场景又将在舞台中托管。我们将创建一个标签来显示按钮是否按下。函数setTitle()用于为舞台提供标题。然后创建一个平铺窗格,在该窗格上调用addChildren()方法将按钮和标签附加到场景中。最后,调用show()方法来显示最终结果。我们将创建一个事件处理程序来处理按钮事件。事件处理程序将使用setOnAction()函数添加到按钮中。
// Java Program to create a button with a image
// and text and add event handler to it
import
javafx.application.Application;
import
javafx.scene.Scene;
import
javafx.scene.control.Button;
import
javafx.scene.layout.*;
import
javafx.scene.image.*;
import
java.io.*;
import
javafx.event.ActionEvent;
import
javafx.event.EventHandler;
import
javafx.scene.control.Label;
import
javafx.stage.Stage;
import
java.net.*;
public
class
button_3
extends
Application {
// launch the application
public
void
start(Stage s)
throws
Exception
{
// set title for the stage
s.setTitle(
"creating buttons"
);
// create a input stream
FileInputStream input =
new
FileInputStream(
"f:\gfg.png"
);
// create a image
Image i =
new
Image(input);
// create a image View
ImageView iw =
new
ImageView(i);
// create a button
Button b =
new
Button(
"Button"
, iw);
// create a stack pane
TilePane r =
new
TilePane();
// create a label
Label l =
new
Label(
"button not selected"
);
// action event
EventHandler<ActionEvent> event =
new
EventHandler<ActionEvent>() {
public
void
handle(ActionEvent e)
{
l.setText(
"button selected "
);
}
};
// when button is pressed
b.setOnAction(event);
// add button
r.getChildren().add(b);
r.getChildren().add(l);
// create a scene
Scene sc =
new
Scene(r,
200
,
200
);
// set the scene
s.setScene(sc);
s.show();
}
public
static
void
main(String args[])
{
// launch the application
launch(args);
}
}
输出 :
- 创建默认按钮和取消按钮的Java程序: 该程序创建一个由名称b和b1指示的按钮。按钮b将作为取消按钮,响应键盘的退出按键,按钮b1将作为默认按钮,响应键盘的进入按键)。该按钮将在场景中创建,而场景又将在舞台中托管。我们将创建一个标签来显示按下了哪个按钮。函数setTitle()用于为舞台提供标题。然后创建一个平铺窗格,在该窗格上调用addChildren()方法将按钮和标签附加到场景中。最后,调用show()方法来显示最终结果。我们将创建一个事件处理程序来处理按钮事件。事件处理程序将使用setOnAction()函数添加到按钮中。
// Java program to create a default button and a
// cancel button and add event handler to it
import
javafx.application.Application;
import
javafx.scene.Scene;
import
javafx.scene.control.Button;
import
javafx.scene.layout.*;
import
javafx.event.ActionEvent;
import
javafx.event.EventHandler;
import
javafx.scene.control.Label;
import
javafx.stage.Stage;
public
class
button_4
extends
Application {
// launch the application
public
void
start(Stage s)
{
// set title for the stage
s.setTitle(
"creating buttons"
);
// create a button
Button b =
new
Button(
"cancel button"
);
// set cancel button
b.setCancelButton(
true
);
// create a button
Button b1 =
new
Button(
"default button"
);
// set default button
b1.setDefaultButton(
true
);
// create a stack pane
TilePane r =
new
TilePane();
// create a label
Label l =
new
Label(
"button not selected"
);
// action event
EventHandler<ActionEvent> event =
new
EventHandler<ActionEvent>() {
public
void
handle(ActionEvent e)
{
l.setText(
" cancel button selected "
);
}
};
EventHandler<ActionEvent> event1 =
new
EventHandler<ActionEvent>() {
public
void
handle(ActionEvent e)
{
l.setText(
" default button selected "
);
}
};
// when button is pressed
b.setOnAction(event);
b1.setOnAction(event1);
// add button
r.getChildren().add(b);
r.getChildren().add(b1);
r.getChildren().add(l);
// create a scene
Scene sc =
new
Scene(r,
200
,
200
);
// set the scene
s.setScene(sc);
s.show();
}
public
static
void
main(String args[])
{
// launch the application
launch(args);
}
}
输出 :
注: 以上程序可能无法在联机IDE中运行,请使用脱机编译器。
参考 : https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/Button.html