073-JavaFX开发GUI图形用户界面03

073-JavaFX开发GUI图形用户界面03

JavaFX样式

import javafx.application.Application;
import javafx.beans.property.ReadOnlyObjectProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class Main extends Application {

    private Stage stage;

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

    @Override
    public void start(Stage stage) {
        this.stage = stage;
        stage.setTitle("Hello World");

        Button apple = new Button("Apple");
        Button banana = new Button("Banana");
        Button grape = new Button("Grape");

        //layout
        VBox vBox = new VBox();
        vBox.setSpacing(10);
        vBox.setPadding(new Insets(10, 10, 10, 10));
        vBox.getChildren().addAll(apple, banana, grape);

        Scene scene = new Scene(vBox, 700, 500);
        scene.getStylesheets().add("style.css");
        stage.setScene(scene);
        stage.show();
    }

    private void toast(String s) {
        new ToastBox().display(s);
    }
}

加个CSS文件

.button{
    -fx-background-color: red;
}

自定义style

import javafx.application.Application;
import javafx.beans.property.ReadOnlyObjectProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class Main extends Application {

    private Stage stage;

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

    @Override
    public void start(Stage stage) {
        this.stage = stage;
        stage.setTitle("Hello World");

        Button apple = new Button("Apple");
        Button banana = new Button("Banana");
        Button grape = new Button("Grape");

        apple.getStyleClass().add("button-red");
        banana.getStyleClass().add("button-blue");
        grape.getStyleClass().add("button-black");

        //layout
        VBox vBox = new VBox();
        vBox.setSpacing(10);
        vBox.setPadding(new Insets(10, 10, 10, 10));
        vBox.getChildren().addAll(apple, banana, grape);

        Scene scene = new Scene(vBox, 700, 500);
        scene.getStylesheets().add("style.css");
        stage.setScene(scene);
        stage.show();
    }

    private void toast(String s) {
        new ToastBox().display(s);
    }
}

加上CSS

.button-red{
    -fx-background-color: red;
    -fx-text-fill: white;
}

.button-blue {
    -fx-background-color: blue;
    -fx-text-fill: white;
}

.button-black {
    -fx-background-color: black;
    -fx-text-fill: white;
}

id选择器

import javafx.application.Application;
import javafx.beans.property.ReadOnlyObjectProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class Main extends Application {

    private Stage stage;

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

    @Override
    public void start(Stage stage) {
        this.stage = stage;
        stage.setTitle("Hello World");

        Button apple = new Button("Apple");
        Button banana = new Button("Banana");
        Button grape = new Button("Grape");

        apple.setId("button-apple");
        banana.setId("button-banana");
        grape.setId("button-grape");

        //layout
        VBox vBox = new VBox();
        vBox.setSpacing(10);
        vBox.setPadding(new Insets(10, 10, 10, 10));
        vBox.getChildren().addAll(apple, banana, grape);

        Scene scene = new Scene(vBox, 700, 500);
        scene.getStylesheets().add("style.css");
        stage.setScene(scene);
        stage.show();
    }

    private void toast(String s) {
        new ToastBox().display(s);
    }
}

加上css

#button-apple {
    -fx-background-color: red;
    -fx-text-fill: white;
}

#button-banana {
    -fx-background-color: blue;
    -fx-text-fill: white;
}

#button-grape {
    -fx-background-color: black;
    -fx-text-fill: white;
}

properties

import javafx.application.Application;
import javafx.beans.property.ReadOnlyObjectProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import java.beans.PropertyChangeListener;
import java.net.http.WebSocket;

public class Main extends Application {

    private Stage stage;

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

    @Override
    public void start(Stage stage) {
        this.stage = stage;
        stage.setTitle("Hello World");

        Person person = new Person();
        person.setName("Alice");

        person.nameProperty().addListener(new ChangeListener<String>() {
            @Override
            public void changed(ObservableValue<? extends String> observableValue, String oldValue, String newValue) {
                toast("The old name is: " + oldValue + "\nThe new name is: " + newValue);
            }
        });

        Button button = new Button("Change Name");
        button.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent actionEvent) {
                person.setName("Bob");
            }
        });

        //layout
        VBox vBox = new VBox();
        vBox.setSpacing(10);
        vBox.setPadding(new Insets(10, 10, 10, 10));
        vBox.getChildren().addAll(button);

        Scene scene = new Scene(vBox, 700, 500);
        scene.getStylesheets().add("style.css");
        stage.setScene(scene);
        stage.show();
    }

    private void toast(String s) {
        new ToastBox().display(s);
    }
}

Person

import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;

public class Person {
    private StringProperty name = new SimpleStringProperty();

    public String getName() {
        return name.get();
    }

    public StringProperty nameProperty() {
        return name;
    }

    public void setName(String name) {
        this.name.set(name);
    }
}
发布了1081 篇原创文章 · 获赞 42 · 访问量 21万+

猜你喜欢

转载自blog.csdn.net/qq_33781658/article/details/104936187