因此,我已经使用Javafx和Ms Access创建了一个数据输入应用程序,但是我想添加一个我不知道如何实现的功能。我希望我的应用程序只运行6天,然后我的所有按钮和文本字段都将被禁用。
当我单击“开始”按钮时,它将 setdisable(false)所有我的按钮和文本字段,并将永远开始日期计数和 setdisable(true)“开始”按钮。我想将此信息存储在一些隐藏的.txt文件中,以便在应用程序关闭时记住启动按钮的状态和日期,并显示日期。在标签中。在第六天结束时,我所有的按钮和文本字段将被永久禁用。
这是我的主班:
package application;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
public class DataEntryMain extends Application {
@Override
public void start(Stage primaryStage) {
try {
Parent root = FXMLLoader.load(getClass().getResource("/application/UI.fxml"));
Scene scene = new Scene(root,1280,700);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
这是我的Controller类:
package application;
import java.sql.SQLException;
import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXTextField;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
public class DataEntryController {
int rn;
@FXML
private JFXTextField t1, t2, t3, t4 , t5, t6, t7, t8, t9, t10, t11, t12, t13, t14, t15;
@FXML
private JFXButton start;
@FXML
private JFXButton lock;
@FXML
private JFXButton save;
@FXML
private JFXButton update;
@FXML
private Label resultConsole;
@FXML
private JFXTextField searchRowNo;
@FXML
private TableView<DataEntry> tabView;
@FXML
private TableColumn<DataEntry, Integer> rowNumber;
@FXML
private TableColumn<DataEntry, String> c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12, c13, c14, c15;
@FXML
private void insertData(ActionEvent event) throws ClassNotFoundException, SQLException{
try {
DataEntryModel.insertData(t1.getText(), t2.getText(), t3.getText(), t4.getText(), t5.getText(), t6.getText(), t7.getText(), t8.getText(), t9.getText(), t10.getText(), t11.getText(), t12.getText(), t13.getText(), t14.getText(), t15.getText());
resultConsole.setText("Data Added Successfully!");
}
catch(SQLException e) {
System.out.println("Exception Occer In Insertion "+e);
e.printStackTrace();
throw e;
}
}
@FXML
private void updateData(ActionEvent event) throws ClassNotFoundException, SQLException{
rn = Integer.parseInt(searchRowNo.getText());
try {
DataEntryModel.updateData(rn, t1.getText(), t2.getText(), t3.getText(), t4.getText(), t5.getText(), t6.getText(), t7.getText(), t8.getText(), t9.getText(), t10.getText(), t11.getText(), t12.getText(), t13.getText(), t14.getText(), t15.getText());
resultConsole.setText("Data Updated Successfully!");
}
catch(SQLException e) {
System.out.println("Exception Occure In Update "+e);
throw e;
}
}
@FXML
private void intialize()throws Exception{
rowNumber.setCellValueFactory(cellData -> cellData.getValue().getRNo().asObject());
c1.setCellValueFactory(cellData -> cellData.getValue().getText1());
c2.setCellValueFactory(cellData -> cellData.getValue().getText2());
c3.setCellValueFactory(cellData -> cellData.getValue().getText3());
c4.setCellValueFactory(cellData -> cellData.getValue().getText4());
c5.setCellValueFactory(cellData -> cellData.getValue().getText5());
c6.setCellValueFactory(cellData -> cellData.getValue().getText6());
c7.setCellValueFactory(cellData -> cellData.getValue().getText7());
c8.setCellValueFactory(cellData -> cellData.getValue().getText8());
c9.setCellValueFactory(cellData -> cellData.getValue().getText9());
c10.setCellValueFactory(cellData -> cellData.getValue().getText10());
c11.setCellValueFactory(cellData -> cellData.getValue().getText11());
c12.setCellValueFactory(cellData -> cellData.getValue().getText12());
c13.setCellValueFactory(cellData -> cellData.getValue().getText13());
c14.setCellValueFactory(cellData -> cellData.getValue().getText14());
c15.setCellValueFactory(cellData -> cellData.getValue().getText15());
ObservableList<DataEntry> deList = DataEntryModel.getAllRecords();
populateTable(deList);
}
private void populateTable(ObservableList<DataEntry> deList) {
tabView.setItems(deList);
}
}
我还有另外3个用于数据库操作的类,我认为这与这里无关。This is how the application looks
答案 0 :(得分:0)
在其中使用TimeUnit.DAYS.sleep(days);
作为单独的线程,然后运行关闭输入的事物。您可以禁用整个窗格,不需要单个按钮,也可以创建一个堆栈窗格,在所有内容的顶部都显示一条消息,并在x天后显示。您还需要使用Platform.runLater
与其他线程的视图进行交互,并且如果窗口在窗口上发生onCloseRequest
事件手动关闭,则需要关闭该线程。
编辑: 主要
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
public void start(Stage primaryStage) throws Exception {
primaryStage = new Stage();
FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/Main.fxml"));
Parent root = loader.load();
var controller = (MainController) loader.getController();
primaryStage.setScene(new Scene(root));
primaryStage.setOnCloseRequest(event -> {
controller.stopThread();
});
primaryStage.show();
}
}
主控制器
public class MainController {
@FXML
private TextField tfTest;
@FXML
private BorderPane borderPane;
private LeKillerThread thread;
@FXML
public void btStartClick(ActionEvent actionEvent) {
//save time to use in Thread? Does not do that now.
}
public void initialize() {
thread = new LeKillerThread(borderPane);
thread.start();
}
public void stopThread() {
this.thread.interrupt();
}
}
线程
public class LeKillerThread extends Thread {
private Node nodeToKill;
public LeKillerThread(Node nodeToKill) {
this.nodeToKill = nodeToKill;
}
@Override
public void run() {
try {
//not gonna test this with days...
TimeUnit.SECONDS.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
Platform.runLater(() -> {
this.nodeToKill.setDisable(true);
});
}
}
Main.fxml
<BorderPane fx:id="borderPane"
xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml"
fx:controller="MainController"
prefHeight="400.0" prefWidth="600.0">
<center>
<VBox alignment="CENTER">
<TextField fx:id="tfTest"></TextField>
<Button text="start" onAction="#btStartClick"></Button>
</VBox>
</center>
</BorderPane>
我好几天都没有对此进行测试...但是它可以工作。如果要跟踪时间,则必须保存启动时的时间,并在下次启动程序时加载它,并在启动线程以休眠该时间量(而不是固定的时间)时计算出正确的时间差量。明智的做法是在一个循环中而不是一个星期内睡眠较少的时间。因此,您有机会测试Window是否以某种方式消失,而无需告诉您(线程)。仅当您的Window彻底崩溃时,通常才应该考虑这个问题。