龙空技术网

简易的TO-DO

aaliss 104

前言:

今天我们对“javafx边框”大致比较关注,姐妹们都需要了解一些“javafx边框”的相关文章。那么小编在网摘上汇集了一些对于“javafx边框””的相关内容,希望我们能喜欢,咱们快快来学习一下吧!

HBox

HBox布局窗格提供了一种简单的方法,用于在单行中排列一系列节点。例如:

创建一个HBox Pane

public HBox addHBox() {    HBox hbox = new HBox();    hbox.setPadding(new Insets(15, 12, 15, 12));    hbox.setSpacing(10);    hbox.setStyle("-fx-background-color: #336699;");    Button buttonCurrent = new Button("Current");    buttonCurrent.setPrefSize(100, 20);    Button buttonProjected = new Button("Projected");    buttonProjected.setPrefSize(100, 20);    hbox.getChildren().addAll(buttonCurrent, buttonProjected);    return hbox;}
BorderPane

BorderPane布局窗格提供了五个区域,用于放置节点:顶部,底部,左,右和中心。下图显示了使用边框窗格创建的布局类型。这些区域可以是任何尺寸。如果您的应用程序不需要其中一个区域,则无需定义它,并且没有为其分配空格。

创建一个BorderPane

BorderPane border = new BorderPane();HBox hbox = addHBox()border.setTop(hbox);border.setLeft(addVBox());addStackPane(hbox);         // Add stack to HBox in top regionborder.setCenter(addGridPane());border.setRight(addFlowPane());
列表显示&操作

ListView类表示可滚动的项目列表。下图显示了酒店预订系统中可用的住宿类型列表。

您可以通过使用setItems方法定义其项目来填充列表。您还可以通过应用setCellFactory方法为列表中的项目创建视图。

创建一个 ListView

ListView<String> list = new ListView<>();ObservableList<String> items =FXCollections.observableArrayList (    "Single", "Double", "Suite", "Family App");list.setItems(items);
To-Do 案例
package io.weijunfu.javafx;import javafx.application.Application;import javafx.beans.value.ChangeListener;import javafx.beans.value.ObservableValue;import javafx.collections.FXCollections;import javafx.collections.ObservableList;import javafx.geometry.Pos;import javafx.scene.Scene;import javafx.scene.control.*;import javafx.scene.input.KeyCode;import javafx.scene.layout.BorderPane;import javafx.scene.layout.HBox;import javafx.scene.layout.Priority;import javafx.stage.Stage;import javafx.util.Callback;import java.util.Iterator;import java.util.Random;/** * TO-DO * * @Author: weijunfu<ijunfu @ 1 6 3 . com> * @Date: 2021-11-12 14:57 * @Version: 1.0.0 */public class ToDoApplication extends Application {    private ListView<ToDo> listView = new ListView<>();    private ObservableList<ToDo> list = FXCollections.observableArrayList();    private TextField textField = new TextField();    private Random random = new Random();    /**     * 生成 ID     * @Author: weijunfu<ijunfu@163.com>     * @Version: 1.0.0     * @Date: 2021/11/12 17:45      * @param     * @Return: java.lang.String     */    private String genNO() {        return "No." + System.currentTimeMillis() + (random.nextInt(899) + 100);    }    @Override    public void start(Stage stage) throws Exception {        BorderPane rootPane = new BorderPane();        HBox topPane = new HBox();        topPane.setAlignment(Pos.CENTER);        topPane.setPrefHeight(50);        // 输入框        textField.setPrefWidth(200);        textField.setPrefHeight(32);        HBox.setHgrow(textField, Priority.ALWAYS);        textField.setOnKeyReleased(keyEvent -> {    // 键盘事件            KeyCode code = keyEvent.getCode();            if(code.equals(KeyCode.ENTER)) {                ToDo toDo = new ToDo();                toDo.setContent(textField.getText());                toDo.setNo(genNO());                list.add(toDo);                textField.clear();            }        });        // 添加 按钮        Button addButton = new Button("添加");        addButton.setPrefWidth(60);        addButton.setPrefHeight(32);        addButton.setOnAction(actionEvent -> {  // 点击事件            if(textField.getText().isEmpty()) return;            ToDo toDo = new ToDo();            toDo.setContent(textField.getText());            toDo.setNo(genNO());            list.add(toDo);            textField.clear();        });        // 删除 按钮        Button deleteButton = new Button("删除");        deleteButton.setPrefWidth(60);        deleteButton.setPrefHeight(32);        deleteButton.setOnAction(event -> { // 删除 当前选择的列表项            int selectedIndex = listView.getSelectionModel().getSelectedIndex();            System.out.println(selectedIndex);            if(selectedIndex >= 0) list.remove(selectedIndex);        });        topPane.getChildren().addAll(textField, addButton, deleteButton);        rootPane.setTop(topPane);        // 初始化数据        list.add(new ToDo(genNO(), "早上吃两个包子,喝一杯豆浆"));        list.add(new ToDo(genNO(), "中午吃一碗水饺,一份蔬菜沙拉"));        list.add(new ToDo(genNO(), "晚上吃一碗牛肉面"));        listView.setItems(list);        // 列表显示数据处理        listView.setCellFactory(new Callback<ListView<ToDo>, ListCell<ToDo>>() {            @Override            public ListCell<ToDo> call(ListView<ToDo> param) {                return new ListCell<ToDo>(){                    @Override                    protected void updateItem(ToDo item, boolean empty) {                        super.updateItem(item, empty);                        if(null == item) {                            this.setGraphic(null);                            return ;                        }                      // 添加复选框                        CheckBox checkBox = new CheckBox();                        checkBox.setText(item.toString());                        checkBox.setSelected(false);                        this.setGraphic(checkBox);                      	// 复选框事件                        checkBox.selectedProperty().addListener(new MyCheckBoxListener(item, checkBox));                    }                };            }        });//        rootPane.setCenter(listView);        Scene scene = new Scene(rootPane, 480, 320);        stage.setScene(scene);        stage.setTitle("To-Do");        stage.show();    }    // 复选框事件处理类    class MyCheckBoxListener implements ChangeListener<Boolean> {        private ToDo toDo;        private CheckBox checkBox;        public MyCheckBoxListener(ToDo toDo, CheckBox checkBox) {            this.toDo = toDo;            this.checkBox = checkBox;        }        @Override        public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {            if(newValue) {                checkBox.setStyle("-fx-background-color: rgba(0, 0, 0, .125); ");            } else {                checkBox.setStyle("-fx-background-color: white");            }        }    }    class ToDo {        private String no;        private String content;        private boolean status;        public ToDo() {        }        public ToDo(String no, String content) {            this.no = no;            this.content = content;            this.status = false;        }        public String getNo() {            return no;        }        public void setNo(String no) {            this.no = no;        }        public String getContent() {            return content;        }        public void setContent(String content) {            this.content = content;        }        public boolean isStatus() {            return status;        }        public void setStatus(boolean status) {            this.status = status;        }        @Override        public String toString() {            return no + "\t\t" + content;        }    }    public static void main(String[] args) {        launch(args);    }}

标签: #javafx边框