前军教程网

中小站长与DIV+CSS网页布局开发技术人员的首选CSS学习平台

JavaFX学习笔记008(按钮)(javafx学习)

按钮介绍

JavaFX中的按钮控件是构建交互式用户界面的重要组件之一。按钮不仅能够响应用户的点击事件,还可以通过样式和属性进行定制。本文将深入介绍JavaFX中按钮的使用,以及一些常用的属性和事件。

创建基本按钮

在JavaFX中创建按钮非常简单。以下是一个基本按钮的示例代码:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;

public class BasicButtonExample extends Application {

    @Override
    public void start(Stage primaryStage) {
        // 创建按钮
        Button button = new Button("点击我");

        // 创建场景
        Scene scene = new Scene(button, 200, 100);

        // 设置舞台
        primaryStage.setTitle("基本按钮示例");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}


按钮样式

JavaFX允许通过CSS样式为按钮定制外观。以下示例演示如何为按钮应用样式:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;

public class StyledButtonExample extends Application {

    @Override
    public void start(Stage primaryStage) {
        // 创建按钮
        Button button = new Button("样式按钮");

        // 应用CSS样式
      // 设置了背景色 字体颜色
        button.setStyle("-fx-background-color: #4CAF50; -fx-text-fill: white;");

        // 创建场景
        Scene scene = new Scene(button, 200, 100);

        // 设置舞台
        primaryStage.setTitle("样式按钮示例");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}


按钮事件

按钮最强大的特性之一是能够响应用户的交互事件。以下是一个简单的示例,演示按钮点击事件的处理:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;

public class ButtonEventExample extends Application {

    @Override
    public void start(Stage primaryStage) {
        // 创建按钮
        Button button = new Button("点击我");

        // 处理按钮点击事件
        button.setOnAction(e -> {
            System.out.println("按钮被点击了!");
        });

        // 创建场景
        Scene scene = new Scene(button, 200, 100);

        // 设置舞台
        primaryStage.setTitle("按钮事件示例");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}


图形按钮

按钮不仅可以包含文本,还可以包含图形。以下示例演示如何创建一个包含图标的按钮:


import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.stage.Stage;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

public class IconButtonExample extends Application {

    @Override
    public void start(Stage primaryStage) throws FileNotFoundException {
        // 创建图标按钮
        Button button = new Button();
        File file = new File("C:\\Users\\Lenovo\\Desktop\\icon.png");
        FileInputStream inputStream = new FileInputStream(file);
        Image image = new Image(inputStream);
        button.setGraphic(new ImageView(image));

        // 创建场景
        Scene scene = new Scene(button, 100, 100);

        // 设置舞台
        primaryStage.setTitle("图标按钮示例");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}


发表评论:

控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言