071-JavaFX开发GUI图形用户界面01

JavaFX开发GUI图形用户界面01

先来写一个HelloWorld

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

public class Main extends Application {
    
    public static void main(String[] args) {
        launch(args);
    }

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

        //创建Button
        Button button = new Button();
        button.setText("Hello World");

        //创建Stack Pane
        StackPane stackPane = new StackPane();
        stackPane.getChildren().add(button);

        //创建Scene
        Scene scene = new Scene(stackPane, 300, 300);
        stage.setScene(scene);
        stage.show();
    }
}

现在给Button加一个点击事件

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class Main extends Application {

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

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

        //创建Button
        Button button = new Button();
        button.setText("Hello World");
        button.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent actionEvent) {
                System.out.println("Hello World");
            }
        });

        //创建Stack Pane
        StackPane stackPane = new StackPane();
        stackPane.getChildren().add(button);

        //创建Scene
        Scene scene = new Scene(stackPane, 300, 300);
        stage.setScene(scene);
        stage.show();
    }
}

现在我们来写一个

两个窗口之间进行跳转

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class Main extends Application {

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

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

        //创建Button1
        Button button1 = new Button();
        button1.setText("Go to the second scene.");

        //创建Button2
        Button button2 = new Button();
        button2.setText("Go to the first scene.");

        //创建pane1
        StackPane pane1 = new StackPane();
        pane1.getChildren().add(button1);

        //创建pane2
        StackPane pane2 = new StackPane();
        pane2.getChildren().add(button2);

        //创建Scene1
        Scene scene1 = new Scene(pane1, 300, 300);

        //创建Scene2
        Scene scene2 = new Scene(pane2, 600, 600);

        //设置点击1
        button1.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent actionEvent) {
                stage.setScene(scene2);
            }
        });

        //设置点击2
        button2.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent actionEvent) {
                stage.setScene(scene1);
            }
        });

        stage.setScene(scene1);
        stage.show();
    }
}

然后我们来写一个提示窗口

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class Main extends Application {

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

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

        //创建Button1
        Button button1 = new Button();
        button1.setText("Go to the second scene.");

        //创建pane1
        StackPane pane1 = new StackPane();
        pane1.getChildren().add(button1);

        //创建Scene1
        Scene scene1 = new Scene(pane1, 300, 300);

        //设置点击1
        button1.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent actionEvent) {
                boolean answer = AlertBox.display("Alert", "Are you sure?");
                System.out.println(answer);
            }
        });

        stage.setScene(scene1);
        stage.show();
    }
}
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Modality;
import javafx.stage.Stage;


public class AlertBox {

    private static boolean answer;

    public static boolean display(String title, String message) {
        Stage stage = new Stage();
        stage.initModality(Modality.APPLICATION_MODAL);
        stage.setTitle(title);
        stage.setMinWidth(300);

        VBox vBox = new VBox();
        Label label = new Label();
        label.setText(message);
        Button button1 = new Button("Yes");
        button1.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent actionEvent) {
                answer = true;
                stage.close();
            }
        });
        Button button2 = new Button("No");
        button2.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent actionEvent) {
                answer = false;
                stage.close();
            }
        });

        vBox.getChildren().addAll(label, button1, button2);
        vBox.setAlignment(Pos.CENTER);

        Scene scene = new Scene(vBox);
        stage.setScene(scene);
        stage.showAndWait();

        return answer;
    }
}

然后我们来写一个

监听关闭程序

public class Main extends Application {

    private Stage stage;

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

    @Override
    public void start(Stage stage) throws Exception {
        this.stage = stage;
        stage.setTitle("Hello World");
        stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
            @Override
            public void handle(WindowEvent windowEvent) {
                closeProgram();
            }
        });

        Button button1 = new Button();
        button1.setText("Go to the second scene.");

        StackPane pane1 = new StackPane();
        pane1.getChildren().add(button1);

        Scene scene1 = new Scene(pane1, 300, 300);

        button1.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent actionEvent) {
                closeProgram();
            }
        });

        stage.setScene(scene1);
        stage.show();
    }

    private void closeProgram() {
        System.out.println("File is saved.");
        Platform.exit();
    }
}

然后我们写一个

退出系统和提示框相结合

import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;

public class Main extends Application {

    private Stage stage;

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

