目標是顯示一個類似於內容的表格,但在這種情況下,使用2組標籤,即標準垂直標籤和水平標籤,兩者必須能夠滾動並保留標籤隨時都可見。Javafx 8滾動窗格內容標題位置不正確
我以爲我弄明白了,它在一開始就起作用,但越滾越多的橫幅越位,它似乎積累了一些錯誤定位一個很長的路..不知道在這裏,否則我做錯事..
Win7上運行的措施JavaFX的8.25
我的代碼:
package com.hdk.tests;
import javafx.application.Application;
import javafx.beans.InvalidationListener;
import javafx.beans.Observable;
import javafx.scene.Scene;
import javafx.scene.SnapshotParameters;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.image.ImageView;
import javafx.scene.image.WritableImage;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class TestScrollPaneBanners extends Application {
boolean photoYInited = false;
boolean photoHInited = false;
ImageView photoY ;
ImageView photoX;
@Override
public void start(Stage primaryStage) {
ScrollPane scrollPane = new ScrollPane();
scrollPane.setPrefSize(400, 400);
scrollPane.setMaxSize(400, 400);
BorderPane panel = new BorderPane();
panel.setPrefSize(600, 600);
HBox horizontalStripp= new HBox();
horizontalStripp.setPrefHeight(35);
horizontalStripp.setMaxWidth(Double.MAX_VALUE);
horizontalStripp.getChildren().add(new Label("Vertical"));
HBox header = new HBox();
HBox.setHgrow(header, Priority.ALWAYS);
header.setMaxWidth(Double.MAX_VALUE);
header.setStyle("-fx-background-color:red;");
Label tl = new Label("This is the top label.....");
tl.setMaxWidth(Double.MAX_VALUE);
header.getChildren().add(tl);
horizontalStripp.getChildren().add(header);
panel.setTop(horizontalStripp);
scrollPane.setContent(panel);
VBox verticalStripp = new VBox();
verticalStripp.setStyle("-fx-background-color:blue;");
verticalStripp.getChildren().add(new Label("Vertical"));
verticalStripp.getChildren().add(new Label("Vertical"));
verticalStripp.getChildren().add(new Label("Vertical"));
verticalStripp.getChildren().add(new Label("Vertical"));
verticalStripp.getChildren().add(new Label("Vertical"));
verticalStripp.getChildren().add(new Label("Vertical"));
panel.setLeft(verticalStripp);
Label infoX = new Label();
Label infoY = new Label();
scrollPane.vvalueProperty().addListener(new InvalidationListener(){
@Override
public void invalidated(Observable observable) {
double visibleHeight = panel.getHeight()-scrollPane.getPrefHeight();
double posX = visibleHeight*scrollPane.getVvalue();
boolean visible = posX<header.getHeight();
String tx ="VisibleY ="+ visible+ " V ="+scrollPane.getVvalue() ;
if(!visible){
//take photo
if(!photoYInited){
//init image
WritableImage sns = header.snapshot(new SnapshotParameters(), null);
photoY = new ImageView(sns);
photoYInited = true;
panel.getChildren().add(photoY);
}
//position image in borderpane
photoY.setLayoutY(posX);
photoY.setLayoutX(header.getLayoutX());
tx+=" Cx "+header.getLayoutX()+" Cy "+posX;
}else{
//its visible
//remove photo if present
if(photoYInited){
panel.getChildren().remove(photoY);
photoYInited = false;
}
}
infoY.setText(tx);
}
});
scrollPane.hvalueProperty().addListener(new InvalidationListener(){
@Override
public void invalidated(Observable observable) {
double visibleWidth = panel.getWidth()-scrollPane.getWidth();
double posX = visibleWidth*scrollPane.getHvalue();
boolean visible = posX < verticalStripp.getWidth();
String tx ="VisibleX ="+ visible+ " H ="+scrollPane.getHvalue() ;
if(!visible){
//take photo
if(!photoHInited){
//init image
WritableImage sns = verticalStripp.snapshot(new SnapshotParameters(), null);
photoX = new ImageView(sns);
photoHInited = true;
panel.getChildren().add(photoX);
}
photoX.setLayoutX(posX);
photoX.setLayoutY(verticalStripp.getLayoutY());
tx+=" Cx "+posX+" Cy "+verticalStripp.getLayoutY();
}else{
//its visible
//remove photo if present
if(photoHInited){
panel.getChildren().remove(photoX);
photoHInited = false;
}
}
infoX.setText(tx);
}
});
VBox all = new VBox();
all.getChildren().add(scrollPane);
all.getChildren().add(infoX);
all.getChildren().add(infoY);
Scene scene = new Scene(all, 400, 500);
primaryStage.setTitle("Hello Banner!");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
感謝您的反饋!我想過這個問題,但似乎沒有邏輯,但編程並不總是logic..Will也許嘗試一下另一個窗格這就是問題所在.. – sja