072-JavaFX开发GUI图形用户界面02

ListView

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
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.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");

        //list view
        ListView<String> listView = new ListView<>();
        listView.getItems().addAll("apple", "orange", "banana", "peach", "grape", "lemon");
        listView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);

        button.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent actionEvent) {
                ObservableList<String> items = listView.getSelectionModel().getSelectedItems();
                String str = "";
                for (String s : items) {
                    str = str + s + " ";
                }
                toast(str);
            }
        });

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

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

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

TreeView

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
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.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");

        //tree
        TreeItem<String> creature = new TreeItem<>("creature");
        TreeItem<String> animal = makeBranch(creature, "animal");
        TreeItem<String> plant = makeBranch(creature, "plant");

        TreeItem<String> sea = makeBranch(animal, "sea");
        TreeItem<String> land = makeBranch(animal, "land");
        makeBranch(sea, "fish");
        makeBranch(sea, "crab");
        makeBranch(land, "lion");
        makeBranch(land, "tiger");

        TreeItem<String> vegetables = makeBranch(plant, "vegetables");
        TreeItem<String> fruits = makeBranch(plant, "fruits");
        makeBranch(vegetables, "potato");
        makeBranch(vegetables, "cabbage");
        makeBranch(fruits, "lemon");
        makeBranch(fruits, "orange");

        TreeView<String> treeView = new TreeView<>(creature);
        treeView.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<TreeItem<String>>() {
            @Override
            public void changed(ObservableValue<? extends TreeItem<String>> observableValue, TreeItem<String> oldValue, TreeItem<String> newValue) {
                toast(newValue.getValue());
            }
        });

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

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

    private TreeItem<String> makeBranch(TreeItem<String> parent, String title) {
        TreeItem<String> item = new TreeItem<>(title);
        parent.getChildren().add(item);
        return item;
    }

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

TableView

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class Test08_TableView 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");

        //name column
        TableColumn<Product, String> nameColumn = new TableColumn<>("Name");
        nameColumn.setMinWidth(200);
        nameColumn.setCellValueFactory(new PropertyValueFactory<>("Name"));

        //price column
        TableColumn<Product, String> priceColumn = new TableColumn<>("Price");
        priceColumn.setMinWidth(100);
        priceColumn.setCellValueFactory(new PropertyValueFactory<>("Price"));

        //amount column
        TableColumn<Product, String> amountColumn = new TableColumn<>("Amount");
        amountColumn.setMinWidth(100);
        amountColumn.setCellValueFactory(new PropertyValueFactory<>("Amount"));

        //table
        TableView<Product> tableView = new TableView<>();
        tableView.setItems(getProducts());
        tableView.getColumns().addAll(nameColumn, priceColumn, amountColumn);

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

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

    private ObservableList<Product> getProducts() {
        ObservableList<Product> products = FXCollections.observableArrayList();
        products.add(new Product("Apple", 5, 10));
        products.add(new Product("Orange", 5, 10));
        products.add(new Product("Lemon", 5, 10));
        products.add(new Product("Banana", 5, 10));
        products.add(new Product("Grape", 5, 10));
        products.add(new Product("Peach", 5, 10));
        return products;
    }

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

