類似javaswing中的repaint方法。
因為我的javafx專案中,parentPane中的childPane,在按住鍵盤方向鍵時,會移動childPane的位置。但是移動的時候不連貫,應該是重繪頻率不夠的原因。所以來請教一下,如何主動重繪UI
uj5u.com熱心網友回復:
不是很清楚你的要求,查詢 雙緩沖 ,可能可以解決你的問題uj5u.com熱心網友回復:

代碼很簡單,如圖,在按下右方向鍵的時候,childPane會向右移動。功能很簡單,但是向右移動的時候,肉眼明顯看出來childPane移動的時候,有閃爍,應該是重繪頻率不夠的原因。請大佬給個主動重繪的demo,萬分感謝~!!!!
uj5u.com熱心網友回復:
代碼附上package com.foo.example;
import application.panel.common.ImageUtil;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundImage;
import javafx.scene.layout.BackgroundPosition;
import javafx.scene.layout.BackgroundRepeat;
import javafx.scene.layout.BackgroundSize;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
public class Test extends Application{
public static Stage stage = null;
public Pane childPane = null;
@Override
public void start(Stage stage) throws Exception {
Test.stage = stage;
Pane pane = new Pane();
pane.setMinSize(800, 500);
pane.setBackground((new Background(new BackgroundImage(new Image(ImageUtil.getFloorUrl("1")), BackgroundRepeat.NO_REPEAT, BackgroundRepeat.NO_REPEAT, BackgroundPosition.CENTER, BackgroundSize.DEFAULT))));
childPane = new Pane();
childPane.setMinSize(50, 50);
childPane.setBackground(new Background(new BackgroundImage(new Image(ImageUtil.getRoleUrl("1")), BackgroundRepeat.NO_REPEAT, BackgroundRepeat.NO_REPEAT, BackgroundPosition.CENTER, BackgroundSize.DEFAULT)));
pane.getChildren().add(childPane);
Group group = new Group();
group.getChildren().add(pane);
Scene scene = new Scene(group);
scene.setOnKeyPressed(new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent e) {
if(e.getCode() == KeyCode.RIGHT) {
System.out.println("==");
childPane.setLayoutX(childPane.getLayoutX() + 5);
}
}
});
Test.stage.setScene(scene);
Test.stage.centerOnScreen();
Test.stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
uj5u.com熱心網友回復:
降低速度 +5 =》+3,+1,增加延時,sleep 100ms,50ms
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/61720.html
標籤:Java EE
上一篇:JAVA課程設計
