javafx02 Button

Button

package org.example;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.*;
import javafx.scene.paint.Paint;
import javafx.scene.text.Font;
import javafx.stage.Stage;

public class Launch7  extends Application {
    public void start(Stage primaryStage) throws Exception {
        Button b1=new Button();
        b1.setText("按钮1");

        b1.setLayoutX(100);
        b1.setLayoutY(100);
        b1.setPrefWidth(200);
        b1.setPrefHeight(200);
//        设置按钮的字体
        b1.setFont(Font.font("仿宋",22));
        b1.setTextFill(Paint.valueOf("#AFFFFF"));
//      设置背景   Paint.valueOf("#8FBC8F15") 后面两位代表透明度
        BackgroundFill bgf=new BackgroundFill(Paint.valueOf("#8FBC8F15"),new CornerRadii(20),new Insets(10));
        Background background = new Background(bgf);
        b1.setBackground(background);
//        设置按钮边框
        BorderStroke borderStroke = new BorderStroke(Paint.valueOf("#8F8F8F"),BorderStrokeStyle.SOLID,new CornerRadii(10),new BorderWidths(10));
        Border border=new Border(borderStroke);
        b1.setBorder(border);
//      设置样式
        b1.setStyle("-fx-background-color: #EE1213");
//        设置监听器 点击事件
        b1.setOnAction(new EventHandler<ActionEvent>() {
            public void handle(ActionEvent event) {
//                event就是对应的事件的组件
                Button button = (Button) event.getSource();
                System.out.println("hello:"+button.getText());
            }
        });


        Group root=new Group();
        root.getChildren().add(b1);
        Scene scene = new Scene(root);
        primaryStage.setScene(scene);
        primaryStage.setTitle("My Demo");
        primaryStage.setHeight(800);
        primaryStage.setWidth(800);
        primaryStage.show();
    }
}

你可能感兴趣的:(javafx,java,前端,javascript)