//弹窗dialogPane
//更好的Dialog
import javafx.application.Application;
import javafx.concurrent.ScheduledService;
import javafx.concurrent.Task;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.text.Text;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.util.Duration;
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
Button button = new Button("点击显示窗口");
AnchorPane an = new AnchorPane();
an.setStyle("-fx-background-color:#00CED1");
an.getChildren().addAll(button);
AnchorPane.setTopAnchor(button,100.0);
AnchorPane.setLeftAnchor(button,100.0);
// an.setPadding(new Insets(100));
// button.setOnAction(action -> System.out.println(button.getText()));
button.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
DialogPane dialogPane = new DialogPane();
//新窗口
Stage stage = new Stage();
Scene sc = new Scene(dialogPane);
stage.setScene(sc);
//标题文字
dialogPane.setHeaderText("从一到十");
//内容文字
dialogPane.setContentText("内容文字");
ImageView imageView = new ImageView("icon/icon.png");
dialogPane.setGraphic(imageView);
dialogPane.setStyle("-fx-background-color:#7CCD7C");
//扩展内容
dialogPane.setExpandableContent(new Text("扩展的内容"));
//添加组件
dialogPane.getButtonTypes().add(ButtonType.APPLY);
dialogPane.getButtonTypes().add(ButtonType.CLOSE);
Button apply = (Button)dialogPane.lookupButton(ButtonType.APPLY);
Button close = (Button)dialogPane.lookupButton(ButtonType.CLOSE);
apply.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
System.out.println(apply.getText());
}
});
close.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
System.out.println(close.getText());
}
});
//不允许改变大小
stage.setResizable(false);
stage.setTitle("这是弹出的窗口");
stage.initOwner(primaryStage);
// stage.initStyle(StageStyle.UTILITY);//去掉装饰UNDECORATED
stage.initModality(Modality.WINDOW_MODAL);//模态化
stage.show();
myScheduledService my = new myScheduledService(dialogPane,stage);
my.setDelay(Duration.seconds(0));
my.setPeriod(Duration.seconds(1));
my.start();
}
});
Scene scene = new Scene(an);
primaryStage.setScene(scene);
primaryStage.setTitle("JavaFX");
primaryStage.setWidth(500);
primaryStage.setHeight(500);
primaryStage.show();
}
}
class myScheduledService extends ScheduledService<Integer>{
int i = 0;
private DialogPane dialogPane = null;
private Stage stage = null;
myScheduledService(DialogPane dialogPane, Stage stage){
this.dialogPane = dialogPane;
this.stage = stage;
}
@Override
protected Task<Integer> createTask() {
return new Task<Integer>() {
@Override
protected Integer call() throws Exception {
return i = i + 1;
}
@Override
protected void updateValue(Integer value) {
if(value <= 10){
myScheduledService.this.dialogPane.setContentText(String.valueOf(value));
}
else{
stage.close();
myScheduledService.this.cancel();
System.out.println("!");
}
}
};
}
}
参照:https://space.bilibili.com/5096022/video?tid = 36&page = 8&keyword =&order = pubdate