TableView Editable

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.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
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");

        //name column
        TableColumn<Product, String> nameColumn = new TableColumn<>("Name");
        nameColumn.setMinWidth(200);
        nameColumn.setCellValueFactory(new PropertyValueFactory<>("Name"));

        //price column
        TableColumn<Product, String> priceColumn = new TableColumn<>("Price");
        priceColumn.setMinWidth(200);
        priceColumn.setCellValueFactory(new PropertyValueFactory<>("Price"));

        //amount column
        TableColumn<Product, String> amountColumn = new TableColumn<>("Amount");
        amountColumn.setMinWidth(200);
        amountColumn.setCellValueFactory(new PropertyValueFactory<>("Amount"));

        //table
        TableView<Product> tableView = new TableView<>();
        tableView.setItems(getProducts());
        tableView.getColumns().addAll(nameColumn, priceColumn, amountColumn);

        TextField nameInput = new TextField();
        nameInput.setPromptText("Name");

        TextField priceInput = new TextField();
        priceInput.setPromptText("Price");

        TextField amountInput = new TextField();
        amountInput.setPromptText("Amount");

        Button add = new Button("add");
        add.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent actionEvent) {
                Product product = new Product();
                product.setName(nameInput.getText());
                product.setPrice(Double.parseDouble(priceInput.getText()));
                product.setAmount(Integer.parseInt(amountInput.getText()));
                tableView.getItems().add(product);
                nameInput.clear();
                priceInput.clear();
                amountInput.clear();
            }
        });

        Button delete = new Button("delete");
        delete.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent actionEvent) {
                ObservableList<Product> items = tableView.getItems();
                ObservableList<Product> selectedItems = tableView.getSelectionModel().getSelectedItems();
                selectedItems.forEach(items::remove);
            }
        });

        HBox hBox = new HBox();
        hBox.setSpacing(10);
        hBox.setPadding(new Insets(10, 10, 10, 10));
        hBox.getChildren().addAll(nameInput, priceInput, amountInput, add, delete);

        VBox vBox = new VBox();
        vBox.setPadding(new Insets(0, 0, 0, 0));
        vBox.getChildren().addAll(tableView, hBox);

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

    private ObservableList<Product> getProducts() {
        ObservableList<Product> products = FXCollections.observableArrayList();
        products.add(new Product("Apple", 5, 10));
        products.add(new Product("Orange", 5, 10));
        products.add(new Product("Lemon", 5, 10));
        products.add(new Product("Banana", 5, 10));
        products.add(new Product("Grape", 5, 10));
        products.add(new Product("Peach", 5, 10));
        return products;
    }

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

Menu

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");

        //menu
        Menu file = new Menu("File");
        Menu edit = new Menu("Edit");
        Menu help = new Menu("Help");

        //menu items
        file.getItems().add(new MenuItem("New Project..."));
        file.getItems().add(new MenuItem("New Project..."));
        file.getItems().add(new SeparatorMenuItem());
        file.getItems().add(new MenuItem("New Module..."));
        file.getItems().add(new MenuItem("New Module..."));
        file.getItems().add(new SeparatorMenuItem());
        file.getItems().add(new MenuItem("Import Project..."));
        file.getItems().add(new MenuItem("Import Project..."));

        edit.getItems().add(new CheckMenuItem("Show Line Numbers"));
        MenuItem menuItem = new MenuItem("Show Alert");
        menuItem.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent actionEvent) {
                toast("Hello World");
            }
        });
        help.getItems().add(menuItem);

        //main menu bar
        MenuBar bar = new MenuBar();
        bar.getMenus().addAll(file);
        bar.getMenus().addAll(edit);
        bar.getMenus().addAll(help);

        //border pane
        BorderPane pane = new BorderPane();
        pane.setTop(bar);

        Scene scene = new Scene(pane, 700, 500);
        stage.setScene(scene);
        stage.show();
    }

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

RadioMenuItem

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");

        //menu
        Menu file = new Menu("File");
        Menu edit = new Menu("Edit");
        Menu help = new Menu("Help");
        Menu about = new Menu("About");

        //menu items
        file.getItems().add(new MenuItem("New Project..."));
        file.getItems().add(new MenuItem("New Project..."));
        file.getItems().add(new SeparatorMenuItem());
        file.getItems().add(new MenuItem("New Module..."));
        file.getItems().add(new MenuItem("New Module..."));
        file.getItems().add(new SeparatorMenuItem());
        file.getItems().add(new MenuItem("Import Project..."));
        file.getItems().add(new MenuItem("Import Project..."));

        edit.getItems().add(new CheckMenuItem("Show Line Numbers"));
        MenuItem menuItem = new MenuItem("Show Alert");
        menuItem.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent actionEvent) {
                toast("Hello World");
            }
        });
        help.getItems().add(menuItem);

        ToggleGroup toggleGroup = new ToggleGroup();
        RadioMenuItem aaa = new RadioMenuItem("AAA");
        RadioMenuItem bbb = new RadioMenuItem("BBB");
        RadioMenuItem ccc = new RadioMenuItem("CCC");

        aaa.setToggleGroup(toggleGroup);
        bbb.setToggleGroup(toggleGroup);
        ccc.setToggleGroup(toggleGroup);

        about.getItems().addAll(aaa, bbb, ccc);

        //main menu bar
        MenuBar bar = new MenuBar();
        bar.getMenus().addAll(file);
        bar.getMenus().addAll(edit);
        bar.getMenus().addAll(help);
        bar.getMenus().addAll(about);

        //border pane
        BorderPane pane = new BorderPane();
        pane.setTop(bar);

        Scene scene = new Scene(pane, 700, 500);
        stage.setScene(scene);
        stage.show();
    }

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

猜你喜欢

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