    @Override
    public void start(Stage stage) throws Exception {
        this.stage = stage;
        stage.setTitle("Hello World");
        stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
            @Override
            public void handle(WindowEvent windowEvent) {
                windowEvent.consume();
                closeProgram();
            }
        });

        Button button1 = new Button();
        button1.setText("Close");
        button1.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent actionEvent) {
                closeProgram();
            }
        });

        StackPane pane1 = new StackPane();
        pane1.getChildren().add(button1);

        Scene scene1 = new Scene(pane1, 300, 300);
        stage.setScene(scene1);
        stage.show();
    }

    private void closeProgram() {
        boolean answer = AlertBox.display("Alert", "Are you sure you want to exit?");
        if (answer) {
            Platform.exit();
        }
    }
}
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Modality;
import javafx.stage.Stage;


public class AlertBox {

    private static boolean answer;

    public static boolean display(String title, String message) {
        Stage stage = new Stage();
        stage.initModality(Modality.APPLICATION_MODAL);
        stage.setTitle(title);
        stage.setMinWidth(300);

        VBox vBox = new VBox();
        Label label = new Label();
        label.setText(message);
        Button button1 = new Button("Yes");
        button1.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent actionEvent) {
                answer = true;
                stage.close();
            }
        });
        Button button2 = new Button("No");
        button2.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent actionEvent) {
                answer = false;
                stage.close();
            }
        });

        vBox.getChildren().addAll(label, button1, button2);
        vBox.setAlignment(Pos.CENTER);

        Scene scene = new Scene(vBox);
        stage.setScene(scene);
        stage.showAndWait();

        return answer;
    }
}

现在我们来写一下复杂布局

import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;

public class Main extends Application {

    private Stage stage;

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

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

        HBox topMenu = new HBox();
        Button buttonA = new Button("File");
        Button buttonB = new Button("Edit");
        Button buttonC = new Button("View");
        topMenu.getChildren().addAll(buttonA, buttonB, buttonC);

        VBox leftMenu = new VBox();
        Button button0 = new Button("Project");
        Button button1 = new Button("Structure");
        Button button2 = new Button("Message");
        leftMenu.getChildren().addAll(button0, button1, button2);

        BorderPane borderPane = new BorderPane();
        borderPane.setTop(topMenu);
        borderPane.setLeft(leftMenu);

        Scene scene = new Scene(borderPane, 300, 300);
        stage.setScene(scene);
        stage.show();
    }

    private void closeProgram() {
        boolean answer = AlertBox.display("Alert", "Are you sure you want to exit?");
        if (answer) {
            Platform.exit();
        }
    }
}

然后我们写一下GridPane

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.*;
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) throws Exception {
        this.stage = stage;
        stage.setTitle("Hello World");
        GridPane grid = new GridPane();
        grid.setPadding(new Insets(10, 10, 10, 10));
        grid.setVgap(10);
        grid.setHgap(10);

        //name label
        Label nameLabel = new Label("Username:");
        GridPane.setConstraints(nameLabel, 0, 0);

        //name input
        TextField nameInput = new TextField("Bob");
        GridPane.setConstraints(nameInput, 1, 0);

        //Password label
        Label passLabel = new Label("Password:");
        GridPane.setConstraints(passLabel, 0, 1);

        //Password input
        TextField passInput = new TextField();
        passInput.setPromptText("password");
        GridPane.setConstraints(passInput, 1, 1);

        Button loginButton = new Button("Log In");
        GridPane.setConstraints(loginButton, 1, 2);

        grid.getChildren().addAll(nameLabel, nameInput, passLabel, passInput, loginButton);

        Scene scene = new Scene(grid, 300, 300);
        stage.setScene(scene);
        stage.show();
    }

}

然后写一下

从TextField获取文本数据

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import org.w3c.dom.Text;

public class Main extends Application {

    private Stage stage;

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

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

        TextField nameInput = new TextField();
        nameInput.setPromptText("Input your age here.");

        Button button = new Button("ok");
        button.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent actionEvent) {
                isInt(nameInput);
            }
        });

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

        Scene scene = new Scene(layout, 300, 300);

        stage.setScene(scene);
        stage.show();
    }

    private boolean isInt(TextField input) {
        try {
            int age = Integer.parseInt(input.getText());
            System.out.println("User is: " + age);
            return true;
        } catch (NumberFormatException e) {
            System.out.println("Error: " + input.getText() + "is not a number.");
            return false;
        }
    }
}

