【发布时间】:2015-01-08 23:31:41
【问题描述】:
目标是显示类似表格的内容,但在这种情况下,有两组标签,普通的垂直标签和水平标签,两者必须能够滚动并始终保持标签可见。
我以为我已经弄清楚了,它在开始时有效,但是您滚动的次数越多,横幅越偏离位置,它似乎在很长一段时间内的定位中积累了一些错误..这里不知道,或者如果我做错了什么..
在 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);
}
}
【问题讨论】:
标签: javafx-8 scrollpane