📜  JavaFX |带有示例的按钮

📅  最后修改于: 2022-05-13 01:54:47.872000             🧑  作者: Mango

JavaFX |带有示例的按钮

Button 类是 JavaFX 包的一部分,它可以有文本或图形或两者兼有。

JavaFX 中的按钮可以是三种不同的类型:

  1. 普通按钮:普通按钮
  2. 默认按钮:接收键盘 VK_ENTER 按下的默认按钮
  3. 取消按钮:接收键盘 VK_ENTER 按下的取消按钮

当按下按钮时,会发送一个动作事件。此 Action Event 可以由 EventHandler 管理。按钮也可以通过实现一个 EventHandler 来处理鼠标事件来响应鼠标事件。

Button 类的构造函数是

  1. Button() :创建一个带有空字符串作为其标签的按钮。
  2. Button(String t) : 创建一个以指定文本为标签的按钮。
  3. Button(String t, Node g) : 创建一个带有指定文本和图标作为其标签的按钮。

常用方法

methodexplanation
setCancelButton(boolean v)Sets the value of the property cancelButton.
setDefaultButton(boolean v)Sets the value of the property defaultButton
isDefaultButton()Gets the value of the property defaultButton.
isCancelButton()Gets the value of the property cancelButton.
cancelButtonProperty()A Cancel Button is the button that receives a keyboard VK_ESC press
defaultButtonProperty()A default Button is the button that receives a keyboard VK_ENTER press
createDefaultSkin()Create a new instance of the default skin for this control.

下面的程序说明了 Button 在 JavaFX 中的使用。

  1. 创建一个按钮并将其添加到舞台的程序:该程序创建一个由名称b指示的 Button。该按钮将在场景内创建,而场景又将托管在舞台内。函数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);
        }
    }
    

    输出

  2. 用于创建按钮并向其添加事件处理程序的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 event = new EventHandler() {
                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);
        }
    }
    

    输出

  3. 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 event = new EventHandler() {
                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);
        }
    }
    

    输出

  4. 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 event = new EventHandler() {
                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);
        }
    }
    

    输出

  5. 创建默认按钮和取消按钮的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 event = new EventHandler() {
                public void handle(ActionEvent e)
                {
                    l.setText("  cancel  button    selected    ");
                }
            };
            EventHandler event1 = new EventHandler() {
                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