CheckBox

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.layout.*;
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) throws Exception {
        this.stage = stage;
        stage.setTitle("Hello World");

        //checkboxes
        CheckBox box1 = new CheckBox("Bacon");
        CheckBox box2 = new CheckBox("Tuna");
        CheckBox box3 = new CheckBox("Chicken");
        CheckBox box4 = new CheckBox("Cheese");
        CheckBox box5 = new CheckBox("Sauce");
        box4.setSelected(true);
        box5.setSelected(true);

        //button
        Button button = new Button("Order now.");
        button.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent actionEvent) {
                String order = "";
                order = handleOption(order, box1);
                order = handleOption(order, box2);
                order = handleOption(order, box3);
                order = handleOption(order, box4);
                order = handleOption(order, box5);
                System.out.println(order);
            }
        });

        //layout
        VBox layout = new VBox(10);
        layout.setPadding(new Insets(20, 20, 20, 20));
        layout.getChildren().addAll(box1, box2, box3, box4, box5, button);

        Scene scene = new Scene(layout, 300, 300);

        stage.setScene(scene);
        stage.show();
    }

    private String handleOption(String order, CheckBox checkbox) {
        if (checkbox.isSelected()) {
            order = order + checkbox.getText() + " ";
        }
        return order;
    }
}

ChoiceBox

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ChoiceBox;
import javafx.scene.layout.*;
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) throws Exception {
        this.stage = stage;
        stage.setTitle("Hello World");

        //button
        Button button = new Button("ok");

        //choice box
        ChoiceBox<String> choiceBox = new ChoiceBox<>();
        choiceBox.getItems().add("Apple");
        choiceBox.getItems().add("Orange");
        choiceBox.getItems().add("Banana");
        choiceBox.getItems().add("Peach");

        choiceBox.setValue("Apple");

        button.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent actionEvent) {
                String value = choiceBox.getValue();
                new ToastBox().display(value);
            }
        });

        VBox vBox = new VBox();
        vBox.setPadding(new Insets(20, 20, 20, 20));
        vBox.getChildren().addAll(choiceBox, button);

        Scene scene = new Scene(vBox, 300, 300);
        stage.setScene(scene);
        stage.show();
    }

    private String handleOption(String order, CheckBox checkbox) {
        if (checkbox.isSelected()) {
            order = order + checkbox.getText() + " ";
        }
        return order;
    }
}

selectionModel

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ChoiceBox;
import javafx.scene.layout.*;
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
        Button button = new Button("ok");

        //choice box
        ChoiceBox<String> choiceBox = new ChoiceBox<>();
        choiceBox.getItems().add("Apple");
        choiceBox.getItems().add("Orange");
        choiceBox.getItems().add("Banana");
        choiceBox.getItems().add("Peach");
        choiceBox.setValue("Apple");

        choiceBox.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<String>() {
            @Override
            public void changed(ObservableValue<? extends String> observableValue, String oldStr, String newStr) {
                toast(newStr);
            }
        });

        VBox vBox = new VBox();
        vBox.setPadding(new Insets(20, 20, 20, 20));
        vBox.getChildren().addAll(choiceBox);

        Scene scene = new Scene(vBox, 300, 300);
        stage.setScene(scene);
        stage.show();
    }

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

ComboBox

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.ComboBox;
import javafx.scene.layout.*;
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
        Button button = new Button("ok");

        //combo
        ComboBox<String> comboBox = new ComboBox<>();
        comboBox.getItems().addAll("Apple", "Banana", "Grape", "Orange", "Lemon", "Peach");
        comboBox.setEditable(true);

        button.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent actionEvent) {
                String value = comboBox.getValue();
                toast(value);
            }
        });

        VBox vBox = new VBox();
        vBox.setPadding(new Insets(20, 20, 20, 20));
        vBox.getChildren().addAll(comboBox, button);

        Scene scene = new Scene(vBox, 300, 300);
        stage.setScene(scene);
        stage.show();
    }

    private void toast(String s) {
        new ToastBox().display(s);
    }
}
发布了1081 篇原创文章 · 获赞 42 · 访问量 21万+

猜你喜欢

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