使用JavaFX开发ToDoList
1. 创建JavaFX项目:打开你的 Java IDE(例如IntelliJ IDEA、Eclipse等),并创建一个新的 JavaFX 项目。
2. 设计用户界面:在Scene Builder工具中设计一个简单的用户界面。您可以包括以下组件:一个列表视图,用于显示待办事项;一个文本字段,用于输入新任务;三个按钮,用于添加、删除和清除所有任务。
3. 编写控制器类:在Java代码中编写控制器类,并将其与您的UI关联。您需要在控制器类中实现添加、删除和清除功能,并将它们与对应的按钮事件关联起来。此外,您还需要为任务列表提供模型和适配器。
4. 编译和运行:完成代码编写后,编译并运行您的应用程序。
在这个示例代码中,我们使用了JavaFX的FXML库和注释来定义用户界面和控制器类。initialize()方法用于初始化任务列表视图handleAddButtonAction()、handleDeleteButtonAction()和handleClearButtonAction()分别处理添加、删除和清除按钮的事件。
package sample;import javafx.collections.FXCollections;import javafx.collections.ObservableList;import javafx.fxml.FXML;import javafx.geometry.Pos;import javafx.scene.control.ListView;import javafx.scene.control.TextField;import javafx.scene.input.MouseEvent;import javafx.scene.layout.GridPane;import javafx.scene.layout.HBox;import sample.control.Card;import sample.control.LabelComb;//控制类public class Controller { @FXML private GridPane gridPane; @FXML private ListView<HBox> todoListView; @FXML private TextField newTodoTextField; public void initialize() { // Initialize the list view with some example tasks HBox card1 = getHBox("Task 1"); HBox card2 = getHBox("Task 2"); HBox card3 = getHBox("Task 3"); ObservableList<HBox> tasks = FXCollections.observableArrayList( card1, card2, card3); todoListView.setItems(tasks); } @FXML private void handleAddButtonAction(MouseEvent event) { String newTask = newTodoTextField.getText(); HBox card = getHBox(newTask); if (!newTask.isEmpty()) { todoListView.getItems().add(card); newTodoTextField.clear(); } } private HBox getHBox(String newTask) { double width = todoListView.getPrefWidth(); LabelComb labelComb = new LabelComb().init(100,newTask,"Green",Card.BASE_STYLE,null); return Card.initCard(width,20, Pos.BASELINE_CENTER,labelComb); } @FXML private void handleDeleteButtonAction(MouseEvent event) { int selectedIndex = todoListView.getSelectionModel().getSelectedIndex(); if (selectedIndex >= 0) { todoListView.getItems().remove(selectedIndex); } } @FXML private void handleClearButtonAction(MouseEvent event) { todoListView.getItems().clear(); } @FXML public void handleCloseButtonAction(MouseEvent event) { Main.mainView.hide(); }}//启动类package sample;import javafx.application.Application;import javafx.fxml.FXMLLoader;import javafx.scene.Parent;import javafx.scene.Scene;import javafx.scene.input.MouseEvent;import javafx.stage.Stage;import javafx.stage.StageStyle;import sample.util.MySystemTray;public class Main extends Application { public static Stage mainView; private double dragOffsetX; private double dragOffsetY; @Override public void start(Stage primaryStage) throws Exception{ mainView = primaryStage; Parent root = FXMLLoader.load(getClass().getResource("sample.fxml")); primaryStage.setTitle("ToDo List"); Scene scene = new Scene(root, 600, 530); primaryStage.initStyle(StageStyle.TRANSPARENT); scene.setFill(null); scene.setOnMousePressed((ev) -> handleMousePressed(ev)); scene.setOnMouseDragged(e -> handleMouseDragged(e)); primaryStage.setScene(scene); MySystemTray.init(primaryStage); primaryStage.show(); } protected void handleMousePressed(MouseEvent e) { // Store the mouse x and y coordinates with respect to the // stage in the reference variables to use them in the drag event this.dragOffsetX = e.getScreenX() - mainView.getX(); this.dragOffsetY = e.getScreenY() - mainView.getY(); } protected void handleMouseDragged(MouseEvent e) { // Move the stage by the drag amount mainView.setX(e.getScreenX() - this.dragOffsetX); mainView.setY(e.getScreenY() - this.dragOffsetY); } public static void main(String[] args) { launch(args); }}//使用fxml定义软件界面<?xml version="1.0" encoding="UTF-8"?><?import javafx.geometry.Insets?><?import javafx.scene.control.Button?><?import javafx.scene.control.Label?><?import javafx.scene.control.ListView?><?import javafx.scene.control.TextField?><?import javafx.scene.layout.GridPane?><GridPane fx:id="gridPane" id="mainView" alignment="BASELINE_CENTER" hgap="10" stylesheets="@mainpage.css" vgap="10" xmlns="http://javafx.com/javafx/8.0.172-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller"> <padding> <Insets bottom="15" top="10" /> </padding> <Label id="icon" minHeight="-Infinity" minWidth="-Infinity" prefHeight="20.0" prefWidth="20.0" GridPane.columnIndex="0" GridPane.columnSpan="1" GridPane.halignment="LEFT" GridPane.rowIndex="0" /> <Label id="title" prefWidth="400" GridPane.columnIndex="0" GridPane.columnSpan="1" GridPane.halignment="LEFT" GridPane.rowIndex="0"> <padding> <Insets left="25" /> </padding> ToDo List</Label> <Button fx:id="closeWin" onMouseClicked="#handleCloseButtonAction" text="X" GridPane.columnIndex="1" GridPane.halignment="RIGHT" GridPane.rowIndex="0" /> <TextField fx:id="newTodoTextField" prefWidth="460" GridPane.columnIndex="0" GridPane.rowIndex="1" /> <Button fx:id="add" onMouseClicked="#handleAddButtonAction" prefWidth="80.0" GridPane.columnIndex="1" GridPane.columnSpan="1" GridPane.rowIndex="1">Add</Button> <ListView fx:id="todoListView" prefWidth="460" GridPane.columnIndex="0" GridPane.columnSpan="2" GridPane.rowIndex="2" /> <Button fx:id="clear" onMouseClicked="#handleClearButtonAction" prefHeight="7.0" prefWidth="80.0" GridPane.columnIndex="0" GridPane.halignment="LEFT" GridPane.rowIndex="3">Clear</Button> <Button fx:id="delete" onMouseClicked="#handleDeleteButtonAction" prefWidth="80.0" GridPane.columnIndex="1" GridPane.halignment="CENTER" GridPane.rowIndex="3">Delete</Button></GridPane>
本程序通过JavaFX开发无其他依赖,JavaFX程序现在也可以开发出比较好看的界面了。
使用idea的Build Artifacts打包。
1. 在Project Structure中配置Artifacts。
在弹出的窗口中左侧选中"Artifacts",点击"+"选择jar,然后选择"from modules with dependencies"。默认的设置必须选择项目的根目录,否则无法运行
2. Build Artifacts打包并执行。
Tips
以上就是一个简单的JavaFX ToDoList应用程序开发过程,如果需要源码加公众号,回复 ToDo获取。建议使用JavaFX8进行开发,因为之后JavaFX库从jdk版本中分离出来了,需要单独下载。如果发现问题,欢迎私信与我交流。其他关于JavaFX的问题也可以在私信中提出。如果这篇博客有帮到您,不要忘记点一个赞。