我爲加載Java對象創建了這個簡單的測試。但不幸的是,我不能將它用於SQL查詢的大規模測試。加載測試消息
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.collections.ObservableList;
import javafx.concurrent.Service;
import javafx.concurrent.Task;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Region;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class MainApp extends Application
{
GetDailySalesService service = new GetDailySalesService();
TestData obj;
@Override
public void start(Stage stage) throws Exception
{
obj = new TestData();
HBox hb = new HBox(100);
Tab tabA = new Tab("test");
Button button = new Button("Test");
///////////
Region veil = new Region();
veil.setStyle("-fx-background-color: rgba(0, 0, 0, 0.4)");
veil.setPrefSize(240, 160);
ProgressIndicator p = new ProgressIndicator();
p.setMaxSize(140, 140);
p.progressProperty().bind(service.progressProperty());
veil.visibleProperty().bind(service.runningProperty());
p.visibleProperty().bind(service.runningProperty());
//tableView.itemsProperty().bind(service.valueProperty());
//tableView.setMinSize(240, 140);
StackPane stack = new StackPane();
stack.getChildren().addAll(obj.someData(), veil, p);
service.start();
//////////////////
button.setOnAction(new EventHandler<ActionEvent>()
{
@Override
public void handle(ActionEvent e)
{
tabA.setContent(stack);
}
});
TabPane tb = new TabPane();
tb.setPrefSize(300, 300);
tb.getTabs().add(tabA);
hb.getChildren().addAll(button, tb);
hb.setPadding(new Insets(20, 20, 20, 20));
hb.setAlignment(Pos.CENTER);
Scene scene = new Scene(hb, 500, 300);
stage.setTitle("JavaFX and Maven");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args)
{
launch(args);
}
class TestData
{
public Label someData() throws InterruptedException
{
Label lb = new Label("It's working!!!!");
Thread.sleep(6000);
return lb;
}
}
class GetDailySalesService extends Service<ObservableList<Object>>
{
@Override
protected Task createTask()
{
return new GetDailySalesTask();
}
}
class GetDailySalesTask extends Task<ObservableList<Object>>
{
@Override
protected ObservableList<Object> call() throws Exception
{
return (ObservableList<Object>) obj.someData();
}
}
}
你能告訴我這是實施這個測試和改進代碼的正確方法。
爲什麼不能在SQL查詢中使用它? – ItachiUchiha
因爲當我點擊UI組件加載對象時,應用程序凍結1-2秒。 –
在您的someData()中,您正在使Javafx UI線程在返回標籤之前睡眠6秒。請檢查它。 除此之外,將所有數據加載事件放入Task類中,以便它不會影響UI,同時數據從數據庫加載 – ItachiUchiha