繁体   English   中英

Javafx-旋转节点以在手持设备上以横向/纵向显示

[英]Javafx - Rotate node for display in landscape/portrait orientation on handheld device

我正在开发的javafx应用程序最终将在分辨率为800x480的手持设备上运行。 该应用程序通常将以纵向模式运行,但是对于某些功能(例如,显示图表和表格),将需要切换到横向模式以更好地显示数据。

我的问题是,是否有直接的方法来操作以90度的倍数旋转的节点?

我可以通过调用setRotate()来旋转表,尽管这引入了几个新问题:

要在旋转时调整列宽的大小,用户必须将列分隔符从左向右拖动(与标题行垂直),表格仍然将其宽度/高度扩展到其父级的大小,尽管这样做不起作用旋转-90度(或90的其他倍数)时。

另一个约束是,图表内容包含在BorderPane的中心,BorderPane的顶部和底部包含工具栏,这会阻止旋转整个场景。

这是我的SSCCE; 如果下面的代码有任何问题,请纠正我。



    import javafx.application.Application;
    import javafx.event.ActionEvent;
    import javafx.event.EventHandler;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.control.TableColumn;
    import javafx.scene.control.TableView;
    import javafx.scene.layout.BorderPane;
    import javafx.scene.layout.Pane;
    import javafx.scene.layout.VBox;
    import javafx.stage.Stage;

    public class TablePanelTrial extends Application {

        BorderPane root = new BorderPane();
        private boolean isRotated=false;

        public static void main(String[] args) {
            launch(args);
        }

        @Override
        public void start(final Stage primaryStage) {
            primaryStage.setTitle("Table Panel Trial");

            final TablePanel tp = new TablePanel();

            Button btnRotate = new Button("Rotate");
            btnRotate.setOnAction(new EventHandler() {
                @Override
                public void handle(ActionEvent event) { 
                    double r = -90;        
                    if(isRotated){
                        r=0;
                        isRotated = !isRotated;
                    }
                    else{
                        r=-90;
                        tp.tv.setMinHeight(200);
                        isRotated = !isRotated;
                    }
                    tp.rotate(r);
                }
            });        

            root.setTop(btnRotate);
            root.setCenter(tp.getVB());
            primaryStage.setScene(new Scene(root, 480, 800));
            primaryStage.show();
        }  

        class TablePanel{
            private VBox vbox = new VBox();
            private TableView tv = new TableView();
            private String[] labelVal = {"Column 1", "Element", "Difference", "File Name", "Report Number"};

            public TablePanel(){
                TableColumn column1 = new TableColumn(labelVal[0]);
                TableColumn column2 = new TableColumn(labelVal[1]);
                TableColumn column3 = new TableColumn(labelVal[2]);
                TableColumn column4 = new TableColumn(labelVal[3]);
                TableColumn column5 = new TableColumn(labelVal[4]);
                tv.getColumns().addAll(column1, column2, column3, column4,column5);
                vbox.getChildren().add(tv);
                tv.setPrefHeight(2000);
            }

            public Pane getVB(){
                return vbox;
            }

            public void rotate(double r){
                vbox.setRotate(r);
            }
        }
    }
    

我认为,当设备进入横向模式时,Android不执行旋转功能。 必须重新绘制布局才能显示它。 使用JavaFX,您可以做的是在场景中添加一个侦听器以侦听宽度的变化,因为这基本上就是这种情况。 您最有可能这样做:


scene.widthProperty().addListener((observable, oldValue, newValue) -> {
         ....initialize the changes to layout here....
    );
});

可能不是最好的解决方案,但我想它是对的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM